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
解题说明:此题是一道图形题,要求判断棋盘中的每个单元是否都有偶数个“o”相邻,最简单的办法是依次遍历,如果遇到一个不满足的就跳出循环打印NO。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
int n,i,j,count,flag=0;
char a[102][102];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
count=0;
if((i-1)>=0&&a[i-1][j]=='o')
{
count++;
}
if((j-1)>=0&&a[i][j-1]=='o')
{
count++;
}
if((i+1)<n&&a[i+1][j]=='o')
{
count++;
}
if((j+1)<n&&a[i][j+1]=='o')
{
count++;
}
if(count%2!=0)
{
flag=1;
break;
}
}
if(flag==1)
{
break;
}
}
if(flag==1)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
return 0;
}