浙大在线测试系统一题 为什么Wrong Answer
/*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
*/
下面是我编的 自己运行正确,但到系统上说Wrong Answer 那位有兴趣或作过的朋友解释下 谢谢
#include <stdio.h>
#include <string.h>
typedef struct array{
int a,b;
char s[3];
};
void main()
{
array k[10];
int i=1,c[10][10],r=1,p=1,m,n;
do{
scanf( "%d%d ",&k[i].a,&k[i].b);
for(m=0;m <k[i].a;m++)
{
for(n=0;n <k[i].b;n++)
scanf( "%d ",&c[m][n]);
}
for(m=0;m <k[i].a;m++)
{
for(n=0;n <k[i].b;n++)
{
if(c[m][n]==0)
{
p=0;
if(c[m][n]==c[m][n-1] || c[m][n]==c[m-1][n])
{
strcpy(k[i].s, "No ");
r=0;
break;
}
}
}
}
if(p!=1 && r==1)
{
strcpy(k[i].s, "Yes ");
}
if(p==1)
{
strcpy(k[i].s, "No ");
}
r=1;p=1;
i++;
}while(k[i-1].a!=0 && k[i-1].b!=0);
for(m=1;m <i-1;m++)
puts(k[m].s);
}
[解决办法]
难道acm有过中文的?
[解决办法]
另外,对ayw215(松花鼠)说的越界,我也想过,感觉定义成一维数组好判断些,只比较和前后两个元素(首尾两个元素特殊对待)就可以了,不用象二维数组那样在逻辑上有很多越界的可能。
[解决办法]
原来这边也有个帖。
ayw215(松花鼠)说得对,错误就是越界的问题。把
if(c[m][n]==c[m][n-1] || c[m][n]==c[m-1][n])
改成
if (n < k[i].a - 1 && c[m][n]==c[m][n-1]
|| m < k[i].b - 1 && c[m][n]==c[m-1][n])
试试