๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/BOJ

[BOJ] ๋ฐฑ์กฐ์˜ ํ˜ธ์ˆ˜(3197)

[๋ฐฑ์ค€(BOJ)] ๋ฐฑ์กฐ์˜ ํ˜ธ์ˆ˜(3197) C++

๋ฌธ์ œ : BOJ_3197๋ฒˆ ๋ฐฑ์กฐ์˜ ํ˜ธ์ˆ˜

๋ฌธ์ œ ์„ค๋ช…

์ด๋ถ„ํƒ์ƒ‰, bfs

๋ฌผ ๊ณต๊ฐ„๊ณผ ๋น™ํŒ ๊ณต๊ฐ„์ด ์žˆ๋Š” ์ง€๋„๊ฐ€ ์ฃผ์–ด์ง€๊ณ , L์— ๋‘ ๋ฐฑ์กฐ์˜ ์œ„์น˜๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋ฌผ ๊ณต๊ฐ„๊ณผ ๊ฐ€๋กœ, ์„ธ๋กœ๋กœ ์ ‘ํ•œ ๋น™ํŒ ๋ถ€๋ถ„์ด ํ•˜๋ฃจ์— ํ•œ ๋ฒˆ์”ฉ ๋…น๋Š”๋ฐ, ์ด๋•Œ ๋‘ ๋ฐฑ์กฐ๊ฐ€ ๋งŒ๋‚  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ์˜ ๋‚ ์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ํ•˜๋ฃจ์— ํ•œ ๋ฒˆ์”ฉ ๋…น๋Š” ๋น™ํŒ ๋ถ€๋ถ„๋„ ๊ณ ๋ คํ•ด์•ผ ํ•˜๊ณ , ๋˜ ๋น™ํŒ์ด ๋…น์•˜์„ ๋•Œ ๋งŒ๋‚  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ์˜ ๋‚ ๋„ ๊ตฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— bfs์™€ ์ด๋ถ„ ํƒ์ƒ‰ ๋‘ ๊ฐ€์ง€๊ฐ€ ์“ฐ์ด๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.


Solution

์ฒ˜์Œ์— bfs๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น™ํŒ์ด ๋ชจ๋‘ ๋…น๋Š” ์ตœ๋Œ€ ๋‚ ์„ ์ด๋ถ„ ํƒ์ƒ‰์˜ max ๊ฐ’์œผ๋กœ ์ง€์ •ํ•ด ์ฃผ๊ณ  ์ด๋ถ„ ํƒ์ƒ‰์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์˜ ์ค‘๊ฐ„๊ฐ’์œผ๋กœ bfs๋ฅผ ๋Œ๋ฆฌ๋ฉฐ ์ค‘๊ฐ„๊ฐ’์˜ ๋‚ ์ด ๋˜๊ธฐ ์ „์— ๋ฐฑ์กฐ๋ฅผ ๋งŒ๋‚œ๋‹ค๋ฉด ์ตœ์†Ÿ๊ฐ’์„ ์˜ฌ๋ ค์ฃผ๊ณ , ๋งŒ๋‚˜์ง€ ๋ชปํ•œ๋‹ค๋ฉด ์ตœ๋Œ“๊ฐ’์„ ๋‚ฎ์ถฐ์ฃผ๋ฉฐ ์ด๋ถ„ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ๊ตฌํ•œ ์ตœ์ ํ•ด๊ฐ€ ๋‘ ๋ฐฑ์กฐ๊ฐ€ ๋งŒ๋‚  ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ์˜ ๋‚ ์ž…๋‹ˆ๋‹ค.


Description

  • ๋‘ ๋ฐฑ์กฐ์˜ ์œ„์น˜๋ฅผ vector<pair<int, int>> v๋กœ ์„ ์–ธํ•˜์—ฌ ํ‘œํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ํŠน์ • ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธํ•œ ํšŸ์ˆ˜๋ฅผ vis ๋ฐฐ์—ด๋กœ ์ฒดํฌํ–ˆ๊ณ , ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด -1๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • vis์˜ -1 ์ดˆ๊ธฐํ™”๋Š” memset์„ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;
char map[1501][1501];
int vis[1501][1501];
int now_vis[1501][1501];
vector<pair<int, int>> v;
void bfs(void);
int dx[] = { 0,0,-1,1 };
int dy[] = { 1,-1,0,0 };
int r, c;
int le = 0;
int ri = 1500 * 2+1;
int res_max = 0;
queue<pair<int, int>> q;
int main(void) {
    memset(vis, -1, sizeof(vis));
    scanf("%d%d", &r, &c);
    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= c; j++) {
            scanf(" %c", &map[i][j]);
            if (map[i][j] == '.') {
                vis[i][j] = 0;
                q.push({ i,j });
                now_vis[i][j] = 1;
            }
            else if (map[i][j] == 'L') {
                v.push_back({ i,j });
                vis[i][j] = 0;
                q.push({ i,j });
                now_vis[i][j] = 1;
            }
        }
    }
    bfs();
    ri = res_max;
    while (le <= ri) {
        memset(now_vis, 0, sizeof(now_vis));
        now_vis[v[0].first][v[0].second] = 1;
        int swc = 0;
        int mi = (le + ri) / 2;
        queue<pair<int, int>> res;
        res.push({ v[0].first,v[0].second });
        while (!res.empty()) {
            int x = res.front().first;
            int y = res.front().second;
            res.pop();
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx<1 || ny<1 || nx>r || ny>c) continue;
                if (nx == v[1].first && ny == v[1].second) {
                    swc++;
                    break;
                }
                if (vis[nx][ny] > mi) continue;
                if (now_vis[nx][ny]) continue;
                now_vis[nx][ny] = 1;
                res.push({ nx,ny });
            }
        }
        if (swc) ri = mi - 1;
        else le = mi + 1;
    }
    printf("%d\n", le);
}
void bfs(void) {
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx<1 || ny<1 || nx>r || ny>c) continue;
            if (map[nx][ny] == 'L') continue;
            if (now_vis[nx][ny]) continue;
            if (vis[nx][ny] == -1) {
                vis[nx][ny] = vis[x][y] + 1;
                res_max = max(res_max, vis[nx][ny]);
                now_vis[nx][ny] = 1;
                q.push({ nx,ny });
            }
        }
    }
}

'Algorithm > BOJ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ] ์™ธํŒ์› ์ˆœํšŒ(2098)  (0) 2022.03.11
[BOJ] ์Šค๋„์ฟ (2580)  (0) 2022.03.10
[BOJ] ํ›„์œ„ ํ‘œ๊ธฐ์‹(1918)  (0) 2022.03.10
[BOJ] ํ–‰์šด์˜ ๋ฐ”ํ€ด(2840)  (0) 2022.03.10
[BOJ] ํšŒ์ „ํ•˜๋Š” ํ(1021)  (0) 2022.03.10