蒟蒻求助

P1746 离开中山路

dxy2020 @ 2021-08-30 22:27:25

为什么机子上测测试数据和答案一样还WA

#include <bits/stdc++.h>
using namespace std;
char a[1005][1005];
int ans[1005][1005],mp[1005][1005],n;
bool vis[1005][1005];
struct Node{
    int x,y;
}begin,end,pos;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
queue<Node> q;
int bfs()
{
    vis[begin.x][begin.y]=1;
    q.push(begin);
    while (!q.empty())
    {
        pos=q.front();
        q.pop();
        for (int i=0;i<4;++i)
        {
            int xx=pos.x+dx[i];
            int yy=pos.y+dy[i];
            if (xx>=1&&xx<=n&&yy>=1&&yy<=n&&vis[xx][yy]==0&&mp[xx][yy]==0)
            {
                q.push({xx,yy});
                vis[xx][yy]=1;
                ans[xx][yy]=ans[pos.x][pos.y]+1;
            }
        }
        if (vis[end.x][end.y]) return ans[end.x][end.y];
    }
    return ans[end.x][end.y]?ans[end.x][end.y]:-1;
}
int main()
{
    scanf ("%d",&n);
    for (int i=1;i<=n;++i)
    {
        for (int j=1;j<=n;++j)
        {
            scanf ("%c",&a[i][j]);
            mp[i][j]=a[i][j]-'0';
        }
        getchar();
    }   
    scanf ("%d%d%d%d",&begin.x,&begin.y,&end.x,&end.y);
    printf ("%d\n",bfs());
    return 0;
}

by Cripple_Abyss @ 2021-08-30 22:43:18

@小小蒟蒻一只

读入那段改为

for (int i=1;i<=n;++i)
    {
        scanf("%s",a[i]+1);
        for (int j=1;j<=n;++j)
        {
            mp[i][j]=a[i][j]-'0';
        }
    }   

by Cripple_Abyss @ 2021-08-30 22:43:55

亲测AC


by dxy2020 @ 2021-08-30 22:57:41

@Cripple_Abyss 谢谢


by Cripple_Abyss @ 2021-08-30 23:13:56

@小小蒟蒻一只 字符串读入问题,有可能出现很多空格换行之类的


by Cripple_Abyss @ 2021-08-30 23:14:44

建议使用 scanf("%s",a); 来读入


by dxy2020 @ 2021-08-30 23:17:13

@Cripple_Abyss orz%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


|