hdu 1045 Fire Net

http://acm.hdu.edu.cn/showproblem.php?pid=1045  暴力搜索

View Code
 1 #include<iostream>
2 #include<cstring>
3 #include<cstdlib>
4 #include<cstdio>
5 using namespace std;
6 char mat[10][10];
7 int n,maxx;
8 bool ok(int x,int y)//put here is ok
9 {
10 if(mat[x][y]!='.') return false;
11 int i,j;
12 for(i=x-1;i>=0;i--)
13 {
14 if(mat[i][y]=='X') break;
15 else if(mat[i][y]=='O') return false;
16 }
17 for(j=y-1;j>=0;j--)
18 {
19 if(mat[x][j]=='X') break;
20 else if(mat[x][j]=='O') return false;
21 }
22 return true;
23 }
24 void search(int pos,int num)//自左向右,自上而下
25 {
26 if(pos==n*n)//
27 {
28 if(maxx<num) maxx=num;
29 return;
30 }
31 int x=pos/n;
32 int y=pos%n;
33 if(ok(x,y))
34 {
35 mat[x][y]='O';
36 search(pos+1,num+1);
37 mat[x][y]='.';
38 }
39 search(pos+1,num);
40 }
41 int main()
42 {
43 int i,j;
44 while(cin>>n && n)
45 {
46 for(i=0;i<n;i++)
47 for(j=0;j<n;j++) cin>>mat[i][j];
48 maxx=-1;
49 search(0,0);
50 cout<<maxx<<endl;
51 }
52 return 0;
53 }


 

posted @ 2012-04-01 23:24  keepmoving89  阅读(112)  评论(0编辑  收藏  举报