연구소 2
시간 제한 | 메모리 제한 | 제출 | 정답 | 맞은 사람 | 정답 비율 |
---|---|---|---|---|---|
1 초 | 512 MB | 1971 | 888 | 605 | 43.369% |
문제
인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 승원이는 연구소의 특정 위치에 바이러스 M개를 놓을 것이고, 승원이의 신호와 동시에 바이러스는 퍼지게 된다.
연구소는 크기가 N×N인 정사각형으로 나타낼 수 있으며, 정사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다.
일부 빈 칸은 바이러스를 놓을 수 있는 칸이다. 바이러스는 상하좌우로 인접한 모든 빈 칸으로 동시에 복제되며, 1초가 걸린다.
예를 들어, 아래와 같이 연구소가 생긴 경우를 살펴보자. 0은 빈 칸, 1은 벽, 2는 바이러스를 놓을 수 있는 칸이다.
xxxxxxxxxx
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, 빈 칸은 바이러스가 퍼지는 시간으로 표시했다.
xxxxxxxxxx
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초만에 모든 칸에 바이러스를 퍼뜨릴 수 있다.
xxxxxxxxxx
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 복사
xxxxxxxxxx
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 복사
xxxxxxxxxx
5
예제 입력 2 복사
xxxxxxxxxx
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 복사
xxxxxxxxxx
5
예제 입력 3 복사
xxxxxxxxxx
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 복사
xxxxxxxxxx
4
예제 입력 4 복사
xxxxxxxxxx
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 복사
xxxxxxxxxx
3
예제 입력 5 복사
xxxxxxxxxx
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 복사
xxxxxxxxxx
7
예제 입력 6 복사
xxxxxxxxxx
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 복사
xxxxxxxxxx
-1
예제 입력 7 복사
xxxxxxxxxx
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 복사
xxxxxxxxxx
4
풀이
사용 알고리즘 : dfs(조합)+bfs
class 선언
class Node{
int x,y,time;
Node(int x, int y, int time){
this.x=x;
this.y=y;
this.time =time;
}
}
바이러스의 위치를 받을 x,y 좌표와 바이러스가 동시에 퍼지므로 큐에서 빼낸걸 기준으로 시간을 구해주기 위해 time이라는 변수도 넣어주었다.
for(int i=0; i<n; i++) {
String[] input = br.readLine().split(" ");
for(int j=0; j<n; j++) {
map[i][j] = Integer.parseInt(input[j]);
if(map[i][j]==2) {
virus.add(new Node(i,j,0)); // 바이러스를 놓을수 있는 위치
}
}
}
ArrayList타입의 virus에는 현재 x좌표 y좌표 시간이 들어있다.
전체적인 방향은 조합알고리즘으로 바이러스를 뽑고 그 후 bfs를 돌리면 된다.
public static void dfs(int level, int start) {
if(level==m) {
bfs();
return ;
}
for(int i=start; i<virus.size(); i++) {
if(!virus_visited[i]) {
virus_arr[level]=i;
virus_visited[i] = true;
dfs(level+1,i+1);
virus_visited[i] = false;
}
}
}
bfs를 돌릴 때 copy_map을 만들어 주었는데 우선 copy_map의 바이러스 놓을수 있는 위치를
일단 모두 0으로 바꿔주었다.
그 후 조합에 들어있는 부분은 2로 바꿔주면 현재 copy_map에는 m개 크기만큼의 바이러스만 있게 된다.
그 후 바이러스 확산을 bfs를 통해 돌린다.
조건에 맞는 부분은 2 (바이러스가 있다)로 바꿔준다.
bfs가 끝나고 copy_map에 0이 있다면 바이러스를 다 확산하지 못하는 것
이건 isPossible로 처리를 해주었다.
public static boolean isPossible() {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(copy_map[i][j]==0) {
return false;
}
}
}
return true;
}
그 후 이 조건에 맞을때만 min값을 변경하고 최종적으로 min값이 변경되지 않았으면 -1을 출력
아닐경우 답을 출력하면 된다.
전체코드
import java.io.*;
import java.util.*;
public class Main {
static int n,m;
static int map[][];
static int min = Integer.MAX_VALUE;
static ArrayList<Node>virus = new ArrayList<>();
static boolean visited[][];
static int copy_map[][]; //시간초 기록할 맵
static boolean virus_visited[]; //바이러스 조합 뽑기 위한 visted
static int virus_arr[]; //바이러스 조합 뽑기 위함
static int dx[] = {0,0,1,-1};
static int dy[] = {1,-1,0,0};
static int max=-1;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String [] t = br.readLine().split(" ");
n = Integer.parseInt(t[0]);
m = Integer.parseInt(t[1]);
copy_map = new int[n][n];
map = new int[n][n];
visited = new boolean[n][m];
for(int i=0; i<n; i++) {
String[] input = br.readLine().split(" ");
for(int j=0; j<n; j++) {
map[i][j] = Integer.parseInt(input[j]);
if(map[i][j]==2) {
virus.add(new Node(i,j,0)); // 바이러스를 놓을수 있는 위치
}
}
}
virus_visited = new boolean[virus.size()];
virus_arr = new int[m];
dfs(0,0);
if(min == Integer.MAX_VALUE) {
System.out.println(-1);
}
else {
System.out.println(min);
}
}
public static void dfs(int level, int start) {
if(level==m) {
bfs();
return ;
}
for(int i=start; i<virus.size(); i++) {
if(!virus_visited[i]) {
virus_arr[level]=i;
virus_visited[i] = true;
dfs(level+1,i+1);
virus_visited[i] = false;
}
}
}
public static void copy() {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(map[i][j]==2) {
copy_map[i][j]=0;
}
else {
copy_map[i][j]= map[i][j];
}
}
}
}
public static boolean isPossible() {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(copy_map[i][j]==0) {
return false;
}
}
}
return true;
}
public static void bfs() {
max =-1;
copy();
visited = new boolean[n][n];
Queue<Node> q= new LinkedList<>();
for(int i=0; i<m; i++) {
q.add(virus.get(virus_arr[i]));
int x = virus.get(virus_arr[i]).x;
int y = virus.get(virus_arr[i]).y;
copy_map[x][y]=2;
}
while(!q.isEmpty()) {
Node a= q.poll();
max = Math.max(max, a.time);
for(int i=0; i<4; i++) {
int nx = a.x+dx[i];
int ny = a.y+dy[i];
if(nx>=0 && ny>=0 && nx<n && ny<n) {
if(!visited[nx][ny] && copy_map[nx][ny]==0) {
visited[nx][ny]=true;
copy_map[nx][ny]=2;
q.add(new Node(nx,ny,a.time+1));
}
}
}
}
if(isPossible()) {
min = Math.min(min,max);
}
}
}
class Node{
int x,y,time;
Node(int x, int y, int time){
this.x=x;
this.y=y;
this.time =time;
}
}
후기
쉽게 한 번에 푼 문제.
요새 웹 공부도 병행하고 있어 알고리즘을 오랜만에 풀었는데 여지껏 알고리즘을 풀었던 것이 안까먹고 기억하고 있는것 같아 뿌듯했다.
'알고리즘' 카테고리의 다른 글
[백준 18808] 스티커 붙이기 -JAVA //le_effort// (0) | 2020.03.26 |
---|---|
[백준 17135] 캐슬디펜스 -JAVA // le_effort// (0) | 2020.03.25 |
[백준 3190] 뱀 -JAVA //le_effort// (0) | 2020.03.12 |
[백준 14890] 경사로 -JAVA // le_effort// (0) | 2020.03.12 |
[백준 14503] 로봇 청소기 -JAVA //le_effort// (0) | 2020.03.12 |