Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn‘t know how to solve it. Can you help him?
Given a n × n checkerboard. Each cell of the board has either character ‘x‘, or character ‘o‘. Is it true that each cell of the board has even number of adjacent cells with ‘o‘? Two cells of the board are adjacent if they share a side.
Input
The first line contains an integer n (1 ≤ n ≤ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either ‘x‘ or ‘o‘) without spaces.
Output
Print "YES" or "NO" (without the quotes) depending on the answer to the problem.
Sample test(s)
input
3 xxo xox oxx
output
YES
input
4 xxxo xoxo oxox xxxx
output
NO
英语真的是差啊,没办法,没理解,好好学英语,这学期过CET4,加油!
遍历搞一下这道题就过了。
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <algorithm>
5 6usingnamespace std;
7constint max_size = 105;
8 9int d1[] = {-1, 0, 0, 1};
10int d2[] = {0, -1, 1, 0};
1112bool check(int x, int y, int n)
13{
14if(x >= 0 && y >= 0 && x < n && y < n)
15returntrue;
16returnfalse;
17}
1819int main()
20{
21int n;
22int graph[max_size][max_size];
2324while(scanf("%d%*c", &n) != EOF)
25 {
26for(int i = 0; i < n; i++)
27 {
28for(int j = 0; j < n; j++)
29 {
30char a = getchar();
31if(a == ‘o‘)
32 graph[i][j] = 1;
33else34 graph[i][j] = 0;
35 }
36 getchar();
37 }
3839int tag = false;
40for(int i = 0; i < n; i++)
41 {
42for(int j = 0; j < n; j++)
43 {
44int ans = 0;
45int x, y;
46for(int k = 0; k < 4; k ++)
47 {
48 x = i + d1[k];
49 y = j + d2[k];
50if(check(x, y, n))
51 {
52if(graph[x][y] == 1)
53 ans++;
54 }
55 }
56if(ans % 2 != 0)
57 {
58 printf("NO\n");
59 tag = true;
60break;
61 }
62 }
63if(tag == true)
64break;
65 }
66if(tag == false)
67 printf("YES\n");
68 }
69return0;
70 }