알고리즘(BOJ)/Gold

백준 - 연구소2 17141

ch.0 2023. 1. 28. 10:58


DFS

	public static void dfs(int x,int y) {

		if(y==size) {
			y=0;
			x++;
		}
		
		if (cnt == virus) {
			bfs(array);

		}
		Stack<int[]> stack = new Stack<int[]>();

		while(x<size) {
			if (array[x][y] == 2) {

				array[x][y] = -1;
				cnt++;
				dfs(x,y+1);
				array[x][y] = 2;
				cnt--;

			}
			y++;
			if(y==size) {
				y=0;
				x++;
			}
		}
		

	}

x,y 값을 증가(=2중포문) 시키면서 2인 값(바이러스) 를 찾는다.

방문여부대신 -1로 바꾸고 다시 반복시킨다.

 

전체소스 

package test;

import java.util.*;
import java.io.*;

public class Main {
	static int size, virus;
	static int[][] array;
	static int[][] ch;

	static int cnt = 0;
	static int min = 999999;

	public static void dfs(int x,int y) {

		if(y==size) {
			y=0;
			x++;
		}
		
		if (cnt == virus) {
			bfs(array);

		}
		Stack<int[]> stack = new Stack<int[]>();

		while(x<size) {
			if (array[x][y] == 2) {

				array[x][y] = -1;
				cnt++;
				dfs(x,y+1);
				array[x][y] = 2;
				cnt--;

			}
			y++;
			if(y==size) {
				y=0;
				x++;
			}
		}
		

	}

	public static void bfs(int[][] array_back) {


		int[][] arr = new int[size][size];

		for (int i = 0; i < size; i++) {
			arr[i] = array_back[i].clone();
		}
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if(arr[i][j]==2) {
					arr[i][j]=0;
				}
				if(arr[i][j]==1) {
					arr[i][j]=-8;
				}
			}
		}

		boolean[][] visited = new boolean[size][size];
		int[] x_move = { -1, 1, 0, 0 };
		int[] y_move = { 0, 0, -1, 1 };
		Queue<int[]> q = new LinkedList<>();

		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (arr[i][j] == -1 && visited[i][j] == false) {
					q.offer(new int[] { i, j, 0 });

				}
			}
		}

		while (!q.isEmpty()) {
//			for (int i = 0; i < size; i++) {
//				for (int j = 0; j < size; j++) {
//					System.out.print(arr[i][j] + " ");
//				}
//				System.out.println();
//			}
//			System.out.println("-------------");

			int point[] = q.poll();
			int x = point[0];
			int y = point[1];
			int time = point[2];

			for (int p = 0; p < 4; p++) {
				int nx = x + x_move[p];
				int ny = y + y_move[p];

				if (nx < 0 || ny < 0 || nx >= size || ny >= size)
					continue;

				if (arr[nx][ny] == 0 && visited[nx][ny] == false) {
					q.offer(new int[] { nx, ny, time + 1 });
					arr[nx][ny] = time + 1;
					visited[nx][ny] = true;
				}

			}

		}

		int max = -9999999;
		boolean check=true;
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (arr[i][j] > max) {
					max = arr[i][j];
				}
				if(arr[i][j]==0) {
					check=false;
				}

			}if(!check) break;
		}

		if (min > max && check==true) {
			min = max;
		}


	}

	public static void main(String args[]) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");

		size = Integer.parseInt(st.nextToken());
		virus = Integer.parseInt(st.nextToken());
		array = new int[size][size];
		ch = new int[size][size];

		for (int i = 0; i < size; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			for (int j = 0; j < size; j++) {
				array[i][j] = Integer.parseInt(st.nextToken());
			}
		}

		dfs(0,0);

		if(min==999999) {
			min=-1;
		}else if(min==-1) {
			min=0;
		}
		System.out.println(min);

	}
}

예외처리로 0이 하나라도 있을경우 바이러스가 다 퍼지지 않았기 때문에 -1 을 리턴시키도록했다.

또 다른 예외처리는 모든 바이러스가 시작점일때다 

이럴경우 -1로 바꿨기 떄문에 최대값이 -1이 되서 틀렸다.

때문에 -1일경우0을 리턴해주도록 했다. (문제에서 바이러스 시작점이 0 이기때문)

 

 

 

 


문제

인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 승원이는 연구소의 특정 위치에 바이러스 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