Ancient Go(简单DFS)

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5546

AC代码:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<bitset>
  6 #include<cassert>
  7 #include<cctype>
  8 #include<cmath>
  9 #include<cstdlib>
 10 #include<ctime>
 11 #include<deque>
 12 #include<iomanip>
 13 #include<list>
 14 #include<map>
 15 #include<queue>
 16 #include<set>
 17 #include<stack>
 18 #include<vector>
 19 using namespace std;
 20 typedef long long ll;
 21 const double pi = acos(-1.0);
 22 const ll mod = 1e9 + 7;
 23 const int inf = 0x3f3f3f3f;
 24 const int maxn = 5e4 + 50;
 25 int Next[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
 26 char str[150][150];
 27 int vis[150][150];
 28 bool check1(int x, int y)
 29 {
 30     vis[x][y] = 1;
 31     for(int i=0;i<4;i++)
 32     {
 33         int xx = x + Next[i][0];
 34         int yy = y + Next[i][1];
 35         if(xx >= 0 && xx < 9 && yy >= 0 && yy < 9 && !vis[xx][yy])
 36         {
 37             /* 两种情况表示在已经换了一个的情况下,还没有封死*/
 38             if(str[xx][yy] == '.') return true;
 39             if(str[xx][yy] == 'o' && check1(xx, yy)) return true;
 40         }
 41     }
 42     return false;
 43 }
 44 bool check(int x, int y)
 45 {
 46     str[x][y] = 'x';///把点换为‘x'
 47     bool flag= false;
 48     for(int i=0;i<4;i++)
 49     {
 50         int xx = x + Next[i][0];
 51         int yy = y + Next[i][1];
 52         if(xx >= 0 && xx < 9 && yy >= 0 && yy < 9)
 53         {
 54             if(str[xx][yy] == 'o')
 55             {
 56                 memset(vis, 0, sizeof(vis));
 57                 if(!check1(xx, yy))///检查此'o'是否被封死
 58                 {
 59                     flag = true;
 60                     break;
 61                 }
 62             }
 63         }
 64     }
 65     str[x][y] = '.';///取消改换
 66     return flag;
 67 }
 68 int main()
 69 {
 70     int t;
 71     cin >> t;
 72     int k = 1;
 73     while(t--)
 74     {
 75         for(int i=0;i<9;i++)
 76         {
 77             cin >> str[i];
 78         }
 79         bool flag = false;
 80         for(int i=0;i<9;i++)
 81         {
 82             for(int j=0;j<9;j++)
 83             {
 84                 if(str[i][j] == 'x' || str[i][j] == 'o') continue;
 85                 else
 86                 {
 87                     if(check(i, j))///检查是否有o的周围再加一个x可以被封死
 88                     {
 89                         flag = true;
 90                         break;
 91                     }
 92                 }
 93             }
 94             if(flag) break;
 95         }
 96         cout << "Case #" << k++ << ": ";
 97         if(flag) cout << "Can kill in one move!!!" << endl;
 98         else cout << "Can not kill in one move!!!" << endl;
 99     }
100     return 0;
101 }
View Code

猜你喜欢

转载自www.cnblogs.com/wsy107316/p/11323046.html