연구소2

Updated:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

	static int num;
	static int[][] arr;
	static ArrayList<Node> list1;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		num = sc.nextInt();
		int v = sc.nextInt();

		arr = new int[num][num];

		list1 = new ArrayList<>();

		for (int i = 0; i < num; i++) {
			for (int j = 0; j < num; j++) {
				arr[i][j] = sc.nextInt();
				if (arr[i][j] == 1)
					arr[i][j] = -1;
				if (arr[i][j] == 2) {
					arr[i][j] = -2;
					list1.add(new Node(i, j, 0));
				}
			}
		}

		/*
		 * for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).r +
		 * " " + list.get(i).c); }
		 */

		/*
		 * for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) {
		 * System.out.print(arr[i][j] + "\t"); } System.out.println(); }
		 */
		c(list1, 0, 0, new Node[v]);

		if (min == Integer.MAX_VALUE)
			System.out.println(-1);
		else
			System.out.println(min);
	}

	static int min = Integer.MAX_VALUE;

	static void c(ArrayList<Node> list, int n, int c, Node[] re) {
		if (re.length == c) {
			int[][] tmp = new int[num][num];
			for (int i = 0; i < num; i++) {
				for (int j = 0; j < num; j++) {
					tmp[i][j] = arr[i][j];
				}
			}

			for (int i = 0; i < list.size(); i++) {
				boolean g = true;
				for (int j = 0; j < re.length; j++) {
					if (list.get(i).r == re[j].r && list.get(i).c == re[j].c) {
						g = false;
					}
				}
				if (g)
					tmp[list.get(i).r][list.get(i).c] = 0;
			}

			Queue<Node> q = new LinkedList<>();
			boolean[][] che = new boolean[num][num];
			for (int i = 0; i < re.length; i++) {
				// System.out.print(re[i].r + " " + re[i].c + " ");
				q.add(re[i]);
				che[re[i].r][re[i].c] = true;
			}
			// System.out.println();

			while (!q.isEmpty()) {
				Node node = q.poll();
				for (int i = 0; i < 4; i++) {
					int nr = node.r + dr[i];
					int nc = node.c + dc[i];
					if (nr >= 0 && nc >= 0 && nr < num && nc < num && tmp[nr][nc] == 0 && che[nr][nc] == false) {
						tmp[nr][nc] = node.cnt + 1;
						che[nr][nc] = true;
						q.add(new Node(nr, nc, node.cnt + 1));
					}
				}
			}

			boolean cc = true;
			for (int i = 0; i < num; i++) {
				for (int j = 0; j < num; j++) {
					if (tmp[i][j] == 0)
						cc = false;
				}
			}

			int max = 0;
			for (int i = 0; i < num; i++) {
				for (int j = 0; j < num; j++) {
					//System.out.print(tmp[i][j] + "\t");
					if (cc && max < tmp[i][j])
						max = tmp[i][j];
				}
				//System.out.println();
			}
			//System.out.println(max);
			//System.out.println();

			if (cc && min > max) {
				min = max;
			}
			//System.out.println(min);
			//System.out.println();
			//System.out.println();

			return;
		}
		if (list.size() == n) {
			return;
		}

		re[c] = list.get(n);
		c(list, n + 1, c + 1, re);
		c(list, n + 1, c, re);

	}

	static class Node {
		int r, c, cnt;

		Node(int r, int c, int cnt) {
			this.r = r;
			this.c = c;
			this.cnt = cnt;
		}
	}

	static int[] dr = { -1, 1, 0, 0 };
	static int[] dc = { 0, 0, -1, 1 };

}

문제

인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 승원이는 연구소의 특정 위치에 바이러스 M개를 놓을 것이고, 승원이의 신호와 동시에 바이러스는 퍼지게 된다.

연구소는 크기가 N×N인 정사각형으로 나타낼 수 있으며, 정사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다.

일부 빈 칸은 바이러스를 놓을 수 있는 칸이다. 바이러스는 상하좌우로 인접한 모든 빈 칸으로 동시에 복제되며, 1초가 걸린다.

예를 들어, 아래와 같이 연구소가 생긴 경우를 살펴보자. 0은 빈 칸, 1은 벽, 2는 바이러스를 놓을 수 있는 칸이다.

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

M = 3이고, 바이러스를 아래와 같이 놓은 경우 6초면 모든 칸에 바이러스를 퍼뜨릴 수 있다. 벽은 -, 바이러스를 놓은 위치는 0, 빈 칸은 바이러스가 퍼지는 시간으로 표시했다.

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

시간이 최소가 되는 방법은 아래와 같고, 5초만에 모든 칸에 바이러스를 퍼뜨릴 수 있다.

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

연구소의 상태가 주어졌을 때, 모든 빈 칸에 바이러스를 퍼뜨리는 최소 시간을 구해보자.

입력

첫째 줄에 연구소의 크기 N(5 ≤ N ≤ 50), 놓을 수 있는 바이러스의 개수 M(1 ≤ M ≤ 10)이 주어진다.

둘째 줄부터 N개의 줄에 연구소의 상태가 주어진다. 0은 빈 칸, 1은 벽, 2는 바이러스를 놓을 수 있는 칸이다. 2의 개수는 M보다 크거나 같고, 10보다 작거나 같은 자연수이다.

출력

연구소의 모든 빈 칸에 바이러스가 있게 되는 최소 시간을 출력한다. 바이러스를 어떻게 놓아도 모든 빈 칸에 바이러스를 퍼뜨릴 수 없는 경우에는 -1을 출력한다.

예제

예제 입력 1

7 3
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

예제 출력 1

5

예제 입력 2

7 3
2 0 2 0 1 1 0
0 0 1 0 1 2 0
0 1 1 2 1 0 0
2 1 0 0 0 0 2
0 0 0 2 0 1 1
0 1 0 0 0 0 0
2 1 0 0 2 0 2

예제 출력 2

5

예제 입력 3

7 4
2 0 2 0 1 1 0
0 0 1 0 1 2 0
0 1 1 2 1 0 0
2 1 0 0 0 0 2
0 0 0 2 0 1 1
0 1 0 0 0 0 0
2 1 0 0 2 0 2

예제 출력 3

4

예제 입력 4

7 5
2 0 2 0 1 1 0
0 0 1 0 1 2 0
0 1 1 2 1 0 0
2 1 0 0 0 0 2
0 0 0 2 0 1 1
0 1 0 0 0 0 0
2 1 0 0 2 0 2

예제 출력 4

3

예제 입력 5

7 3
2 0 2 0 1 1 0
0 0 1 0 1 0 0
0 1 1 1 1 0 0
2 1 0 0 0 0 2
1 0 0 0 0 1 1
0 1 0 0 0 0 0
2 1 0 0 2 0 2

예제 출력 5

7

예제 입력 6

7 2
2 0 2 0 1 1 0
0 0 1 0 1 0 0
0 1 1 1 1 0 0
2 1 0 0 0 0 2
1 0 0 0 0 1 1
0 1 0 0 0 0 0
2 1 0 0 2 0 2

예제 출력 6

-1

예제 입력 7

5 1
2 2 2 1 1
2 1 1 1 1
2 1 1 1 1
2 1 1 1 1
2 2 2 1 1

예제 출력 7

4

알고리즘 분류

  • 그래프 이론
  • 그래프 탐색
  • 브루트포스 알고리즘
  • 너비 우선 탐색
  • 깊이 우선 탐색

Leave a comment