반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 시뮬레이션
- 주사위굴리기
- char
- 원시타입
- 차이점
- primitivetype
- 자바
- sql태그
- 알고리즘
- Spring
- 차이
- SQL
- Char[]
- 코딩테스트
- string
- 자바오류
- 변수
- ReferenceType
- ClassPathResource
- mybatis
- 프로그래머스
- JSON
- Exception
- include태그
- json파싱
- 데이터탑입
- java
- 내맘대로정리
- 참조타입
- 백준
Archives
- Today
- Total
재채기는 H
백준 17142 - 연구소3 JAVA 본문
반응형
https://www.acmicpc.net/problem/17142
BFS를 활용해서 바이러스를 확장시키면 되는 문제
14502 연구소 문제와 비슷하다. 다만 몇 가지 조건만 추가해주면 된다.
1. 바이러스를 확장해 나아가다가 비활성화된 바이러스를 만났을 때
2. 맵을 복사할 때 문자로 바꾸는 것
2번 같은 경우에는 굳이 이차원 문자 배열로 만들 필요는 없고 자기만의 방식으로 구분하면 되지만
문제에서 주어진대로 구현하고 싶어서 이차 원문자 배열로 복사하였다.
두 가지 조건만 고려해서 접근하다면 그리 어렵지 않은 문제였다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
//바이러스 클래스
class Virus {
int x;
int y;
int time;
Virus(int x, int y, int time) {
this.x = x;
this.y = y;
this.time = time;
}
}
public class 백준_연구소3_17142 {
static int N, M, result = Integer.MAX_VALUE;
static int[][] map;
static char[][] copyMap;
static boolean[][] visited;
static ArrayList<Virus> list;
static int[] dx = { -1, 0, 1, 0 };
static int[] dy = { 0, 1, 0, -1 };
public static void main(String[] args) throws Exception {
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];
copyMap = new char[N][N];
list = new ArrayList<>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if (map[i][j] == 2) {
list.add(new Virus(i, j, 0));
}
}
}
//Combination으로 활성된 바이러스 선택
boolean[] check = new boolean[list.size()];
selectVirus(check, 0, list.size(), M);
//결과에 전부 못확장 시키면 리턴 -1
if (result == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(result);
}
}
//Combination으로 활성된 바이러스 선택
static void selectVirus(boolean[] check, int depth, int n, int r) {
if (r == 0) {
int val = 0;
//활성화된 바이러스와 비활성화된 바이러스의 리스트
ArrayList<Virus> selectList = new ArrayList<>();
ArrayList<Virus> remainList = new ArrayList<>();
visited = new boolean[N][N];
for (int i = 0; i < n; i++) {
if (check[i]) {
selectList.add(list.get(i));
} else {
remainList.add(list.get(i));
}
}
//정수형 이차웜 배열인 map을 문자형 이차원 배열로 변형하고 비활성화 된 바이러스 *로 표시
createCopyMap(remainList);
//바이러스 확장 시작
spreadVirus(selectList);
val = checkCount(); //확장 안된 곳 확인
// print();
if (val == -1) { //확장 안된 곳 존재
return;
} else { //확장 되었을 때의 최소 시간 계산
result = Math.min(result, val);
return;
}
}
if (depth == n) {
return;
}
check[depth] = true;
selectVirus(check, depth + 1, n, r - 1);
check[depth] = false;
selectVirus(check, depth + 1, n, r);
}
//지도 확인 메소드
static void print() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(copyMap[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
//정수형 이차원 배열을 문자형 이차원 배열로 변경
static void createCopyMap(ArrayList<Virus> remainList) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (map[i][j] == 1) {
copyMap[i][j] = '-';
} else {
copyMap[i][j] = '0';
}
}
}
for (int i = 0; i < remainList.size(); i++) {
copyMap[remainList.get(i).x][remainList.get(i).y] = '*';
}
}
//바이러스 확장 BFS
static void spreadVirus(ArrayList<Virus> selectList) {
for (int i = 0; i < selectList.size(); i++) {
visited[selectList.get(i).x][selectList.get(i).y] = true;
}
while (!selectList.isEmpty()) {
Virus p = selectList.remove(0);
for (int i = 0; i < 4; i++) {
int x = p.x + dx[i];
int y = p.y + dy[i];
if (0 <= x && x < N && 0 <= y && y < N && !visited[x][y]) {
if (copyMap[x][y] == '0' || copyMap[x][y] == '*') {
if (copyMap[x][y] == '*') {
selectList.add(new Virus(x, y, p.time + 1));
} else {
copyMap[x][y] = (char) ((char) (p.time+1)+'0');
selectList.add(new Virus(x, y, p.time + 1));
}
visited[x][y] = true;
}
}
}
}
}
// 전부 확장 확인
static int checkCount() {
int val = 0;
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (copyMap[i][j] - '0' > val) {
val = copyMap[i][j] - '0';
}
if (copyMap[i][j] == '0') {
cnt++;
}
}
}
if (cnt > M) {
return -1;
} else {
return val;
}
}
}
반응형
'알고리즘' 카테고리의 다른 글
백준 17779 - 게리멘더링2 JAVA (0) | 2020.08.20 |
---|---|
백준 17140 - 이차원 배열과 연산 JAVA (0) | 2020.08.08 |
백준 17144 - 미세먼지 안녕! JAVA (0) | 2020.07.31 |
백준 16235 - 나무 재테크 JAVA (2) | 2020.07.31 |
백준 16234 - 인구 이동 JAVA (0) | 2020.07.28 |
Comments