标签:des blog http io ar os for sp strong
Description

Input
Output
Sample Input
4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
Sample Output
5 1 5 2 4
n皇后的改编题 由于n比较小可以直接dfs
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
char mat[10][10];
int ans,n;
bool ok(int x,int y)
{
int i;
if(mat[x][y]!=‘.‘) return false;
for(i=x+1;i<=n&&mat[i][y]!=‘X‘;i++)
{
if(mat[i][y]==‘0‘) return false;
}
for(i=x-1;i>=1&&mat[i][y]!=‘X‘;i--)
{
if(mat[i][y]==‘0‘) return false;
}
for(i=y;i<=n&&mat[x][i]!=‘X‘;i++)
{
if(mat[x][i]==‘0‘) return false;
}
for(i=y-1;i>=1&&mat[x][i]!=‘X‘;i--)
{
if(mat[x][i]==‘0‘) return false;
}
return true;
}
void dfs(int coun)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(ok(i,j))
{
mat[i][j]=‘0‘;
dfs(coun+1);
mat[i][j]=‘.‘;
}
}
}
if(ans<coun) ans=coun;
}
int main()
{
int i,j;
while(cin>>n&&n!=0)
{
gets(mat[0]);//吸收换行
for(i=1;i<=n;i++)
{
gets(mat[i]+1);
}
ans=0;
dfs(0);
printf("%d\n",ans);
}
return 0;
}
POJ 1315 Don't Get Rooked(dfs)
标签:des blog http io ar os for sp strong
原文地址:http://www.cnblogs.com/sola1994/p/4074215.html