AC4个点

B3625 迷宫寻路

jiangbaolin @ 2024-05-29 17:38:06


#include<bits/stdc++.h>
using namespace std;
long long a[10001][10001],k=1,n,m,s,plasx[4]={0,1,0,-1},plasy[4]={1,0,-1,0};
bool tf=false;
char c;
void go(int x,int y){
    if(tf){
        return;
    }
    if(x==n&&y==m){
        tf=true;
        return;
    }
    else{
        for(int i=0;i<=3;i++){
            if(a[x+plasx[i]][y+plasy[i]]==1){
                a[x][y]=0;
                go(x+plasx[i],y+plasy[i]);
                a[x][y]=1;
            }
        }
    }
}
int main(){
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin >> c;
            if(c=='.'){
                a[i][j]=1;
            }
            else{
                a[i][j]=0;
            }
        }
    }
    go(1,1);
    if(tf==true){
        cout << "Yes";
    }
    else{
        cout << "No";
    }
    return 0;
}

by CaoSheng_zzz @ 2024-05-29 18:39:32

@jiangbaolin 你是不是 TLE 了


by CaoSheng_zzz @ 2024-05-29 18:43:40

新加一个 vis 的二位数组统计这个点是否走过,如果走过的话那么就不去搜索这一个点,我直接将你的代码改了。可以 AC,我试过了 QWQ。

Code:

#include<bits/stdc++.h>
using namespace std;
long long a[10001][10001],k=1,n,m,s,plasx[4]={0,1,0,-1},plasy[4]={1,0,-1,0},vis[10001][10001];
bool tf=false;
char c;
void go(int x,int y){
    vis[x][y] = 1;
    if(tf){
        return;
    }
    if(x==n&&y==m){
        tf=true;
        return;
    }
    else{
        for(int i=0;i<=3;i++){
            if(vis[x+plasx[i]][y+plasy[i]] == 1) continue;
            if(a[x+plasx[i]][y+plasy[i]]==1){
                a[x][y]=0;
                go(x+plasx[i],y+plasy[i]);
                a[x][y]=1;
            }
        }
    }
}
int main(){
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin >> c;
            if(c=='.'){
                a[i][j]=1;
            }
            else{
                a[i][j]=0;
            }
        }
    }
    go(1,1);
    if(tf==true){
        cout << "Yes";
    }
    else{
        cout << "No";
    }
    return 0;
}

by jiangbaolin @ 2024-05-30 16:45:47

谢谢


by jiangbaolin @ 2024-05-30 16:46:50

已关注


|