Tags
- ๊น์ด ์ฐ์ ํ์
- Java
- ์ํ
- ๊ต์ฌ
- CodingTest
- LV2
- ์๋ฃ๊ตฌ์กฐ
- ๋ฐฑํธ๋ํน
- PGM
- Study
- dfs
- DP
- ๊ตฌํ
- ๊ทธ๋ํ ์ด๋ก
- BFS
- stack
- Dynamic Programming
- ์ ๋ ฌ
- BOJ
- greedy
- Python
- SpringBoot
- Brute Force Algorithm
- ๊ทธ๋ํ ํ์
- sort
- ์๋ฎฌ๋ ์ด์
- queue
- ์ ์๋ก
- ๋๋น ์ฐ์ ํ์
- ๋ฌธ์์ด
Archives
๊ธฐ๋ก๋ฐฉ
BOJ_17141 : ์ฐ๊ตฌ์ 2 ๋ณธ๋ฌธ
๐ธ ๋ฌธ์ ๋ถ์ ๐ธ
- N x N ์ฐ๊ตฌ์์์ M๊ฐ์ ๋ฐ์ด๋ฌ์ค๊ฐ ๋ชจ๋ ํผ์ง๋ ์ต์ ์๊ฐ์ ์ถ๋ ฅํ๋ค.
- ๋ฐ์ด๋ฌ์ค๋ฅผ ๋์ ์ ์๋ ๊ณณ : 2 , ๋น ๊ณต๊ฐ : 0, ๋ฒฝ : 1
- 2์ ๊ฐ์๋ 10 ์ดํ์ ์์ฐ์์ด๋ค.
- ๋ชจ๋ ๋์ ์ ์๋ ๊ฒฝ์ฐ๋ -1์ ์ถ๋ ฅํ๋ค.
- ๋ฐ์ด๋ฌ์ค๋ฅผ ๋์ ์ ์๋ 2๊ฐ ์ ํ ์๋ฆฌ๋ค ์ค M๊ฐ๋ฅผ ๋ฝ์์ผ ํ๋ฏ๋ก ์กฐํฉ์ด๋ค.
- ๋ฐ์ด๋ฌ์ค๊ฐ ํผ์ง๋ ๊ฒ์ ๊ตฌํํ๊ธฐ ์ํด์ BFS๋ฅผ ์ฌ์ฉํ๋ค.
๐ธ ์ฝ๋ ๐ธ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
private static class Point {
int r, c;
public Point(int r, int c) {
this.r = r;
this.c = c;
}
}
private static int N, M, ans, selected[];
private static int[][] map, temp;
private static boolean[][] visited;
private static ArrayList<Point> virus;
private static int[] dx = {-1, 0, 1, 0};
private static int[] dy = {0, 1, 0, -1};
public static void main(String[] args) throws IOException {
// Input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new int[N][N];
temp = new int[N][N];
selected = new int[M];
virus = new ArrayList<>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
int num = Integer.parseInt(st.nextToken());
if (num == 1) map[i][j] = -1;
else if (num == 2) {
virus.add(new Point(i, j));
map[i][j] = 0;
} else {
map[i][j] = 0;
}
}
}
// Combination
ans = Integer.MAX_VALUE;
comb(0, 0);
// Output
System.out.println(ans == Integer.MAX_VALUE ? -1 : ans);
}
private static void comb(int start, int cnt) {
if (cnt == M) {
deepCopy();
visited = new boolean[N][N];
Queue<Point> que = new LinkedList<>();
for (int i = 0; i < M; i++) {
Point p = virus.get(selected[i]);
que.add(p);
visited[p.r][p.c] = true;
}
bfs(que, visited);
return;
}
for (int i = start; i <= virus.size() - (M - cnt); i++) {
selected[cnt] = i;
comb(i + 1, cnt + 1);
}
}
private static void bfs(Queue<Point> que, boolean[][] visited) {
int time = 0;
while (!que.isEmpty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
Point point = que.poll();
temp[point.r][point.c] = time;
for (int j = 0; j < dx.length; j++) {
int r = point.r + dx[j];
int c = point.c + dy[j];
if (0 <= r && r < N && 0 <= c && c < N && !visited[r][c] && temp[r][c] == 0) {
visited[r][c] = true;
que.add(new Point(r, c));
}
}
}
if (que.isEmpty()) break;
time++;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (temp[i][j] == 0 && !visited[i][j]) return;
}
}
ans = Math.min(ans, time);
}
private static void deepCopy() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
temp[i][j] = map[i][j];
}
}
}
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- ๋งต ์ ๋ณด๋ฅผ ์ ๋ ฅ ๋ฐ์ ๋, 2๋ฅผ ๋ฐ๊ฒฌํ๋ฉด ArrayList virus์ ํด๋น ์ขํ๋ฅผ ์ ์ฅํด๋๊ณ ๋งต์๋ 0์ผ๋ก ์ ์ด๋๋ค.
- ์กฐํฉ์์ virus์์ ์ด๋ค ์ขํ๋ฅผ M๊ฐ ๊ณ ๋ฅผ์ง ์ธ๋ฑ์ค๋ฅผ ์ ํํ๋ค.
- ๊ณ ๋ฅธ ์์น์์ ๋ฐ์ด๋ฌ์ค๋ฅผ ๋ชจ๋ ํผํธ๋ ค๋ณด๊ณ 0์ ๊ฐ์๋ฅผ ์นด์ดํธ ํ ๋ค ans๋ฅผ ์
๋ฐ์ดํธํ๋ค.
- ๋ฐ์ด๋ฌ์ค๋ฅผ ํผํธ๋ฆฌ๋ ๊ฑด ์๋ณธ ๋งต์ ๊ฑด๋๋ฆฌ์ง ์๊ณ temp๋ก ๊น์ ๋ณต์ฌ ํ ์คํํ๋ค.
๐ธ end ๐ธ
- ๋ฒฝ์ ๊ตณ์ด 1์์ -1๋ก ๋ฐ๊ฟ ํ์๋ ์์๋ ๊ฒ ๊ฐ๋ค.
728x90
'CodingTest > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ_13459 : ๊ตฌ์ฌ ํ์ถ (0) | 2023.04.06 |
---|---|
BOJ_2239 : ์ค๋์ฟ (0) | 2023.04.06 |
BOJ_2636 : ์น์ฆ (0) | 2023.04.06 |
BOJ_1600 : ๋ง์ด ๋๊ณ ํ ์์ญ์ด (0) | 2023.04.06 |
BOJ_10971 : ์ธํ์ ์ํ 2 (0) | 2023.04.06 |