求助一个C语言输入的问题
  • 板块P1331 海战
  • 楼主FOOLSIX
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/12/13 15:26
  • 上次更新2023/10/24 07:48:07
查看原帖
求助一个C语言输入的问题
821474
FOOLSIX楼主2022/12/13 15:26

在本机上能得到正确答案,提交就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了...

2022/12/13 15:26
加载中...