在本机上能得到正确答案,提交就WA,我怀疑是输入数据时候和我想的不一样
#include <stdio.h>
char check[1002][1002];
int nextx[4]={1,0,0,-1};
int nexty[4]={0,1,-1,0};
void dfs(int x,int y)
{
int tx,ty;
check[x][y]='.';
for(int i=0;i<4;i++)
{
tx=x+nextx[i];
ty=y+nexty[i];
if(check[tx][ty]=='#')
dfs(tx,ty);
}
return;
}
int check2(int i,int j)
{
int s=0;
if(check[i][j]=='#') s++;
if(check[i+1][j]=='#') s++;
if(check[i][j+1]=='#') s++;
if(check[i+1][j+1]=='#') s++;
if(s==3)
return 1;
else
return 0;
}
int main()
{
int count=0;
int x,y;
scanf("%d %d\n",&x,&y);
for(int i=1;i<=x;i++)
{
for(int j=1;j<=y;j++)
scanf("%c",&check[i][j]);
getchar();
}
for(int i=1;i<=x-1;i++)
for(int j=1;j<=y-1;j++)
{
if(check2(i,j))
{
printf("Bad placement.");
return 0;
}
}
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++){
if(check[i][j]=='#'){
count++;
dfs(i,j);
}
}
printf("There are %d ships.",count);
return 0;
}
可以看到有很多返回bad palcement 但我下在测试点2(就是样例数据),在本机输出是正确的. 然后换用C++,仅仅改成cin输入就AC了...