60分bfs求调

B3625 迷宫寻路

tyy6034 @ 2023-07-28 18:36:30

#include<bits/stdc++.h>
using namespace std;
int n,m,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char a[105][105];
bool vis[105][105];
struct node{
    int x,y;
};
queue<node> q;
void bfs(){
    q.push(node{1,1});
    vis[1][1]=1;
    while(!q.empty())
    {
        int x=q.front().x,y=q.front().y;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int x_new=x+dx[i],y_new=y+dy[i];
            if(x_new<1||x_new>n||y_new<1||y_new>m)continue;
            if(vis[x_new][y_new]==true)continue;
            if(x_new==n&&y_new==m){
                cout<<"Yes"<<endl;
                return ;
            }
            q.push(node{x_new, y_new});
            vis[x_new][y_new] = true;
        }
    }
    cout<<"No"<<endl;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    bfs();
    return 0;
}

by Sunhaotian2011 @ 2023-07-28 19:24:16

/*

*/
#include<bits/stdc++.h>
#define itn int
#define el endl//卵用没有,个人习惯 
using namespace std;
int n,m;
char arr[110][110];//数组多开10个是好习惯 
struct node{
    int x,y;
}a;
queue<node> q;
bool flag=0;
void bfs(){
    while(!q.empty()){
        a=q.front();
        if(a.x==n&&a.y==m){
            flag=1;
            cout<<"Yes";
            return ;
        }
        if(a.x>0&&a.x<=n&&a.y>0&&a.y<=m&&arr[a.x][a.y]=='.'){
            arr[a.x][a.y]='#';//过河拆桥 
            a.x++;
            q.push(a);
            a.x-=2;
            q.push(a);
            a.x++;
            a.y++;
            q.push(a);
            a.y-=2;
            q.push(a);
        }
        q.pop();
    }
}
int main(){
    cin>>n>>m;
    a.x=1,a.y=1;
    q.push(a);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>arr[i][j];
        }
    }
    bfs();
    if(flag==0){
        cout<<"No";
    }
    return 0;
}

可以借鉴一下


by Sunhaotian2011 @ 2023-07-28 19:26:08

@tyy6034 你没有输出No


by tyy6034 @ 2023-07-28 19:37:16

@Sunhaotian2011 过了,谢谢

你好像认识你


by WindowsExplorer @ 2023-08-11 12:11:49

#include<bits/stdc++.h>
using namespace std;

//帮讨论区改代码测试
int n,m,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char a[105][105];
bool vis[105][105];
struct node{
    int x,y;
};
queue<node> q;
void bfs(){
    q.push(node{1,1});
    vis[1][1]=1;
    while(!q.empty())
    {
        int x=q.front().x,y=q.front().y;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int x_new=x+dx[i],y_new=y+dy[i];
            if(x_new<1||x_new>n||y_new<1||y_new>m){
                vis[x_new][y_new] = true;
                continue;
            }
            if(vis[x_new][y_new]==true)continue;
            vis[x_new][y_new] = true;
            //要加墙的判断
            if (a[x_new][y_new] == '#'){
                continue;
            }
            if(x_new==n&&y_new==m){
                cout<<"Yes"<<endl;
                return ;
            }
            q.push(node{x_new, y_new});
        }
    }
    cout<<"No"<<endl;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    bfs();
    return 0;
}

|