티스토리 뷰
https://www.acmicpc.net/problem/15806
// https://www.acmicpc.net/problem/15806
// 영우의 기숙사 청소
// 큐
class Item {
public next: Item | null = null;
constructor(public value: any) {}
}
class Queue {
public head: Item | null = null;
public tail: Item | null = null;
public size = 0;
constructor() {}
push(value: any) {
const node = new Item(value);
if (!this.head) {
this.head = node;
} else if (this.tail) {
this.tail.next = node;
}
this.tail = node;
this.size += 1;
}
pop() {
if (this.head) {
const item = this.head;
this.head = this.head.next;
this.size -= 1;
return item ? item.value : null;
} else {
return null;
}
}
}
const dx = [2, 2, 1, 1, -2, -2, -1, -1];
const dy = [1, -1, 2, -2, 1, -1, 2, -2];
const input: string[] = require('fs').readFileSync('./dev/stdin').toString().trim().split('\n');
const [N, M, K, T] = input.shift()!.split(' ').map(Number);
const moldList = input.splice(0, M).map((v: string) =>
v
.trim()
.split(' ')
.map((v) => +v - 1)
);
const inspectList = input.map((v: string) =>
v
.trim()
.split(' ')
.map((v) => +v - 1)
);
const q = new Queue();
const visited = Array.from(new Array(N), () => Array.from(new Array(N), () => Array(2).fill(false)));
moldList.forEach((mold) => {
q.push(mold);
});
for (let t = 0; t < T; t++) {
const qSize = q.size;
for (let i = 0; i < qSize; i++) {
const [x, y] = q.pop();
let flag = false;
for (let k = 0; k < 8; k++) {
const nx = x + dx[k];
const ny = y + dy[k];
if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
flag = true;
if (!visited[nx][ny][(t + 1) % 2]) {
q.push([nx, ny]);
visited[nx][ny][(t + 1) % 2] = true;
}
}
if (flag) visited[x][y][t % 2] = true;
}
}
inspectList.forEach((v) => {
const [x, y] = v;
if (visited[x][y][T % 2]) {
console.log('YES');
process.exit();
}
});
console.log('NO');
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
TS) 백준 6549번: 히스토그램에서 가장 큰 직사각형 (0) | 2024.03.03 |
---|---|
TS) 백준 7662번: 이중 우선순위 큐 (0) | 2024.03.01 |
TS) 백준 1385번: 벌집 (2) | 2024.02.28 |
TS)백준 6588번: 골드바흐의 추측 (0) | 2024.02.28 |
Node.js) 백준 14245번: XOR (1) | 2024.02.28 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 그래프
- MOD
- 다이나믹프로그래밍
- node.js
- create databases;
- 면접비
- 최소공통조상
- BFS
- create db
- 서버개발
- MySQL
- 동적프로그래밍
- 로드나인
- 롱베케이션
- 다이나밍프로그래밍
- 투포인터 연습
- 서버점검
- 면접질문
- 투포인터
- 은둔청년체험
- 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 |
글 보관함