Description
Tom's Meadow
Tom has a meadow in his garden. He divides it into N * M squares. Initially all the squares were covered with grass. He mowed down the grass on some of the squares and thinks the meadow is beautiful if and only if
- Not all squares are covered with grass.
- No two mowed squares are adjacent.
Two squares are adjacent if they share an edge. Here comes the problem: Is Tom's meadow beautiful now?
Input
The input contains multiple test cases!
Each test case starts with a line containing two integers N, M (1 <= N, M <= 10) separated by a space. There follows the description of Tom's Meadow. There're N lines each consisting of M integers separated by a space. 0(zero) means the corresponding position of the meadow is mowed and 1(one) means the square is covered by grass.
A line with N = 0 and M = 0 signals the end of the input, which should not be processed
Output
One line for each test case.
Output "Yes" (without quotations) if the meadow is beautiful, otherwise "No"(without quotations).
Sample Input
2 2
1 0
0 1
2 2
1 1
0 0
2 3
1 1 1
1 1 1
0 0
Sample Output
Yes
No
No
解题思路:
题意为1.若有两个相近的数都为0,或2.所有数都为1,则输出No,此题中为了不让第一排第一列和最后一排最后一列成为特殊情况,构建数组a[13][13]并赋初值1,并a[1][1]成为首元素,这样第一排第一列和最后一排最后一列的周围也都有了值,可与其它数一同判断,
细节处理:
数组a的行数和列数要大于等于12
代码:
#include<iostream>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
if(n==0&&m==0) break;
int i,j;
int a[15][15];
int flag=0,flag1=1;
for(i=0;i<=14;i++)
for(j=0;j<=14;j++)
a[i][j]=1;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]==0) flag=1;
}
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
if((a[i][j]==0&&a[i-1][j]==0)||(a[i][j]==0&&a[i+1][j]==0)||(a[i][j]==0&&a[i][j-1]==0)||(a[i][j]==0&&a[i][j+1]==0))
flag1=0;
}
if(flag&&flag1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}