1일1알

백준 2146번 다리 만들기 C++ 본문

알고리즘

백준 2146번 다리 만들기 C++

영춘권의달인 2022. 9. 5. 19:27

출처 : https://www.acmicpc.net/problem/2146

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

#include <bitset>

using namespace std;
using int64 = long long;

int n;
int id = 1;

int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };

struct Info {
    int row;
    int col;
    int id;
    int moveCnt;
};

queue<Info> q;

vector<vector<pair<int,int>>> board;
vector<vector<bool>> found;

void Bfs(int row, int col) {

    if (found[row][col]) return;
    if (board[row][col].first == 0) return;

    queue<pair<int, int>> _q;
    _q.push({ row,col });
    board[row][col].first = id;
    found[row][col] = true;

    while (!_q.empty()) {

        auto curr = _q.front();
        _q.pop();

        for (int i = 0; i < 4; i++) {
            int nextRow = curr.first + dRow[i];
            int nextCol = curr.second + dCol[i];
            if (nextRow < 0 || nextRow >= n) continue;
            if (nextCol < 0 || nextCol >= n) continue;
            if (found[nextRow][nextCol]) continue;
            if (board[nextRow][nextCol].first == 0) {
                board[nextRow][nextCol] = { id,1 };
                q.push({ nextRow,nextCol,id,1 });
            }
            else {
                board[nextRow][nextCol].first = id;
                _q.push({ nextRow,nextCol });
            }
            found[nextRow][nextCol] = true;
        }
    }

    id++;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

    cin >> n;
    board = vector<vector<pair<int,int>>>(n, vector<pair<int,int>>(n));
    found = vector<vector<bool>>(n, vector<bool>(n, false));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int input;
            cin >> input;
            board[i][j] = { input,0 };
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            Bfs(i, j);
        }
    }

    int ans = 10000;
    while (!q.empty()) {
        auto curr = q.front();
        q.pop();

        for (int i = 0; i < 4; i++) {
            int nextRow = curr.row + dRow[i];
            int nextCol = curr.col + dCol[i];
            if (nextRow < 0 || nextRow >= n) continue;
            if (nextCol < 0 || nextCol >= n) continue;
            if (board[nextRow][nextCol].first == curr.id) continue;
            if (board[nextRow][nextCol].first == 0) {
                board[nextRow][nextCol] = { curr.id,curr.moveCnt+1 };
                q.push({ nextRow,nextCol,curr.id,curr.moveCnt + 1 });
            }
            else {
                ans = min(ans, curr.moveCnt + board[nextRow][nextCol].second);
            }
        }
    }

    cout << ans;
}

'알고리즘' 카테고리의 다른 글

백준 2607번 비슷한 단어 C++  (0) 2022.09.07
백준 20291번 파일 정리 C++  (0) 2022.09.06
백준 1937번 욕심쟁이 판다 C++  (0) 2022.09.04
백준 9466번 텀 프로젝트 C++  (0) 2022.09.03
백준 1520번 내리막 길 C++  (0) 2022.09.02