您的位置:首页 > 其它

zoj 2850 Beautiful Meadow

2017-03-09 14:58 309 查看
Beautiful MeadowTime Limit: 2 Seconds Memory Limit: 65536 KB


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

#include <iostream>
using namespace std;
int main(){
int n, m;
int p[10][10];
int i, j, k;
while(cin >> n >> m){
if(n == 0 && m == 0){
break;
}
int flag = 1;//表示没修剪过
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
cin >> p[i][j];
if(p[i][j] == 0)
flag = 0;
}
}
if(flag == 1){
cout << "No" << endl;
continue;
}
//判断第0行
for(k = 1; k < m; k++){
if(p[0][k] == 0 && p[0][k - 1] == 0){
cout << "No" << endl;
goto RL;
}
}
//判断第0列
for(k = 1; k < n; k++){
if(p[k][0] == 0 && p[k - 1][0] == 0){
cout << "No" << endl;
goto RL;
}
}
//判断第1——n-1行
for(i = 1; i < n; i++){
for(j = 1; j < m; j++){
if(p[i][j] == 0 && p[i - 1][j] == 0){
cout << "No" << endl;
goto RL;
}
if(p[i][j] == 0 && p[i][j - 1] == 0){
cout << "No" << endl;
goto RL;
}
}
}
cout << "Yes" << endl;
continue;
RL:
continue;
}
//system("pause");
return 0;
}


总结:1.需要判断是不是、有没有,用一个标识符就行,此题可以不用一个变量做计数(判断1的个数是不是和矩阵大小一样,如果一样,则说明没有修剪,不漂亮)

2.现学的goto语句,第二层内循环想要直接结束第一层循环,用goto语句(以前不知道这么用。。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: