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
题解:
个人感觉昨晚上的CF的A题坑到要死了,正确题意是若所给矩阵中的每一个位置四面相邻的共有偶数个'o'的话,就是YES,否则是NO,但真心无力吐槽题中的一句话Two cells of the board are adjacent if they share a side.什么是共用一条边。。。自然而然想到了同行或同列,但是WA了两个小时,今天看题解才知道,这句话就是个摆设,直接考虑边界判断str[i-1][j] str[i+1][j] str[i][j-1] str[i][j+1] 就好了。。。Orz。。。。。
题目链接:http://codeforces.com/problemset/problem/462/A
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
int n;
char str[110][110];
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++){
gets(str[i]);
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int cnt=0;
if(i!=0&&str[i-1][j]=='o') cnt++;
if(i!=n-1&&str[i+1][j]=='o') cnt++;
if(j!=0&&str[i][j-1]=='o') cnt++;
if(j!=n-1&&str[i][j+1]=='o') cnt++;
if(cnt%2==1) ans=1;
}
}
if(ans) printf("NO\n");
else printf("YES\n");
return 0;
}