zoj 2850 Beautiful Meadow

Beautiful Meadow

Time 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

  1. Not all squares are covered with grass.
  2. 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 NM (1 <= NM <= 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 #include <iostream>
 2 using namespace std;
 3 int main(){
 4     int n, m;
 5     int p[10][10];
 6     int i, j, k;
 7     while(cin >> n >> m){
 8         if(n == 0 && m == 0){
 9             break;
10         }
11         int flag = 1;//表示没修剪过
12         for(i = 0; i < n; i++){
13             for(j = 0; j < m; j++){
14                 cin >> p[i][j];
15                 if(p[i][j] == 0)
16                     flag = 0;
17             }
18         }
19         if(flag == 1){
20             cout << "No" << endl;
21             continue;
22         }
23         //判断第0行
24         for(k = 1; k < m; k++){
25             if(p[0][k] == 0 && p[0][k - 1] == 0){
26                 cout << "No" << endl;
27                 goto RL;
28             }
29         }
30         //判断第0列
31         for(k = 1; k < n; k++){
32             if(p[k][0] == 0 && p[k - 1][0] == 0){
33                 cout << "No" << endl;
34                 goto RL;
35             }
36         }
37         //判断第1——n-1行
38         for(i = 1; i < n; i++){
39             for(j = 1; j < m; j++){
40                 if(p[i][j] == 0 && p[i - 1][j] == 0){
41                     cout << "No" << endl;
42                     goto RL;
43                 }
44                 if(p[i][j] == 0 && p[i][j - 1] == 0){
45                     cout << "No" << endl;
46                     goto RL;
47                 }
48             }
49         }
50         cout << "Yes" << endl;
51         continue;
52         RL:
53             continue;
54     }
55     //system("pause");
56     return 0;
57 }

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

2.现学的goto语句,第二层内循环想要直接结束第一层循环,用goto语句(以前不知道这么用。。)

posted @ 2017-03-09 14:58  琴影  阅读(349)  评论(0编辑  收藏  举报