题目:
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
题意分析:
代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
char a[105][105];
int main()
{
int n;
char c;
int flag;
cin >> n;
memset(a, 0, sizeof(a));
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
cin >> c;
if (c == 'o')
a[i][j] = 1;
}
flag = 1;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
int cnt = 0;
if (a[i-1][j])
++cnt;
if (a[i+1][j])
++cnt;
if (a[i][j-1])
++cnt;
if (a[i][j+1])
++cnt;
if (cnt & 1)
{
flag = false;
break;
}
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
解答Appleman的简单任务
本文解释了如何解决Appleman提出的关于检查棋盘上每个单元格与其周围单元格中'o'字符数量是否全为偶数的简单问题。通过遍历棋盘并计算每个单元格周围'o'的数量,确定最终答案。
1294

被折叠的 条评论
为什么被折叠?



