๐Ÿ™‡โ€โ™€๏ธ[Gold V] ๋ณด๋ฌผ์„ฌ - 2589

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2156 KB, ์‹œ๊ฐ„: 164 ms

๋ถ„๋ฅ˜

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰, ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

์ œ์ถœ ์ผ์ž

2024๋…„ 6์›” 9์ผ 13:31:35

๋ฌธ์ œ ์„ค๋ช…

๋ณด๋ฌผ์„ฌ ์ง€๋„๋ฅผ ๋ฐœ๊ฒฌํ•œ ํ›„ํฌ ์„ ์žฅ์€ ๋ณด๋ฌผ์„ ์ฐพ์•„๋‚˜์„ฐ๋‹ค. ๋ณด๋ฌผ์„ฌ ์ง€๋„๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋ฉฐ ์—ฌ๋Ÿฌ ์นธ์œผ๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ๋‹ค. ๊ฐ ์นธ์€ ์œก์ง€(L)๋‚˜ ๋ฐ”๋‹ค(W)๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค. ์ด ์ง€๋„์—์„œ ์ด๋™์€ ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด์›ƒํ•œ ์œก์ง€๋กœ๋งŒ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ•œ ์นธ ์ด๋™ํ•˜๋Š”๋ฐ ํ•œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ๋ณด๋ฌผ์€ ์„œ๋กœ ๊ฐ„์— ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋Š”๋ฐ ์žˆ์–ด ๊ฐ€์žฅ ๊ธด ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š” ์œก์ง€ ๋‘ ๊ณณ์— ๋‚˜๋‰˜์–ด ๋ฌปํ˜€์žˆ๋‹ค. ์œก์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ๊ณณ ์‚ฌ์ด๋ฅผ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋ ค๋ฉด ๊ฐ™์€ ๊ณณ์„ ๋‘ ๋ฒˆ ์ด์ƒ ์ง€๋‚˜๊ฐ€๊ฑฐ๋‚˜, ๋ฉ€๋ฆฌ ๋Œ์•„๊ฐ€์„œ๋Š” ์•ˆ ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์™€ ๊ฐ™์ด ์ง€๋„๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๋ฉด ๋ณด๋ฌผ์€ ์•„๋ž˜ ํ‘œ์‹œ๋œ ๋‘ ๊ณณ์— ๋ฌปํ˜€ ์žˆ๊ฒŒ ๋˜๊ณ , ์ด ๋‘˜ ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋Š” ์‹œ๊ฐ„์€ 8์‹œ๊ฐ„์ด ๋œ๋‹ค.

๋ณด๋ฌผ ์ง€๋„๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋ณด๋ฌผ์ด ๋ฌปํ˜€ ์žˆ๋Š” ๋‘ ๊ณณ ๊ฐ„์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ๋ณด๋ฌผ ์ง€๋„์˜ ์„ธ๋กœ์˜ ํฌ๊ธฐ์™€ ๊ฐ€๋กœ์˜ ํฌ๊ธฐ๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด L๊ณผ W๋กœ ํ‘œ์‹œ๋œ ๋ณด๋ฌผ ์ง€๋„๊ฐ€ ์•„๋ž˜์˜ ์˜ˆ์™€ ๊ฐ™์ด ์ฃผ์–ด์ง€๋ฉฐ, ๊ฐ ๋ฌธ์ž ์‚ฌ์ด์—๋Š” ๋นˆ ์นธ์ด ์—†๋‹ค. ๋ณด๋ฌผ ์ง€๋„์˜ ๊ฐ€๋กœ, ์„ธ๋กœ์˜ ํฌ๊ธฐ๋Š” ๊ฐ๊ฐ 50์ดํ•˜์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ณด๋ฌผ์ด ๋ฌปํ˜€ ์žˆ๋Š” ๋‘ ๊ณณ ์‚ฌ์ด๋ฅผ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋กœ ์ด๋™ํ•˜๋Š” ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์•ผํ•˜๋ฏ€๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ํ™œ์šฉํ•˜์˜€๋‹ค.

bfs๋ฅผ ์ด์ฐจ์› ๋ฐฐ์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

