View Code
//数据小,dfs+回溯,能够水过
#include <iostream>
#include
<cstring>
#include
<queue>

using namespace std;
const int MAX = 10;
char map[MAX][MAX];
int n,ans;

bool can(int x,int y)
{
int i,j;

for(i = x+1;i < n;++i)
{
if(map[i][y] == 'Z') return false;
if(map[i][y] == 'X') break;
}
for(i = 0;i < x;++i)
{
if(map[i][y] == 'Z') return false;
if(map[i][y] == 'X') break;
}

for(j = 0;j < y;++j)
{
if(map[x][j] == 'Z') return false;
if(map[x][j] == 'X') break;
}

for(j = y+1;j < n;++j)
{
if(map[x][j] == 'Z') return false;
if(map[x][j] == 'X') break;
}

return true;
}

void dfs(int sum)
{
for(int i = 0;i < n;++i)
{
for(int j = 0;j < n;++j)
{
if(map[i][j] == '.' && can(i,j))
{
map[i][j]
= 'Z';
dfs(sum
+1);
map[i][j]
= '.';
}

if(i == n-1 && j == n-1)
{
if(sum > ans) ans = sum;
return;
}
}
}
}


int main()
{
int i;

while(scanf("%d",&n),n != 0)
{
for(i = 0;i < n;++i) scanf("%s",map[i]);

ans
= -2000000; dfs(0);

cout
<<ans<<endl;
}

return 0;
}
posted on 2011-03-13 14:03  c++fans  阅读(186)  评论(0编辑  收藏  举报