본문 바로가기
알고리즘/탐색

너비 우선 탐색(BFS) 응용 문제 - 1

by sim0609 2023. 2. 20.

미로를 탐색하는 BFS 문제를 소개하고자 한다.

DFS보다 BFS가 적합한 이유는 BFS가 해당 깊이에서 갈 수 있는 노드 탐색을 마친 후 다음 깊이로 넘어가기 때문이다.

 

백준 - 2178: 미로 탐색하기

https://www.acmicpc.net/problem/2178

 

2178번: 미로 탐색

첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.

www.acmicpc.net

 

코드

#include <iostream>
#include <queue>
using namespace std;

// 상하좌우 탐색을 위한 배열
static int dx[] = { 0, 1, 0, -1 };
static int dy[] = { 1, 0, -1, 0 };

//static int graph[101][101];
//static bool visited[101][101] = { false };
static vector<vector<int>> graph;
static vector<vector<int>> visited;
static int n, m;
void BFS(int i, int j);

int main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> m;
    graph.resize(n, vector<int>(m, 0));
    visited.resize(n, vector<int>(m, 0));

    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < m; j++) {
            // 문자열을 숫자로 바꾸기
            graph[i][j] = s[j] - '0';
        }
    }

    BFS(0, 0);
    cout << graph[n - 1][m - 1];

}

void BFS(int i, int j) {
    queue<pair<int, int>> q;
    q.push(make_pair(i, j));

    while (!q.empty()) {
        // 큐 값을 저장할 배열(큐에서 노드 데이터 가져오기)
        int now[2];
        now[0] = q.front().first;
        now[1] = q.front().second;
        q.pop();
        visited[i][j] = true;

        for (int k = 0; k < 4; k++) { // 상하좌우 탐색
            int x = now[0] + dx[k];
            int y = now[1] + dy[k];

            if (x >= 0 && y >= 0 && x < n && y < m) { // 좌표 유효성 검사
                if (graph[x][y] != 0 && !visited[x][y]) { // 미방문 노드 검사
                    visited[x][y] = true;
                    graph[x][y] = graph[now[0]][now[1]] + 1; // 깊이 업데이트
                    q.push(make_pair(x, y));
                }
            }
        }
    }
}

 

나의 코드

#include <iostream>
#include <queue>
using namespace std;

int dx[] = { 0, 1, 0, -1 };
int dy[] = { 1, 0, -1, 0 };

static queue<pair<int, int>> q;
static int cnt = 0;
static int n, m;
static vector<vector<int>> graph;
static vector<vector<int>> visited;

void BFS(int i, int j);

int main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> m;

    graph.resize(n, vector<int>(m, 0));
    visited.resize(n, vector<int>(m, 0));

    string s;

    for (int i = 0; i < n; i++) {
        cin >> s;
        for (int j = 0; j < m; j++) {
            graph[i][j] = s[j] - '0';
        }
    }

    BFS(0, 0);
    cout << graph[n - 1][m - 1];
}


void BFS(int i, int j) {
 
    int now[2];
    visited[i][j] = 1;

    for (int k = 0; k < 4; k++) {

        int x = i + dx[k];
        int y = j + dy[k];

        if (x >= 0 && y >= 0 && x < n && y < m) {
            if (visited[x][y] == 0 && graph[x][y] != 0) {
                visited[x][y] = 1;
                graph[x][y] = graph[i][j] + 1;
                q.push(make_pair(x, y));
            }
        }
        else {
            continue;
        }
    }

    if (q.size() == 0) {
        return;
    }

    now[0] = q.front().first;
    now[1] = q.front().second;

    q.pop();
    BFS(now[0], now[1]);
}

'알고리즘 > 탐색' 카테고리의 다른 글

이진 탐색  (0) 2023.02.21
너비 우선 탐색(BFS) 응용 문제 - 2  (0) 2023.02.21
너비 우선 탐색 - BFS  (0) 2023.02.02
깊이 우선 탐색(DFS) 응용 문제 - 2  (0) 2023.01.31
깊이 우선 탐색(DFS) 응용 문제 - 1  (0) 2023.01.27