티스토리 뷰
https://www.acmicpc.net/problem/17142
17142번: 연구소 3
인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 바이러스는 활성 상태와 비활성 상태가 있다. 가장 처음에 모든 바이러스는 비활성 상태이고,
www.acmicpc.net
class Node {
constructor(item) {
this.item = item;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(item) {
const node = new Node(item);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
this.length += 1;
}
pop() {
const popItem = this.head;
this.head = this.head.next;
this.length -= 1;
return popItem.item;
}
}
const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n');
const [N, M] = input.shift().split(' ').map(Number);
let Lab = input.map((v) => v.split(' ').map(Number));
let answer = Infinity;
const virus = [];
const virusLab = [];
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (Lab[i][j] == 2) {
virus.push([i, j]);
Lab[i][j] = '*';
} else if (Lab[i][j] == 1) {
Lab[i][j] = '-';
} else if (Lab[i][j] == 0) {
Lab[i][j] = Infinity;
}
}
}
function bfs(x, y) {
const dr = [0, 0, 1, -1];
const dc = [1, -1, 0, 0];
let tempLab = Lab.map((v) => [...v]);
const q = new Queue();
q.push([x, y, 0]);
tempLab[x][y] = 0;
while (q.length > 0) {
const [r, c, t] = q.pop();
}
virusLab.push(tempLab);
}
virus.forEach(([x, y]) => {
bfs(x, y);
});
const V = virus.length;
// V Combination M
for (let i = 0; i <= (1 << V) - 1; i++) {
let j = i;
const selected = [];
let index = 0;
while (j > 0) {
if ((j & 1) == 1) {
selected.push(index);
}
j >>= 1;
index++;
}
if (selected.length == M) {
const dx = [0, 0, 1, -1];
const dy = [1, -1, 0, 0];
const q = new Queue();
let injectedLab = Lab.map((v) => [...v]);
selected.forEach((v) => {
const [x, y] = virus[v];
q.push([x, y, 0]);
injectedLab[x][y] = 0;
});
while (q.length > 0) {
const [x, y, t] = q.pop();
for (let i = 0; i < 4; i++) {
const nx = x + dx[i];
const ny = y + dy[i];
if (
nx >= 0 &&
nx < N &&
ny >= 0 &&
ny < N &&
injectedLab[nx][ny] != '-' &&
injectedLab[nx][ny] > t + 1
) {
injectedLab[nx][ny] = t + 1;
q.push([nx, ny, t + 1]);
} else if (nx >= 0 && nx < N && ny >= 0 && ny < N && injectedLab[nx][ny] == '*') {
injectedLab[nx][ny] = t + 1;
q.push([nx, ny, t + 1]);
}
}
}
virus.filter((v, i) => {
if (!selected.includes[i]) {
return v;
}
}).forEach(([x, y]) => {
injectedLab[x][y] = '*';
});
let time = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (injectedLab[i][j] == '-' || injectedLab[i][j] == '*') continue;
time = time > injectedLab[i][j] ? time : injectedLab[i][j];
}
}
answer = answer > time ? time : answer;
}
}
console.log(answer === Infinity ? -1 : answer);
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 2440번: 별 찍기 - 3 (0) | 2023.06.14 |
---|---|
Node.js) 백준:17779번: 게리맨더링2 (0) | 2023.06.12 |
Node.js)백준 17140번: 이차원 배열과 연산 (0) | 2023.06.11 |
Node.js)백준 14891번: 톱니바퀴 (0) | 2023.06.11 |
Node.js)백준 14503번: 로봇청소기 (0) | 2023.06.11 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- MOD
- 면접비
- 다이나믹프로그래밍
- DB 생성
- create databases;
- 최소공통조상
- 투포인터
- MySQL
- 다이나밍프로그래밍
- 그래프
- 서버점검
- 은둔청년체험
- 투포인터 연습
- KMP
- node.js
- 로드나인
- 개발자면접
- 롱베케이션
- BFS
- create db
- 면접질문
- 동적프로그래밍
- 서버개발
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
29 | 30 |
글 보관함