문제 : https://www.acmicpc.net/problem/2589
[ 알고리즘풀이 ]
Input으로 주어지는 Map이 크지 않으므로, 모든 육지에서 BFS 탐색을 진행해 해당 육지에서 가장 먼 육지까지의 거리를 구하고 답을 갱신해주면 된다.
#include<iostream>
#include<queue>
using namespace std;
int N, M, ans = -1, dx[4] = { -1,0,1,0 }, dy[4] = { 0, 1, 0, -1 };
char map[51][51];
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 0; i < N; i++)
cin >> map[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] == 'L') {
bool check[51][51] = {};
queue<int> x, y, l;
x.push(i), y.push(j), l.push(0);
check[i][j] = true;
while (!x.empty()) {
int curX = x.front(), curY = y.front(), curL = l.front();
x.pop(), y.pop(), l.pop();
ans = max(ans, curL);
for (int k = 0; k < 4; k++) {
int nextX = curX + dx[k];
int nextY = curY + dy[k];
if (0 <= nextX && nextX < N && 0 <= nextY && nextY < M) {
if(map[nextX][nextY] == 'L' && check[nextX][nextY] == false){
x.push(nextX), y.push(nextY), l.push(curL + 1);
check[nextX][nextY] = true;
}
}
}
}
}
}
}
cout << ans;
return 0;
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 17825 : 주사위 윷놀이 - travelbeeee (0) | 2020.03.06 |
---|---|
[BOJ] 1806 : Subsequence - travelbeeee (0) | 2020.03.06 |
[BOJ] 16236 : 아기 상어 - travelbeeee (0) | 2020.03.05 |
[BOJ] 13460 : 구슬 탈출 2 (코드개선) - travelbeeee (0) | 2020.03.04 |
[BOJ] 13460 : 구슬 탈출 2 - travelbeeee (0) | 2020.03.04 |