<a target=_blank href="http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1002</a>
</pre><pre name="code" class="cpp">
#include <stdio.h>
#include <memory.h>
typedef struct{
int x;
int y;
}Move;
Move move[4]={{-1,0},{1,0},{0,-1},{0,1}};
char g[5][5];
int max,n;
int check(int x,int y)
{
int i,j,k,a,b;
for(i=0;i<4;i++)
{
a=x+move[i].x;
b=y+move[i].y;
while(a>=0&&a<n&&b>=0&&b<n)
{
if(g[a][b]=='X')
{
break;
}
if(g[a][b]=='@')
{
return 0;
}
a+=move[i].x;
b+=move[i].y;
}
}
return 1;
}
void fun(int k,int cur) //k为当前层数 cur为当前炮台放置个数
{
int i,j,x,y;
if(k==n*n) // 遍历完了
{
if(cur>max)
{
max=cur;
return;
}
}
else
{
x=k/n;
y=k%n; //计算当前坐标
if(g[x][y]=='.')
{
if(check(x,y)) //如果可以放的话 (为了找出最大值考虑所有情况)
{ //就两种 当前位置放或不放
g[x][y]='@';
fun(k+1,cur+1);//放
g[x][y]='.'; // 放完后 考虑不放时 要恢复原样
}
}
fun(k+1,cur); //不放 和放不了
}
}
int main()
{
int i,j,k;
while(scanf("%d",&n)&&n)
{
max=-1;
for(i=0;i<n;i++)
{
scanf("%s",g[i]);
}
fun(0,0);
printf("%d\n",max);
}
return 0;
}