티스토리 뷰
https://www.acmicpc.net/problem/17141
17141번: 연구소 2
인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 승원이는 연구소의 특정 위치에 바이러스 M개를 놓을 것이고, 승원이의 신호와 동시에 바이러
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;
}
}
let input = require('fs').readFileSync('./dev/stdin').toString().split('\n').map(v => v.split(' ').map(Number));
const [N, M] = input.shift()
let answer = Infinity;
const virus = [];
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (input[i][j] == 2) {
virus.push([i, j])
input[i][j] = Infinity;
} else if (input[i][j] == 0) {
input[i][j] = Infinity;
} else {
input[i][j] = -1;
}
}
}
// 바이러스 조합 virus.length C M
const possibleVirucLoc = [];
function dfsVirus(arr) {
if (arr.length == M) {
possibleVirucLoc.push([...arr])
return;
} else {
for (let i = arr[arr.length - 1] + 1; i < virus.length; i++) {
arr.push(i);
dfsVirus(arr);
arr.pop();
}
}
}
for (let i = 0; i < virus.length; i++) {
dfsVirus([i]);
}
const dx = [0, 0, -1, 1];
const dy = [-1, 1, 0, 0];
possibleVirucLoc.forEach(v => {
let board = input.map(v => [...v]);
const q = new Queue();
v.forEach(v => {
const [x, y] = virus[v];
board[x][y] = 0;
q.push([x, y, 0]);
})
while (q.length > 0) {
const [x, y] = q.pop();
const time = board[x][y];
for (let k = 0; k < 4; k++) {
const nx = x + dx[k];
const ny = y + dy[k];
if (nx < 0 || nx >= N || ny < 0 || ny >= N || board[nx][ny] <= time + 1) continue;
board[nx][ny] = time + 1;
q.push([nx, ny]);
}
}
const flatBoard = [...new Set(board.flat())];
if (!flatBoard.includes(Infinity)) {
const max = Math.max(...flatBoard);
answer = Math.min(max, answer);
}
})
if (answer == Infinity) console.log(-1);
else console.log(answer);
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 10156번: 과자 (0) | 2023.07.11 |
---|---|
Node.js) 백준 1722번: 순열의 순서 (0) | 2023.07.11 |
Node.js) 백준 16987번: 계란으로 계란치기 (0) | 2023.07.09 |
Node.js) 백준 1941번: 소문난 칠공주 (0) | 2023.07.09 |
Node.js) 백준 11559번: Puyo Puyo (0) | 2023.07.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 면접질문
- node.js
- DB 생성
- 동적프로그래밍
- 다이나밍프로그래밍
- 롱베케이션
- BFS
- 그래프
- 개발자면접
- 최소공통조상
- 투포인터
- create databases;
- 면접비
- MOD
- 투포인터 연습
- 서버개발
- 다이나믹프로그래밍
- 서버점검
- create db
- MySQL
- 로드나인
- 은둔청년체험
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함