A. Appleman and Easy Task

来源:互联网 发布:java并发编程实战 pdf 编辑:程序博客网 时间:2024/06/06 10:56

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
3xxoxoxoxx
output
YES
input
4xxxoxoxooxoxxxxx
output
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;}


0 0
原创粉丝点击