60分,求助,帮助者+1关注

B3625 迷宫寻路

pengqiushi @ 2022-09-22 22:20:14

代码如下:

#include<iostream>
using namespace std;
char a[1005][1005];
int bj[1005][1005];
int n,m;
int flag=0;
bool cmp(){
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            if(bj[i][j]==0&&a[i][j]=='.')
                return true;
    cout<<"NO";
    flag=1;
    return false;
}
int dfs(int x,int y) {
    bj[x][y]=1;
    if(x==n&&y==m){
        cout<<"Yes";
        flag=1;
        return 0;
    }
    if(cmp()==false)
        return 0;
    if(a[x+1][y]=='.'&&bj[x+1][y]==0)
        if(dfs(x+1,y)==0)
            return 0;
    if(a[x][y+1]=='.'&&bj[x][y+1]==0)
        if(dfs(x,y+1)==0)
            return 0;
    if(a[x-1][y]=='.'&&bj[x-1][y]==0)
        if(dfs(x-1,y)==0)
            return 0;
    if(a[x][y-1]=='.'&&bj[x][y-1]==0)
        if(dfs(x,y-1)==0)
            return 0;
    return 1;
}
int main() {
    cin>>n>>m;
    a[1][1]=1;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            cin>>a[i][j];
    if(a[1][1]=='#'||a[n][m]=='#'){
        printf("No");
        return 0;
    }
    dfs(1,1);
    if(flag==0)
        cout<<"NO";
    return 0;
}

评测记录


by Coffee_zzz @ 2022-09-22 22:28:51

你滴No写错了qwq


by Vanishing_Stars @ 2022-09-22 22:30:31

@pengqiushi 根据题意,是输出No,不是NO,代码其实没问题的


by pengqiushi @ 2022-09-22 22:32:57

@LiuJiaHe_HuangDou 没有啊


by pengqiushi @ 2022-09-22 22:33:26

@CODE_SUPERVISOR 哦,知道了


by Coffee_zzz @ 2022-09-22 22:33:58

qwq


by pengqiushi @ 2022-09-22 22:36:55

还是不行啊


by pengqiushi @ 2022-09-22 22:42:41

@LiuJiaHe_HuangDou @CODE_SUPERVISOR 哦,现在AC了,刚才没改全,谢谢。 你们均加1关注,辛苦了。


by Coffee_zzz @ 2022-09-22 23:37:13

其实感觉cmp和a[1][1]==‘#’那几行没什么用


|