알고리즘 공부
백준 - 경쟁적 전염
컴퓨터과학
2024. 1. 2. 23:37
https://www.acmicpc.net/problem/18405
18405번: 경쟁적 전염
첫째 줄에 자연수 N, K가 공백을 기준으로 구분되어 주어진다. (1 ≤ N ≤ 200, 1 ≤ K ≤ 1,000) 둘째 줄부터 N개의 줄에 걸쳐서 시험관의 정보가 주어진다. 각 행은 N개의 원소로 구성되며, 해당 위치
www.acmicpc.net
bfs 문제 쉽게 풀었습니다.
import java.util.*;
public class Main {
private int x;
private int y;
private int data;
public Main(int x, int y, int data) {
this.x = x;
this.y = y;
this.data = data;
}
public int I() {
return this.x;
}
public int J() {
return this.y;
}
public int Data() {
return this.data;
}
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int[][] maps = new int[n][n];
Queue<Main>[] virusQueues = new LinkedList[k + 1];
for (int i = 1; i <= k; i++) {
virusQueues[i] = new LinkedList<>();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
maps[i][j] = scan.nextInt();
if (maps[i][j] != 0) {
virusQueues[maps[i][j]].add(new Main(i, j, maps[i][j]));
}
}
}
int s = scan.nextInt();
int x = scan.nextInt() - 1;
int y = scan.nextInt() - 1;
int[] diri = {1, -1, 0, 0};
int[] dirj = {0, 0, 1, -1};
int sec = 0;
while (sec < s) {
for (int v = 1; v <= k; v++) {
int size = virusQueues[v].size();
for (int i = 0; i < size; i++) {
Main virus = virusQueues[v].poll();
for (int d = 0; d < 4; d++) {
int tmpdiri = virus.I() + diri[d];
int tmpdirj = virus.J() + dirj[d];
if (tmpdiri < 0 || tmpdirj < 0 || tmpdiri >= n || tmpdirj >= n || maps[tmpdiri][tmpdirj] != 0) {
continue;
}
maps[tmpdiri][tmpdirj] = virus.Data();
virusQueues[v].add(new Main(tmpdiri, tmpdirj, virus.Data()));
}
}
}
sec++;
}
System.out.println(maps[x][y]);
}
}