forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2589.cpp
More file actions
79 lines (70 loc) · 1.49 KB
/
Copy path2589.cpp
File metadata and controls
79 lines (70 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
string board[50];
bool visited[50][50];
int N, M;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int getmaxd(int i, int j)
{
int res = 0;
visited[i][j] = true;
queue<pair<pair<int, int>, int>> bfsQueue;
bfsQueue.push({{i, j}, 0});
while (!bfsQueue.empty())
{
int x = bfsQueue.front().first.first;
int y = bfsQueue.front().first.second;
int d = bfsQueue.front().second;
res = max(res, d);
bfsQueue.pop();
for (int t = 0; t < 4; t++)
{
int tx = x + dx[t];
int ty = y + dy[t];
if (tx >= 0 && ty >= 0 && tx < N && ty < M && !visited[tx][ty] && board[tx][ty] == 'L')
{
visited[tx][ty] = true;
bfsQueue.push({{tx, ty}, d + 1});
}
}
}
return res;
}
void reservisited()
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
visited[i][j] = false;
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
int i, j;
int ans = 0;
for (i = 0; i < N; i++)
{
cin >> board[i];
}
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
if (board[i][j] == 'L')
{
reservisited();
ans = max(ans, getmaxd(i, j));
}
}
}
cout << ans << "\n";
return 0;
}