W-23

来源:互联网 发布:大数据时代知乎 编辑:程序博客网 时间:2024/05/17 09:09
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

题意描述:

将一个正方形分成几块,每一块一开始均种上草,若所有块的草未剪则不漂亮,所剪的块公用一个边则为不漂亮,其余为漂亮。给你数据,让你判断是否漂亮!

解题思路:

先对第一种情况判断,然后在对数据的第一行进行判断,逐一判断。

细节:

注意第一行与第一列,每个数的前一个和上一个进行判断。

代码:

#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;       }              for(k = 1; k < m; k++){         if(p[0][k] == 0 && p[0][k - 1] == 0){               cout << "No" << endl;               goto RL;           }        }              for(k = 1; k < n; k++){            if(p[k][0] == 0 && p[k - 1][0] == 0){              cout << "No" << endl;               goto RL;             }     }      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;}

心得:

此题较为麻烦,需要耐心!

0 0
原创粉丝点击