第一道dfs,值得纪念
#include <stdio.h>
const int max=5;
char map[max][max];
int visit[max][max];
int count,k,maxn,n;
int judge(int x,int y)
{
int i,j;
for(i=x,j=y-1;j>=0;j--)
{
if(visit[i][j]==1) return 0;
else if(map[i][j]=='X') break;
}
for(j=y,i=x-1;i>=0;i--)
{
if(visit[i][j]==1) return 0;
else if(map[i][j]=='X') break;
}
return 1;
}
void dfs(int k,int count)
{
int xn,yn;
if(k==n*n)
{
if(count>maxn)
{
maxn=count;
}
return;
}
else
{
xn=k/n;
yn=k%n;
if(visit[xn][yn]==0&&judge(xn,yn)&&map[xn][yn]=='.')
{
count++;
visit[xn][yn]=1;
dfs(k+1,count);
visit[xn][yn]=0;
count--;
}
dfs(k+1,count);
}
}
int main()
{
int i,j;
while(scanf("%d",&n)==1)
{
if(n==0) break;
count=0;
k=0;
maxn=0;
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<n;j++)
{
scanf("%c",&map[i][j]);
visit[i][j]=0;
}
}
dfs(0,0);
printf("%d\n",maxn);
}
return 0;
}