萌新20分求调代码dfs
查看原帖
萌新20分求调代码dfs
1016963
paidasen楼主2023/12/2 10:23
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
int n, m;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char mapn[150][150];
int book[150][150];
int step[10000][10000];
int xx,yy;
struct node{
    int x, y;
};
void bfs(int x, int y){
    queue<node>q;
    node start = {x,y};
    q.push(start);
    book[x][y]=1;
    while(!q.empty()){
        node now=q.front();
        q.pop();
        if(now.x==n&&now.y==m){
            cout << n << " " << m;
            exit(0);
        }
        cout << now.x << " " << now.y << endl;
        for(int i = 0; i <= 3; i++){
            int nx=now.x+dir[i][0];
            int ny=now.y+dir[i][1];
            if(mapn[nx][ny]=='.'&&book[nx][ny]==0){
                book[nx][ny]=1;
                node newp={nx,ny};
                q.push(newp);
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> mapn[i][j];
        }
    }
    bfs(1,1);
    return 0;
}

2023/12/2 10:23
加载中...