求助!

P1746 离开中山路

Crystron_Halqifibrax @ 2018-08-17 08:21:36

WA0分,求大佬帮忙

#include<cstdio>
#include<cstring>
using namespace std;
int x1,x2,y1,y2,a[1001][1001],vis[1001][1001],min=9999,n;
char c[1001][1001];
void dfs(int x,int y,int sum)
{
    if(x>n||y>n||x<1||y<1)
        return;
    if(c[x][y]==1)
        return;
    if(vis[x][y]==1)
        return;
    if(sum>=a[x][y])
        return;
    if(x==x2&&y==y2)
    {
        if(sum<min)
        {
            min=sum;
            return;
        }
    }           
    a[x][y]=sum;
    vis[x][y]=1;
    dfs(x+1,y,sum+1);
    dfs(x-1,y,sum+1);
    dfs(x,y+1,sum+1);
    dfs(x,y-1,sum+1);
    vis[x][y]=0;
}
int main()
{
    memset(a,0x7f,sizeof(a));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%c",&c[i][j]);
        }
        int s;
        scanf("%c",&s);
    }   
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    dfs(x1,y1,0);
    printf("%d",min);
    return 0;
}

by Planet6174 @ 2018-08-17 08:25:05

scanf("%c",&s);
可能是这个问题?


by Crystron_Halqifibrax @ 2018-08-17 08:29:34

@Planet6174 这是为了读换行


by Crystron_Halqifibrax @ 2018-08-17 08:31:00

好吧,char写成了int,不过还是不对呀


by Planet6174 @ 2018-08-17 08:33:55

@飞翔的OI梦
唔,明白了


by Planet6174 @ 2018-08-17 08:34:11

这题怎么能用 DFS 呢
应该是 BFS


by Planet6174 @ 2018-08-17 08:34:26

重写吧


by Crystron_Halqifibrax @ 2018-08-17 08:35:53

改了改变成1A1W8T,但是dfs和bfs不是通用的吗,不懂啊


by Crystron_Halqifibrax @ 2018-08-17 08:36:04

@Planet6174


|