-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53fa6cd
commit 5d48c34
Showing
3 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: ::: ::: */ | ||
/* Problem Number: 17141 :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ahchjang <boj.kr/u/ahchjang> +#+ +#+ +#+ */ | ||
/* +#+ +#+ +#+ */ | ||
/* https://boj.kr/17141 #+# #+# #+# */ | ||
/* Solved: 2024/10/01 15:04:58 by ahchjang ### ### ##.kr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
import java.util.*; | ||
import java.io.*; | ||
|
||
public class Main { | ||
|
||
static class Point { | ||
int x, y; | ||
|
||
public Point(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
} | ||
|
||
static int[][] dist; | ||
static int M; | ||
static ArrayList<ArrayList<Integer>> combinations = new ArrayList<>(); | ||
|
||
static int[] dx = { 1, -1, 0, 0 }; | ||
static int[] dy = { 0, 0, 1, -1 }; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
int N = Integer.parseInt(st.nextToken()); | ||
M = Integer.parseInt(st.nextToken()); | ||
ArrayList<Point> virusPoints = new ArrayList<>(); | ||
|
||
int[][] map = new int[N][N]; | ||
for (int i = 0; i < N; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
for (int j = 0; j < N; j++) { | ||
int k = Integer.parseInt(st.nextToken()); | ||
map[i][j] = k; | ||
if (k == 2) | ||
virusPoints.add(new Point(i, j)); | ||
} | ||
} | ||
|
||
int virusPoints_count = virusPoints.size(); | ||
ArrayList<Integer> virus_list = new ArrayList<>(); | ||
|
||
permutation(virusPoints_count, virus_list, 1); | ||
int answer = 2000; | ||
|
||
for (ArrayList<Integer> virus_loc : combinations) { | ||
Queue<Point> queue = new LinkedList<>(); | ||
dist = new int[N][N]; | ||
for (int i = 0; i < N; i++) { | ||
Arrays.fill(dist[i], -1); | ||
} | ||
int max = 0; | ||
|
||
for (Integer i = 1; i <= virusPoints_count; i++) { | ||
int x = virusPoints.get(i - 1).x; | ||
int y = virusPoints.get(i - 1).y; | ||
if (virus_loc.contains(i)) { | ||
queue.add(virusPoints.get(i - 1)); | ||
dist[x][y] = 0; | ||
} | ||
} | ||
|
||
for (int i = 0; i < map.length; i++) { | ||
for (int j = 0; j < map.length; j++) { | ||
if (map[i][j] == 1) | ||
dist[i][j] = -2; | ||
} | ||
} | ||
|
||
while (!queue.isEmpty()) { | ||
Point cp = queue.poll(); | ||
int cx = cp.x; | ||
int cy = cp.y; | ||
|
||
for (int i = 0; i < 4; i++) { | ||
int nx = cx + dx[i]; | ||
int ny = cy + dy[i]; | ||
|
||
if (nx < 0 || nx >= N || ny < 0 || ny >= N) | ||
continue; | ||
if (map[nx][ny] == 1) | ||
continue; | ||
if (dist[nx][ny] != -1) | ||
continue; | ||
|
||
dist[nx][ny] = dist[cx][cy] + 1; | ||
queue.add(new Point(nx, ny)); | ||
max = Math.max(dist[nx][ny], max); | ||
} | ||
} | ||
|
||
// 만약 모든 점이 방문했다면 dist[][] 에 -1이 없을 것이다. | ||
boolean flag = true; | ||
for (int i = 0; i < N; i++) { | ||
for (int j = 0; j < N; j++) { | ||
if (dist[i][j] == -1) { | ||
flag = false; | ||
break; | ||
} | ||
} | ||
if (!flag) | ||
break; | ||
} | ||
|
||
if (flag) { | ||
answer = Math.min(answer, max); | ||
} | ||
} | ||
|
||
if (answer == 2000) { | ||
System.out.println(-1); | ||
} else { | ||
System.out.println(answer); | ||
} | ||
|
||
} | ||
|
||
static void permutation(int virusPoints_count, ArrayList<Integer> list, int start) { | ||
|
||
if (list.size() == M) { | ||
combinations.add(new ArrayList<>(list)); | ||
return; | ||
} | ||
|
||
for (Integer i = start; i <= virusPoints_count; i++) { | ||
list.add(i); | ||
permutation(virusPoints_count, list, i + 1); | ||
list.remove(list.size() - 1); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# 17141번: 연구소 2 - <img src="https://static.solved.ac/tier_small/12.svg" style="height:20px" /> Gold IV | ||
|
||
<!-- performance --> | ||
|
||
<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.--> | ||
|
||
<!-- end --> | ||
|
||
## 문제 | ||
|
||
[문제 링크](https://boj.kr/17141) | ||
|
||
|
||
<p>인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 승원이는 연구소의 특정 위치에 바이러스 M개를 놓을 것이고, 승원이의 신호와 동시에 바이러스는 퍼지게 된다.</p> | ||
|
||
<p>연구소는 크기가 N×N인 정사각형으로 나타낼 수 있으며, 정사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다.</p> | ||
|
||
<p>일부 빈 칸은 바이러스를 놓을 수 있는 칸이다. 바이러스는 상하좌우로 인접한 모든 빈 칸으로 동시에 복제되며, 1초가 걸린다.</p> | ||
|
||
<p>예를 들어, 아래와 같이 연구소가 생긴 경우를 살펴보자. 0은 빈 칸, 1은 벽, 2는 바이러스를 놓을 수 있는 칸이다.</p> | ||
|
||
<pre>2 0 0 0 1 1 0 | ||
0 0 1 0 1 2 0 | ||
0 1 1 0 1 0 0 | ||
0 1 0 0 0 0 0 | ||
0 0 0 2 0 1 1 | ||
0 1 0 0 0 0 0 | ||
2 1 0 0 0 0 2</pre> | ||
|
||
<p>M = 3이고, 바이러스를 아래와 같이 놓은 경우 6초면 모든 칸에 바이러스를 퍼뜨릴 수 있다. 벽은 -, 바이러스를 놓은 위치는 0, 빈 칸은 바이러스가 퍼지는 시간으로 표시했다.</p> | ||
|
||
<pre>6 6 5 4 - - 2 | ||
5 6 - 3 - 0 1 | ||
4 - - 2 - 1 2 | ||
3 - 2 1 2 2 3 | ||
2 2 1 0 1 - - | ||
1 - 2 1 2 3 4 | ||
0 - 3 2 3 4 5</pre> | ||
|
||
<p>시간이 최소가 되는 방법은 아래와 같고, 5초만에 모든 칸에 바이러스를 퍼뜨릴 수 있다.</p> | ||
|
||
<pre>0 1 2 3 - - 2 | ||
1 2 - 3 - 0 1 | ||
2 - - 2 - 1 2 | ||
3 - 2 1 2 2 3 | ||
3 2 1 0 1 - - | ||
4 - 2 1 2 3 4 | ||
5 - 3 2 3 4 5</pre> | ||
|
||
<p>연구소의 상태가 주어졌을 때, 모든 빈 칸에 바이러스를 퍼뜨리는 최소 시간을 구해보자.</p> | ||
|
||
|
||
|
||
## 입력 | ||
|
||
|
||
<p>첫째 줄에 연구소의 크기 N(5 ≤ N ≤ 50), 놓을 수 있는 바이러스의 개수 M(1 ≤ M ≤ 10)이 주어진다.</p> | ||
|
||
<p>둘째 줄부터 N개의 줄에 연구소의 상태가 주어진다. 0은 빈 칸, 1은 벽, 2는 바이러스를 놓을 수 있는 칸이다. 2의 개수는 M보다 크거나 같고, 10보다 작거나 같은 자연수이다.</p> | ||
|
||
|
||
|
||
## 출력 | ||
|
||
|
||
<p>연구소의 모든 빈 칸에 바이러스가 있게 되는 최소 시간을 출력한다. 바이러스를 어떻게 놓아도 모든 빈 칸에 바이러스를 퍼뜨릴 수 없는 경우에는 -1을 출력한다.</p> | ||
|
||
|
||
|
||
## 소스코드 | ||
|
||
[소스코드 보기](Main.java) |