LoginSignup
0
1

More than 3 years have passed since last update.

C++ 幅優先探索(BFS)

Posted at

深さ優先探索ではスタック
幅優先探索ではキュー

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)

const int INF=1001001001;
char maze[100][100];//迷路を表す文字列の配列
int N,M; 
int sx,sy; //スタート位置
int gx,gy; //ゴール位置
int d[100][100]; //各点までの最短距離の配列
int dx[4]={1,0,-1,0}, dy[4]={0,1,0,-1}; //移動の4方向ベクトル

int bfs(){
    queue <P> que;
    //全ての点をINFで初期化
    REP(i,N) REP(j,M) d[i][j]=INF;

    //スタート地点をキューに入れ、その点の距離を0にする
    que.push(P(sx,sy));
    d[sx][sy]=0;

    //キューが空になるまでループ
    while(!Q.empty()){
        //キューの先頭を取り出す
        P p=que.front(); que.pop();
        //取り出してきた状態がゴールなら探索を止める
        if (p.first==gx && p.second==gy) break;
        //移動4方向をループ
        for (int i=0; i<4; i++){
            //移動した後の点を(nx,xy)とする
            int nx=p.first+dx[i],ny=p.second+dy[i];
            //移動が可能かの判定と既に訪れたことのあるかの判定
            if(0<=nx && nx<N && 0<=ny && ny<M && maze[nx][ny]!='#' && d[nx][ny]==INF){
                que.push(P(nx,ny));
                d[nx][ny]=d[p.first][p.second]+1;
            }
        }
    }
    return d[gx][gy];
}

void solve(){
    int res=bfs();
    printf("%d\n",res);
}

例えば...
幅優先探索問題
https://atcoder.jp/contests/abc151/tasks/abc151_d

#include<bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
typedef long long ll;
const int dx[4] = { 0, -1, 1, 0 };
const int dy[4] = { -1, 0, 0, 1 };
const int INF = 1e9 + 7;
using namespace std;
typedef pair<int,int> P;

int main() {
    int h, w; cin >> h >> w;
    char s[21][21];
    REP(i, h) REP(j, w) cin >> s[i][j];
    int ans = 0;
    int d[21][21];

    REP(i, h) {
        REP(j, w) {
            queue <P> Q;
            //全ての点をINFで初期化
            REP(k, 21) REP(l, 21) d[k][l] = INF; 
            //スタート地点をキューに入れ、その点の距離を0にする
            if (s[i][j] == '#')continue;
            Q.push({ i,j });
            d[i][j] = 0;

            //キューが空になるまでループ
            while (!Q.empty()) {
                //キューの先頭を取り出す
                int y = Q.front().first;
                int x = Q.front().second;
                Q.pop();
                for (int k = 0; k < 4; ++k) {
                    int ny = y + dy[k];//const int dy[4] = { -1, 0, 0, 1 };
                    int nx = x + dx[k];//const int dx[4] = { 0, -1, 1, 0 };
                    if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' && d[ny][nx] == INF) {
                        Q.push({ ny,nx });
                        d[ny][nx] = d[y][x] + 1;
                    }
                }
            }
            REP(k, h) {
                REP(l, w) {
                    if (d[k][l] == INF)continue;
                    if (s[k][l] == '#')continue;
                    ans = max(ans, d[k][l]);
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1