Tags
- ๊ตฌํ
- ์ ์๋ก
- greedy
- sort
- ๊ต์ฌ
- dfs
- ๋ฌธ์์ด
- ๋๋น ์ฐ์ ํ์
- SpringBoot
- Java
- Brute Force Algorithm
- BOJ
- queue
- ๋ฐฑํธ๋ํน
- ์ ๋ ฌ
- ๊ทธ๋ํ ํ์
- Study
- ์ํ
- ๊ทธ๋ํ ์ด๋ก
- Dynamic Programming
- ์๋ฃ๊ตฌ์กฐ
- Python
- CodingTest
- BFS
- DP
- ์๋ฎฌ๋ ์ด์
- ๊น์ด ์ฐ์ ํ์
- PGM
- LV2
- stack
Archives
๊ธฐ๋ก๋ฐฉ
BOJ_2589 : ๋ณด๋ฌผ์ฌ ๋ณธ๋ฌธ
2589๋ฒ: ๋ณด๋ฌผ์ฌ
์ฒซ์งธ ์ค์๋ ๋ณด๋ฌผ ์ง๋์ ์ธ๋ก์ ํฌ๊ธฐ์ ๊ฐ๋ก์ ํฌ๊ธฐ๊ฐ ๋น์นธ์ ์ฌ์ด์ ๋๊ณ ์ฃผ์ด์ง๋ค. ์ด์ด L๊ณผ W๋ก ํ์๋ ๋ณด๋ฌผ ์ง๋๊ฐ ์๋์ ์์ ๊ฐ์ด ์ฃผ์ด์ง๋ฉฐ, ๊ฐ ๋ฌธ์ ์ฌ์ด์๋ ๋น ์นธ์ด ์๋ค. ๋ณด๋ฌผ ์ง๋์
www.acmicpc.net
๐ธ ๋ฌธ์ ๋ถ์ ๐ธ
- ์ก์ง๋ ์ํ์ข์ฐ๋ก ๊ฑด๋ ์ ์๋ ๋ ์ด๋ค.
- ๋ ๋ณด๋ฌผ์ ํ ์ก์ง์ ์์ผ๋ฉฐ, ์ต๋จ ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ฅ ๊ธด ๋ ์์น์ ์๋ค.
- bfs๋ก ๊ฐ์ฅ ๋ฉ๋ฆฌ ํผ์ง๊ณณ์ ์ฐพ์ผ๋ฉด, ์ต๋จ ๊ฑฐ๋ฆฌ ์ค ๊ฐ์ฅ ๊ธด ๊ฑฐ๋ฆฌ์ด๋ค.
- ๋ ๋ณด๋ฌผ ์ฌ์ด๋ฅผ ์ด๋ํ๋ ์๊ฐ์ ์ถ๋ ฅํ๋ค.
๐ธ ์ฝ๋ ๐ธ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int max = 0;
static int[] di = { -1, 0, 1, 0 };
static int[] dj = { 0, 1, 0, -1 };
static int r, c;
static char[][] map;
public static class Index {
int i, j;
public Index(int i, int j) {
this.i = i;
this.j = j;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
map = new char[r][c];
for (int i = 0; i < r; i++) {
map[i] = br.readLine().toCharArray();
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (map[i][j] == 'L') {
max = Math.max(max, bfs(i, j));
}
}
}
System.out.println(max);
}
private static int bfs(int i, int j) {
boolean[][] visited = new boolean[r][c];
Queue<Index> queue = new LinkedList<>();
queue.add(new Index(i, j));
visited[i][j] = true;
int cnt = -1;
while (!queue.isEmpty()) {
int num = queue.size();
cnt++;
for (int t = 0; t < num; t++) {
Index now = queue.poll();
for (int d = 0; d < 4; d++) {
int nexti = now.i + di[d];
int nextj = now.j + dj[d];
if (nexti >= 0 && nexti < r && nextj >= 0 && nextj < c && map[nexti][nextj] == 'L'
&& !visited[nexti][nextj]) {
queue.add(new Index(nexti, nextj));
visited[nexti][nextj] = true;
}}
}
}
return cnt;
}
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- ๊ฐ ์ก์ง๋ณ๋ก BFS๋ก ์ต๋จ ๊ฑฐ๋ฆฌ์ ์ต๋๊ฐ์ ๊ตฌํ๋ค.
- ๊ทธ ์ค ์ต๋๊ฐ์ ์ถ๋ ฅํ๋ค.
๐ธ end ๐ธ
- ์ต๋จ๊ฑฐ๋ฆฌ๋ผ๋ ์ ์์ bfs๊ฐ ๊ฐ์ฅ ๋จผ์ ๋ ์ฌ๋๊ณ , ๊ทธ ์ค ์ต๋๊ฐ์ด๋ผํด์ ์ ๋ฐ์ดํธ๋ง ํ๋ฉด ๋ ๊ฒ๊ฐ์๋ค.
- ๊ฐ๋ก ์ธ๋ก๊ฐ 50 ์ดํ์ด๋ฏ๋ก bfs, dfs ๋ชจ๋ ๊ฐ๋ฅํ๋ค๊ณ ์๊ฐํ๋ค.
728x90
'CodingTest > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ_2110 : ๊ณต์ ๊ธฐ ์ค์น (0) | 2023.04.18 |
---|---|
BOJ_1806 : ๋ถ๋ถํฉ (0) | 2023.04.18 |
BOJ_11279 : ์ต๋ ํ (0) | 2023.04.18 |
Lv.1 : ํฌ๊ธฐ๊ฐ ์์ ๋ถ๋ถ ๋ฌธ์์ด (0) | 2023.04.18 |
Lv.1 : ๊ฐ์ฅ ๊ฐ๊น์ด ๊ฐ์ ๊ธ์ (0) | 2023.04.18 |