链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1045
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#problem/A
一看原题,先用搜索写一下,还是要学学匹配吧!
以前的代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 10 #define INF 0x3f3f3f3f int n, ans; char G[N][N]; bool judge(int x, int y) { int i; if(G[x][y]==‘X‘) return false; for(i=x; i>=0; i--) { if(G[i][y]==‘X‘) break; if(G[i][y]==‘D‘) return false; } for(i=y; i>=0; i--) { if(G[x][i]==‘X‘) break; if(G[x][i]==‘D‘) return false; } return true; } void DFS(int z, int k) { int x, y; x = z/n; y = z%n; if(z==n*n) { ans = max(ans, k); return ; } if(judge(x, y)) { G[x][y] = ‘D‘; DFS(z, k+1); G[x][y] = ‘.‘; } DFS(z+1, k); } int main() { while(scanf("%d", &n),n) { int i; memset(G, 0, sizeof(G)); for(i=0; i<n; i++) scanf("%s", G[i]); ans = 0; DFS(0, 0); printf("%d\n", ans); } return 0; }
时间: 2024-10-28 17:56:45