漂亮的草坪
时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交:121 测试通过:46
总提交:121 测试通过:46
描述
Tom的花园里有一个草坪,他把它分成N*M个方块。一开始,所有的广场上都长着草。他剪去一些方块上的草,并认为仅符合这两个条件,这块草坪才算漂亮:
(1)不是所有的方块都长着草。
(2)两块剪去草的方块不能相连。
如果两块共一条边,那么就算是连着的。现在问题来了:Tom的草坪漂亮吗?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tom的草坪
输入
输入包含多个测试案例!
每个测试案例包含两个整数N,M(1<=N,M<=10)开始,这两个数占一行,中间用空格隔开。接下来是Tom的草坪的描述。有N行,每行有M个整数,用空格隔开。0表示该方块被剪去草,1表示该方块长着草。
一行上如果是N=0且M=0,表示输入的结束,这行不要处理。
输出
一个测试用例输出一行。
如果这块草坪是漂亮的,输出“Yes”,否则输出“No”(不包括引号)。
样例输入
2 2
1 0
0 1
2 2
1 1
0 0
2 3
1 1 1
1 1 1
0 0
1 0
0 1
2 2
1 1
0 0
2 3
1 1 1
1 1 1
0 0
样例输出
Yes
No
No
No
No
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
const size_t MAX_SIZE_X = 10;
const size_t MAX_SIZE_Y = 10;
const short int MAX_DIRECTION = 4;
const short int DIRECTION_X[MAX_DIRECTION] = {0, 0, 1, -1};
const short int DIRECTION_Y[MAX_DIRECTION] = {-1, 1, 0, 0};
short int sward[MAX_SIZE_X][MAX_SIZE_Y];
size_t size_x, size_y;
void input(void){
for (size_t x(0); x != size_x; ++x){
for (size_t y(0); y != size_y; ++y){
scanf("%hd", &sward[x][y]);
}
}
}
bool is_linked(const size_t& x, const size_t& y){
size_t current_x, current_y;
for (short int direction(0); direction != MAX_DIRECTION; ++direction){
current_x = x + DIRECTION_X[direction];
current_y = y + DIRECTION_Y[direction];
if ((current_x >= 0 && current_x < size_x)
&& (current_y >= 0 && current_y < size_y)
&& sward[current_x][current_y] == 0){
return true;
}
}
return false;
}
bool is_beautiful(void){
bool have_grass = false;
for (size_t x(0); x != size_x; ++x){
for (size_t y(0); y != size_y; ++y){
if (sward[x][y] == 0){
have_grass = true;
if (is_linked(x, y)){
return false;
}
}
}
}
return have_grass;
}
int main(void){
while (cin >> size_x >> size_y, size_x != 0 && size_y != 0){
input();
cout << (is_beautiful() ? "Yes" : "No") << endl;
}
return 0;
}