听取WA声一片!

B3625 迷宫寻路

poordoor @ 2024-11-03 23:39:19

#include<bits/stdc++.h>
using namespace std;
bool a[102][102],w=false;
int sx,sy,fx,fy;
    int n,m;
int z[4]={
0,1,0,-1
},
c[4]={
1,0,-1,0
};

void fun(int x,int y){
    a[x][y]=false;
    if(x==n&&y==m){
        w=true;
        cout<<"YES";
        return;
    }
    int tx,ty;
    for(int i=0;i<=3;i++){
        tx=x+z[i];
        ty=y+c[i];
    if(tx<=n&&tx>=1&&ty<=m&&ty>=1&&a[tx][ty])fun(tx,ty);
}
}
int main(){
    char k;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>k;
        if(k=='#')a[i][j]=false;
        else if(k=='.')a[i][j]=true;
        }
    }
    fun(1,1);
    if(w==false)cout<<"NO";
    return 0;
} 

听取WA声一片。


by poordoor @ 2024-11-03 23:39:46

是用DFS做的。


by poordoor @ 2024-11-03 23:57:28

稍加改动

#include<bits/stdc++.h>
using namespace std;
bool a[102][102],w=false;
int sx,sy,fx,fy;
    int n,m;
int z[4]={1,0,-1,0},c[4]={0,1,0,-1};

void fun(int x,int y){
    a[x][y]=false;
    if(w==true)return;
    cout<<x<<" "<<y<<endl;
    if(x==n&&y==m){
        w=true;
        cout<<"YES";
        return;
    }
    int tx,ty;
    for(int i=0;i<=3;i++){
        tx=x+z[i];
        ty=y+c[i];
    if(tx<=n&&tx>=1&&ty<=m&&ty>=1&&a[tx][ty]==true)fun(tx,ty);
}
}
int main(){
    char k;
    a[1][1]=false;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>k;
        if(k=='#')a[i][j]=false;
        else if(k=='.')a[i][j]=true;
        }
    }
    fun(1,1);
    if(w==false)cout<<"NO";
    return 0;
}/*
6 10
.####.....
.####.###.
.####...#.
.######.#.
........#.
#########.
*/

by poordoor @ 2024-11-04 00:06:20

解决了,输出有问题


by poordoor @ 2024-11-04 00:09:01

@poordoor 解决了个集贸,还是WA。


by poordoor @ 2024-11-04 00:16:22

这下是真解决了,提交时忘了删除测试的x,y。


|