您的位置:首页 > 其它

hdu 1045 Fire Net

2012-04-01 23:24 357 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1045 暴力搜索

View Code

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
char mat[10][10];
int n,maxx;
bool ok(int x,int y)//put here is ok
{
if(mat[x][y]!='.') return false;
int i,j;
for(i=x-1;i>=0;i--)
{
if(mat[i][y]=='X') break;
else if(mat[i][y]=='O') return false;
}
for(j=y-1;j>=0;j--)
{
if(mat[x][j]=='X') break;
else if(mat[x][j]=='O') return false;
}
return true;
}
void search(int pos,int num)//自左向右,自上而下
{
if(pos==n*n)//
{
if(maxx<num) maxx=num;
return;
}
int x=pos/n;
int y=pos%n;
if(ok(x,y))
{
mat[x][y]='O';
search(pos+1,num+1);
mat[x][y]='.';
}
search(pos+1,num);
}
int main()
{
int i,j;
while(cin>>n && n)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++) cin>>mat[i][j];
maxx=-1;
search(0,0);
cout<<maxx<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: