《ACM书中题目》 W
来源:互联网 发布:乔丹荣誉数据 编辑:程序博客网 时间:2024/06/05 16:55
原题
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 ifNot 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).
理解&思路
判断一个给定矩阵是否“漂亮”,漂亮的条件是:
1、不能全是1
2、两个0不能挨着
定义一个变量初始化为1,在输入过程中进行判断,如果输入有0,则该标识变为0,表示矩阵不全为1。 之后再逐行、行间判断是否有两个挨着的0。AC代码
#include<bits/stdc++.h>using namespace std;int main(){ int p[10][10]; int n,m,i,j,k,flg; while(cin>>n>>m&&n!=0&&m!=0) { flg=1; for(i=0;i<n;i++) for(j=0;j<m;j++) { cin>>p[i][j]; if(p[i][j]==0)flg=0; } if(flg) { cout<<"No"<<endl; goto RL; } for(i=1;i<m;i++) { if(p[0][i]==0&&p[0][i-1]==0) { cout<<"No"<<endl; goto RL; } } for(i=1;i<n;i++) { for(j=0;j<m;j++) { if(p[i][j]==0&&p[i-1][j]==0) { cout<<"No"<<endl; goto RL; } if(j!=0) { if(p[i][j]==0&&p[i][j-1]==0) { cout<<"No"<<endl; goto RL; } } } } cout<<"Yes"<<endl; continue; RL:continue; }}
- 总结
开始把题意理解错了,当成了两个未修剪的草坪也不能挨着。
代码:
#include<bits/stdc++.h>using namespace std;int main(){ int p[10][10]; int n,m,i,j,k; while(cin>>n>>m&&n!=0&&m!=0) { for(i=0;i<n;i++) for(j=0;j<m;j++) cin>>p[i][j]; for(i=1;i<m;i++) { if(p[0][i]==0&&p[0][i-1]==0||p[0][i]==1&&p[0][i-1]==1) { cout<<"No"<<endl; goto RL; } } for(i=1;i<n;i++) { for(j=0;j<m;j++) { if(p[i][j]==0&&p[i-1][j]==0||p[i][j]==1&&p[i-1][j]==1) { cout<<"No"<<endl; goto RL; } if(j!=0) { if(p[i][j]==0&&p[i][j-1]==0||p[i][j]==1&&p[i][j-1]==1) { cout<<"No"<<endl; goto RL; } } } } cout<<"Yes"<<endl; continue; RL:continue; }}
- 《ACM书中题目》 W
- acm书中题目 w-23
- 《acm程序设计》书中题目W-23
- 《ACM程序设计》书中题目--problem w
- 《ACM程序设计》书中题目 W
- 《ACM程序设计》书中题目 W
- 《ACM程序设计》书中题目 W
- 《ACM程序设计》书中题目W
- ACM程序设计书中题目--W(草地问题)
- 《ACM程序设计》书中题目W 第二十三题 漂亮的草坪
- 《ACM程序设计》书中题目W(让草坪变美丽吧,Tom!)
- ACM程序设计书上题目W
- ACM书中题目 K
- ACM书中题目 O
- ACM书中题目 G
- 《ACM书中题目》 J
- 《ACM 书中题目》 k
- 《ACM 书中题目》L
- 快速创建表的方法
- linux下tomcat 80端口被占用的解决方法
- IntelliJ Idea 常用快捷键列表
- Linux安装Redis服务
- md5和base64加密后
- 《ACM书中题目》 W
- SVN检出checkout时报错errror: Unable to connect to a repository at URL'svn://localhost'
- Oracle异常处理
- 从客户端(&)中检测到有潜在危险的Request.Path值解决方案
- 动作游戏Demo(二)主角的控制
- 【IDE-Visual Studio】Visual Studio对常用第三方库的包含,VC++目录修改
- Sybase PowerDesigner 16 的破解文件
- nexus-linux安装
- 算法-01 8种排序总有一款适合你