以前花了无数时间在这题上,今天终于弄懂了。
这题的数据量很小,直接暴力就可。每个点有两种情况:放或不放。 用 0 ,1表示。遍历 + 判断。
#include <iostream>
using namespace std;
int cot[4][4],N,ans,flag,sum;
bool Jud(int x,int y)
{
int i;
if(cot[x][y] == 0){
for(i = x-1;i >= 0;i --)
{
if(cot[i][y] == 1) return 0;
else if(cot[i][y] == 2) break;
}
for(i = x +1;i < N;i ++)
{
if(cot[i][y] == 1) return 0;
else if(cot[i][y] == 2) break;
}
for(i = y-1;i >= 0;i --)
{
if(cot[x][i] == 1) return 0;
else if(cot[x][i] == 2) break;
}
for(i = y+1;i < N;i ++)
{
if(cot[x][i] == 1) return 0;
else if(cot[x][i] == 2) break;
}
return 1;
}
return 0;
}
void Dfs(int s)
{
int i,j;
for(i = 0;i < N;i ++)
{
for(j = 0;j < N;j ++)
{
if(Jud(i,j))
{
cot[i][j] = 1;
Dfs(s+1);
cot[i][j] = 0;
}
}
}
if(s > ans) ans = s;
}
int main()
{
int i,j;
char c;
while(cin>>N&&N)
{
for(i = 0;i < N;i ++)
{
for(j = 0;j < N;j ++)
{
cin>>c;
if(c == 'X') cot[i][j] = 2;
else cot[i][j] = 0;
}
}
ans = 0;
Dfs(0);
cout<<ans<<endl;
}
}