# zoj1002 Fire Net

879人阅读 评论(1)

1. //zoj1002 Fire Net
2. //Accepted 1002 C++ 00:00.01 436K
3. #include <stdio.h>
4. int n;
5. int max;
6. char map[5][5];
7. bool canput(int x, int y)
8. {
9.        int i,j;
10.        if (map[x][y]!='.') return false;
11.        for (i=x+1; i<n; i++){
12.               if (map[i][y]=='X') break;
13.               if (map[i][y]=='O') return false;
14.        }
15.        for (i=x-1; i>=0; i--){
16.               if (map[i][y]=='X') break;
17.               if (map[i][y]=='O') return false;
18.        }
19.        for (j=y+1; j<n; j++){
20.               if (map[x][j]=='X') break;
21.               if (map[x][j]=='O') return false;
22.        }
23.        for (j=y-1; j>=0; j--){
24.               if (map[x][j]=='X') break;
25.               if (map[x][j]=='O') return false;
26.        }
27.        return true;
28. }
29. bool finish()
30. {
31.        for (int i=0; i<n; ++i)
32.               for (int j=0; j<n; ++j)
33.                      if (canput(i,j))
34.                             return false;
35.        return true;
36. }
37. void backtrack(int num)
38. {
39.        if (finish()){
40.               if (num>max) max = num;
41.               return ;
42.        }
43.        for (int i=0; i<n; ++i)
44.               for (int j=0; j<n; ++j)
45.                      if (canput(i,j)){
46.                             map[i][j] = 'O';
47.                             backtrack(num+1);
48.                             map[i][j] = '.';
49.                      }
50. }
51. void solve()
52. {
53.        getchar();
54.        for (int i=0; i<n; ++i){
55.               for (int j=0; j<n; ++j)
56.                      scanf ("%c",&map[i][j]);
57.               getchar();
58.        }
59.        max = 0;
60.        backtrack(0);
61.        printf ("%d/n",max);
62. }
63. int main()
64. {
65. #ifdef ONLINE_JUDGE
66. #else
67.        freopen("1002.txt","r",stdin);
68. #endif
69.        while (scanf("%d",&n)!=EOF && n)
70.               solve();
71. #ifdef ONLINE_JUDGE
72. #else
73.        fclose(stdin);
74. #endif
75.        return 0;
76. }
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：36771次
• 积分：664
• 等级：
• 排名：千里之外
• 原创：33篇
• 转载：0篇
• 译文：0篇
• 评论：10条
文章分类
评论排行
最新评论