Algorithm
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.
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.
Print "YES" or "NO" (without the quotes) depending on the answer to the problem.
3
xxo
xox
oxx
YES
4
xxxo
xoxo
oxox
xxxx
NO
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 101;
int n;
int di[] = { 1, 0, -1, 0 };
int dj[] = { 0, 1, 0, -1 };
char g[N][N];
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%s", g[i]);
int res = 0;
for(int i = 0; i < n; ++i)
for(int j = 0, cnt = 0; j < n; ++j) {
for(int k = 0, ni, nj; k < 4; ++k) {
ni = i + di[k];
nj = j + dj[k];
if(ni >= 0 && ni < n && nj >= 0 && nj < n && g[ni][nj] == 'o')
++cnt;
}
if(cnt % 2 == 0)
++res;
}
if(res == (n * n))
puts("YES");
else
puts("NO");
return 0;
}
Copy The Code &
Try With Live Editor
Input
xxo
xox
oxx
Output
Demonstration
Codeforces Solution-Appleman and Easy Task-Solution in C, C++, Java, Python