2.16 Beautiful Meadow(漂亮的草坪)

                                   2.16 Beautiful Meadow

2.16.1 时空限制

         Time limit: 1 Seconds Memory limit: 32768K 

2.16.2 题目内容

         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 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 Mintegers 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

 2.16.3 题目来源

         Problem Source: Zhejiang Provincial Programming Contest 2007 (Author: CAO, Peng) 

2.16.4 汉语翻译

      1.题目

                                                        漂亮的草坪

       Tom 的花园里有一块草坪。他把它分成 N*M 的方块。一开始,所有的方块上都长着草。他剪去一些方块上的草,并认为仅符合这两个条件这块草坪才算漂亮:
      (1)不是所有的方块上都长着草。
      (2)两块剪去草的方块不能相连。
         如果两块方块共一条边,那么就算是连着的。现在问题来了:Tom 的草坪漂亮吗?

 

     2.输入描述

       输入包含多个测试案例!
     每个测试案例以包含两个整数 N,M(1≤N, M≤10)开始,这两个数占一行,中间用空格隔开。接下来是 Tom 的草坪的描述。有 N 行,每行有 M 个整数,用空格隔开。0 表示该方块被剪去的草,1 表示该方块长着草。
       一行上如果是 N =0 且 M =0,表示输入的结束,这行不要处理。  

     3.输出描述

       一个测试案例输出一行。
      如果这块草坪是漂亮的,输出“Yes”,否则输出“No”(不包括引号)。

    4.输入样例

2 2 
1 0 
0 1 
2 2 
1 1 
0 0 
2 3 
1 1 1 
1 1 1 
0 0 

5.输出样例

Yes 
No 
No 
 

#include <fstream> 
#include <iostream> 
using namespace std; 
int main(int argc, char* argv[]) 
{ 
    int n,m,sum;
	int i,j;
	int ss[10][10];
	while(cin>>n>>m)
	{
		sum=0;
		if(n==0&&m==0)break;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				cin>>ss[i][j];
			}
		}
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(ss[i][j]==0)
				{
					sum++;
					if(j==m-1)
					{
						if(ss[i][j]==ss[i][0])
						{
							cout<<"No"<<endl;
							goto RL;
						}
					}
					else
					{
						if(ss[i][j]==ss[i][j+1])
						{
							cout<<"No"<<endl;
							goto RL;
						}
					}
					if(i==n-1)
					{
						if(ss[i][j]==ss[0][j])
						{
							cout<<"No"<<endl;
							goto RL;
						}
					}
					else
					{
						if(ss[i][j]==ss[i+1][j])
						{
							cout<<"No"<<endl;
							goto RL;
						}
					}
				}
			}
		}
		if(sum!=0)
           cout<<"Yes"<<endl;
		else
			cout<<"No"<<endl;
		continue;
RL:
		continue;
	}
	return 0; 
} 

2.16.5 解题思路

       本题针对一个矩阵,判断该矩阵是否漂亮,题目简单,但判断的逻辑需要好好理顺。
       下面两种情况是不漂亮的,除此以外的任何情况,都是漂亮的:
     (1)全是 1,不漂亮。这就包括这样一种情况,整个草坪只有一个方块,如果方块的草没有被剪去,那就不漂亮。
     (2)两个方块共一条边,即前后两个元素或上下两个元素是 00。

2.16.6 参考答案

#include <iostream> 
using namespace std; 
int main(int argc, char* argv[]) 
{ 
    int p[10][10]; 
    int n,m; 
    int i,j,k; 
    int flag=1;//1 表示没锄过草
    while(cin>>n>>m) 
	{ 
		if(n==0 && m==0)break;//读数据结束
        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;//修剪过
			} 
		} 
       //开始判断
       //第一种情况:没修剪过,即全是 1,则不漂亮
       if(flag==1) 
	   { 
		   cout<<"No"<<endl; 
		   continue; 
	   } 
       //第二种情况:剪的两块共一条边,即前后两个元素或上下两个元素是 00,不漂亮
       //判断第 0 行
	   for(k=1;k<m;k++) 
	   { 
		   if(p[0][k]==0 && p[0][k-1]==0) 
		   { 
			   cout<<"No"<<endl; 
			   goto RL; 
		   } 
	   } 
       //判断第 1~n-1 行
       for(i=1;i<n;i++) 
	   { 
		   for(int j=0;j<m;j++) 
		   { 
			   //本行与上一行同一列元素都是 0 吗?
			   if(p[i][j]==0 && p[i-1][j]==0) 
			   { 
				   cout<<"No"<<endl; 
				   goto RL;//结束
			   } 
			   //本行中当前元素和前一元素都是 0 吗?
			   if(j!=0) 
			   { 
				   if(p[i][j]==0 && p[i][j-1]==0) 
				   { 
					   cout<<"No"<<endl; 
					   goto RL;//结束
				   } 
			   } 
		   } 
	   } 
	   cout<<"Yes"<<endl;//漂亮
	   continue;
RL: 
	   continue; 
	} 
	return 0; 
} 


 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值