#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int MAX = 51;
int n,m;
char way[MAX][MAX];
char shortestway[MAX][MAX];
typedef struct {
int x, y;
}Move;
Move moves[4] = { {1,0},{-1,0},{0,1},{0,-1} };
int bfs(int i,int j) {
int longestway=0;
memset(shortestway, 0, sizeof(shortestway));
queue<pair<int,int>> q;
q.push(make_pair(i, j));
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int k = 0; k < 4; k++) {
int ma = x + moves[k].x;
int mb = y + moves[k].y;
if (ma >= 0 && ma < n && mb >= 0 && mb < m) {
if (way[ma][mb] == 'L' && shortestway[ma][mb] == 0) {
q.push(make_pair(ma, mb));
shortestway[ma][mb] = shortestway[x][y] + 1;
longestway = shortestway[ma][mb];
}
}
}
}
return longestway;
}
int main(void) {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> way[i];
}
int ans = 0,waycount;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (way[i][j] == 'L') {
waycount = bfs(i, j);
if (ans < waycount)
ans = waycount;
}
}
}
cout << ans;
}
'백준 알고리즘 > BFS' 카테고리의 다른 글
백준 1697번 C++ (0) | 2019.02.22 |
---|---|
백준 2178번 C++ (0) | 2019.02.19 |
백준 10026번 C++ (0) | 2019.01.13 |
백준 1389번 C++ (0) | 2019.01.13 |
백준 2644번 C++ (0) | 2019.01.12 |