์๊ฐ ์ ํ | ๋ฉ๋ชจ๋ฆฌ ์ ํ | ์ ๋ต ๋น์จ |
1 ์ด | 128 MB | 39.752% |
ํด๊ฒฐ๋ฐฉ์
๋ณด๋ฌผ์ ์์ ์ ๊ณผ ๋ ์ ์ ๋ชจ๋ฅด๋ ์ํ์์ ๊ฐ์ฅ ํฐ ์ก์ง์ ๋๊ณผ ๋์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ด์ค for๋ฌธ์ ๋๋ฆฌ๋ฉด์ ๋ชจ๋ ์์น์ ๋ํด BFS๋ฅผ ๋๋ ธ๋ค. BFS๋ฅผ ๋๋ฆด ๋ ๋ง๋ค ํ์ฌ๊ฐ ๊ฐ์ฅ ๊ธด ๊ฑฐ๋ฆฌ๋ผ๋ฉด ans๋ฅผ ๊ฐฑ์ ํ์๊ณ , ๋ง์ง๋ง๊น์ง BFSํ์์ ๋ชจ๋ ๋ง์ณค๋ค๋ฉด ans๋ฅผ ์ถ๋ ฅํ๋ค.
๋ฉ๋ชจ๋ฆฌ | ์๊ฐ |
2004 KB | 84 ms |
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
|
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define F(x,y,u,p) for(int x = 0 ; x<u; x++)for(int y = 0; y<p; y++)
typedef pair<int, int> p;
int dx[] = { 0,1,0,-1 }, dy[] = { 1,0,-1,0 };
int n, m, chk[55][55], ans;
char arr[55][55];
void bfs(int x, int y) {
queue<p> q;
q.push({ x, y });
chk[x][y] = 0;
while (!q.empty()) {
int r = q.front().first, c = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nr = r + dx[i], nc = c + dy[i];
if (nr < 0 || nr >= n || nc < 0 || nc >= m || chk[nr][nc] >= 0 || arr[nr][nc] == 'W') continue;
chk[nr][nc] = chk[r][c] + 1;
if (ans < chk[nr][nc]) ans = chk[nr][nc];
q.push({ nr, nc });
}
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%s", arr[i]);
}
F(i, j, n, m) {
if(arr[i][j] == 'L') bfs(i, j);
memset(chk, -1, sizeof(chk));
}
printf("%d", ans);
return 0;
}
|
cs |
'๐ฅ PS(Problem Solving) ๐ฅ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] #1260 DFS์ BFS (0) | 2019.10.13 |
---|---|
[BOJ] #11403 ๊ฒฝ๋ก ์ฐพ๊ธฐ (0) | 2019.10.13 |
[BOJ] #3187 ์์น๊ธฐ ๊ฟ (0) | 2019.10.13 |
[BOJ] #7562 ๋์ดํธ์ ์ด๋ (0) | 2019.10.13 |
[BOJ] #16236 ์๊ธฐ ์์ด (0) | 2019.10.11 |
๋๊ธ