uva 260 - Il Gioco dell'X

题解:

  一定有人获胜,非黑即白;获胜条件为: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

时间: 2025-01-12 15:19:21

uva 260 - Il Gioco dell'X的相关文章

L&#39;opzione di luce del puntatore laser

Prima di tutto, sono di buone dimensioni, non i 'mini' puntatori laser che altri stanno vendendo. è fantastico come un puntatore laser 1000mW e la base magnetica e la luce di area sul lato sono dei vantaggi. Il caso è pesante e sembra ben costruito -

UVA 716 - Commedia dell&#39; arte(三维N数码问题)

UVA 716 - Commedia dell' arte 题目链接 题意:给定一个三维的n数码游戏,要求变换为按顺序,并且最后一个位置是空格,问能否变换成功 思路:和二维的判定方法一样,因为z轴移动,等于交换N^2 - 1次,y轴移动等于交换N - 1次,x轴移动不变,逆序对的奇偶性改变方式不变. 那么n为偶数的时候,逆序对为偶数可以,为奇数不行 n为奇数时候,看空格位置的y轴和z轴分别要走的步数和逆序对的奇偶性相不相同,相同可以,不相同步行 代码: #include <cstdio> #i

uva 716 - Commedia dell&#39; arte(置换)

题目链接:uva 716 - Commedia dell' arte 题目大意:给定一个三维的八数码,0表示空的位置,问说是否可以排回有序序列. 解题思路:对于n为奇数的情况,考虑三维八数码对应以为状态下去除0的时候逆序对数,偶数的情况下,考虑将0的位置转移到(n,n,n)位置后对应序列的逆序对数.如果逆序对数为偶数即为可以,奇数不可以. #include <cstdio> #include <cstring> #include <algorithm> using na

uva 10526 - Intellectual Property(后缀数组)

题目链接:uva 10526 - Intellectual Property 题目大意:给定两个文本,问说下面一个文本中在哪些位置上抄袭了上面个一个文本的,输出n个抄袭位置(不足n个情况全部输出),按照长度优先输出,长度相同的输出位置靠前的. 注意:空格,回车都算一个字符:一段字符只能是抄袭上面的一部分,比如上:NSB*SB 下:NSB 答案:NSB. 解题思路:将两个文本连接在一起,中间用没有出现的字符分割,然后处理处后缀数组,根据height数组的性质,求出哪些位置匹配的长度不为0(注意匹配

UVA 10817 Headmaster&#39;s Headache 状压DP

记录两个状态S1,S2分别记录哪些课程被1个人教过或2个人教过,然后记忆化搜索 UVA - 10817 Headmaster's Headache Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem D: Headmaster's Headache Time limit: 2 seconds The headmaster of Spr

The Sultan&#39;s Successors UVA - 167

the squares thus selected sum to a number at least as high as one already chosen by the Sultan. (For those unfamiliar with the rules of chess, this implies that each row and column of the board contains exactly one queen, and each diagonal contains n

UVa 10115 - Automatic Editing

题目:给你一些字符串的替换关系,以及一个句子.按顺序替换,输出最后结果. 分析:字符串.按照替换顺序依次替换(这个替换用过之后,就不再使用),每个替换可能出现多次. 这里注意,如果当前串中有多个可被当前单词替换的位置,只替换最前面的那个, 下次用本次生成的串替换,而不是整体一次性替换. 说明:注意数据清空. #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio>

UVA 10106 Product (大数相乘)

Product The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer. The Output For each input pair of lines the output line should c

UVA The Sultan&#39;s Successors

题目如下: The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that thecountry will be split into up to k separate parts on her death andeach part will be inherited by whoever performs best at some test. Itis possible for any