티스토리 뷰
https://www.acmicpc.net/problem/11967
11967번: 불켜기
(1, 1)방에 있는 스위치로 (1, 2)방과 (1, 3)방의 불을 켤 수 있다. 그리고 (1, 3)으로 걸어가서 (2, 1)방의 불을 켤 수 있다. (2, 1)방에서는 다시 (2, 2)방의 불을 켤 수 있다. (2, 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')
.map((v) => v.split(' ').map(Number));
const [N, M] = input.shift();
const dx = [0, 0, 1, -1];
const dy = [1, -1, 0, 0];
let room = Array.from(Array(N), () => Array(N).fill(0));
const light = new Map();
input.forEach((v) => {
const [x, y, a, b] = v.map((v) => v - 1);
const key = `${x}_${y}`;
if (light.has(key)) {
light.set(key, [...light.get(key), [a, b]]);
} else {
light.set(key, [[a, b]]);
}
});
room[0][0] = 2;
const q1 = new Queue();
const q2 = new Queue();
q1.push([0, 0]);
let answer = 1;
while (q1.length > 0) {
const [x, y] = q1.pop();
const key = `${x}_${y}`;
const value = light.get(key);
if (value) {
// 불켜기
value.forEach((v) => {
const [a, b] = v;
if (room[a][b] == 0) {
room[a][b] = 1;
q2.push([a, b]);
answer++;
}
});
}
for (let i = 0; i < q2.length; i++) {
const [x, y] = q2.pop();
let flag = false;
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 || room[nx][ny] == 0) continue;
if (room[nx][ny] == 2) {
room[x][y] = 2;
q1.push([x, y]);
flag = true;
break;
}
}
if (!flag) {
q2.push([x, y]);
}
}
}
// console.log(room.map((v) => v.join(' ')).join('\n'));
console.log(answer);
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 1074번: Z (0) | 2023.06.29 |
---|---|
Node.js) 백준 11729번: 하노이 탑 이동 순서 (0) | 2023.06.28 |
Node.js) 백준 2530번: 인공지능 시계 (0) | 2023.06.28 |
Node.js)백준 2146번: 다리 만들기 (0) | 2023.06.26 |
Node.js) 백준 6593번: 상범 빌딩 (0) | 2023.06.25 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- MySQL
- 다이나믹프로그래밍
- 롱베케이션
- 최소공통조상
- create databases;
- node.js
- 면접질문
- 로드나인
- 동적프로그래밍
- 투포인터
- 개발자면접
- 면접비
- create db
- 투포인터 연습
- 그래프
- BFS
- 서버점검
- 은둔청년체험
- MOD
- 다이나밍프로그래밍
- 서버개발
- 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 |
글 보관함