// BFS๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ์œก์ง€ ์นธ์—์„œ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
int bfs(vector<vector<char>>& grid, int x, int y) 
{
    int n = grid.size();
    int m = grid[0].size();
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    queue<pair<pair<int, int>, int>> q;
    q.push({ {x, y}, 0 });
    visited[x][y] = true;
    int maxDistance = 0;

    while (!q.empty()) 
    {
        int cx = q.front().first.first;
        int cy = q.front().first.second;
        int distance = q.front().second;
        q.pop();
        maxDistance = max(maxDistance, distance);

        for (int i = 0; i < 4; ++i) 
        {
            int nx = cx + dx[i];
            int ny = cy + dy[i];
            if (isValid(nx, ny, n, m) && !visited[nx][ny] && grid[nx][ny] == 'L') 
            {
                q.push({ {nx, ny}, distance + 1 });
                visited[nx][ny] = true;
            }
        }
    }

    return maxDistance;
}

dx, dy์™€ ์œ„์น˜๊ฐ€ grid์— ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜๋„ ๋งŒ๋“ค์—ˆ๋‹ค.

// ์ƒํ•˜์ขŒ์šฐ ์ด๋™์„ ์œ„ํ•œ ๋ฐฐ์—ด
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };

// ์ง€๋„์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
bool isValid(int x, int y, int n, int m) 
{
    return x >= 0 && x < n && y >= 0 && y < m;
}

์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” bfs๋ฅผ ์œก์ง€ ํ•œ์นธ๋‹น ๋Œ๋ ค์•ผํ•˜๊ณ  ๊ทธ ์ค‘ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•ด์•ผํ•œ๋‹ค.

// ๋ณด๋ฌผ์ด ๋ฌปํ˜€ ์žˆ๋Š” ๋‘ ๊ณณ ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
int shortestTreasureDistance(vector<vector<char>>& grid) 
{
    int n = grid.size();
    int m = grid[0].size();
    int maxDistance = 0;

    for (int i = 0; i < n; ++i) 
    {
        for (int j = 0; j < m; ++j) 
        {
            if (grid[i][j] == 'L') 
            {
                maxDistance = max(maxDistance, bfs(grid, i, j));
            }
        }
    }

    return maxDistance;
}

image

๐Ÿš€์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <climits>
#include <queue>

using namespace std;
// ์ƒํ•˜์ขŒ์šฐ ์ด๋™์„ ์œ„ํ•œ ๋ฐฐ์—ด
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };

// ์ง€๋„์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
bool isValid(int x, int y, int n, int m) 
{
    return x >= 0 && x < n && y >= 0 && y < m;
}

// BFS๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ์œก์ง€ ์นธ์—์„œ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
int bfs(vector<vector<char>>& grid, int x, int y) 
{
    int n = grid.size();
    int m = grid[0].size();
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    queue<pair<pair<int, int>, int>> q;
    q.push({ {x, y}, 0 });
    visited[x][y] = true;
    int maxDistance = 0;

    while (!q.empty()) 
    {
        int cx = q.front().first.first;
        int cy = q.front().first.second;
        int distance = q.front().second;
        q.pop();
        maxDistance = max(maxDistance, distance);

        for (int i = 0; i < 4; ++i) 
        {
            int nx = cx + dx[i];
            int ny = cy + dy[i];
            if (isValid(nx, ny, n, m) && !visited[nx][ny] && grid[nx][ny] == 'L') 
            {
                q.push({ {nx, ny}, distance + 1 });
                visited[nx][ny] = true;
            }
        }
    }

    return maxDistance;
}

// ๋ณด๋ฌผ์ด ๋ฌปํ˜€ ์žˆ๋Š” ๋‘ ๊ณณ ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
int shortestTreasureDistance(vector<vector<char>>& grid) 
{
    int n = grid.size();
    int m = grid[0].size();
    int maxDistance = 0;

    for (int i = 0; i < n; ++i) 
    {
        for (int j = 0; j < m; ++j) 
        {
            if (grid[i][j] == 'L') 
            {
                maxDistance = max(maxDistance, bfs(grid, i, j));
            }
        }
    }

    return maxDistance;
}

int main()
{
	FILE* stream;
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen_s(&stream, "input.txt", "rt", stdin);

    int n, m;
    cin >> n >> m;
    vector<vector<char>> board = vector<vector<char>>(n, vector<char>(m));
    // ์˜ˆ์‹œ ์ž…๋ ฅ
    for (int y = 0; y < n; ++y)
    {
        for (int x = 0; x < m; ++x)
        {
            cin >> board[y][x];
        }
    }

    // ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ถœ๋ ฅ
    cout << shortestTreasureDistance(board) << endl;  // ์ถœ๋ ฅ: 8

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: