码迷,mamicode.com
首页 > 其他好文 > 详细

uva 260 - Il Gioco dell'X

时间:2016-01-14 22:20:43      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

题解:

  一定有人获胜,非黑即白;获胜条件为:black是由 上走到下,white是由 左走到右;

 1 #include <cstdio>
 2 using namespace std;
 3 int N;
 4 char board[201][201];
 5 const int direction[][2] = {{-1,-1},{-1,0},{0,-1},{0,1},{1,0},{1,1}};
 6 void DFS(int i, int j, char c,int &win)
 7 {
 8     board[i][j] = .;
 9     if (c == b && i == N-1) win = 1;
10     if (c == w && j == N-1) win = 2;
11     for (int x=0; x<6; ++x){
12         int i_next = i+direction[x][0];
13         int j_next = j+direction[x][1];
14         if (i_next<0 || i_next>=N || j_next<0 || j_next>=N) continue;
15         if (board[i_next][j_next] == c)
16             DFS(i_next, j_next, c, win);
17     }
18 }
19 int main()
20 {
21     //reopen("input.txt","rt",stdin);
22     int Case = 1;
23     while (scanf("%d",&N)){
24         if (!N) break;
25         for (int i=0; i<N; ++i)
26             scanf("%s",board[i]);
27         int win = 0; // win=1:Black  win=2:White
28         for (int i=0; i<N; ++i)
29             if (board[i][0] == w)
30                 DFS(i, 0, w, win);
31         for (int j=0; j<N; ++j)
32             if (board[0][j] == b)
33                 DFS(0, j, b, win);
34         if (win == 1) printf("%d B\n",Case++);
35         else printf("%d W\n",Case++);
36     }
37     return 0;
38 }

 

题解:

  必有一胜,所以只判断black胜不胜就够了。black胜利的条件是能从第一行走到最后一行,white的胜利条件是能出第一列走到最后一列;

 1 #include <iostream>
 2 using namespace std;
 3 
 4 const int maxn = 200 + 10;
 5 
 6 char board[maxn][maxn];
 7 
 8 void wb(int x, int y, int n, char co)
 9 {
10     if(x>=0 && x<n && y>=0 && y<n)
11         if(board[x][y] == co)
12         {
13             board[x][y] = 1;
14             wb(x-1, y-1, n, co);
15             wb(x, y-1, n, co);
16             wb(x+1, y, n, co);
17             wb(x-1, y, n, co);
18             wb(x, y+1, n, co);
19             wb(x+1, y+1, n, co);
20         }
21 }
22 
23 int main()
24 {
25     int n, i, j, ans = 0;
26     while(cin>>n && n)
27     {
28         for(i = 0; i < n; i++)
29             for(j = 0; j < n; j++)
30                 cin >> board[i][j];
31 
32         bool flag = false;
33         //black: first row
34         for(j = 0; j < n; j++)
35             if(board[0][j] == b)
36                 wb(0, j, n, b);
37         for(j = 0; j < n; j++)
38             if(board[n-1][j] == 1)
39             {
40                 cout << ++ans << " B" << endl;
41                 flag = true;
42                 break;
43             }
44 
45         if(flag)
46             continue;
47         else cout << ++ans << " W" << endl;
48     }
49     return 0;
50 }

 

uva 260 - Il Gioco dell'X

标签:

原文地址:http://www.cnblogs.com/aze-003/p/5131652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!