티스토리 뷰
https://www.acmicpc.net/problem/6593
6593번: 상범 빌딩
당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어
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 dx = [0, 0, 0, 0, -1, 1];
const dy = [0, 0, 1, -1, 0, 0];
const dz = [1, -1, 0, 0, 0, 0];
const answer = [];
while (input.length > 1) {
const [L, R, C] = input.shift().split(' ').map(Number);
//빌딩 만들기
const building = input.splice(0, (R + 1) * L).reduce(
(r, v) => {
if (v == '') {
r.push([]);
} else {
r[r.length - 1].push(v.split(''));
}
return r;
},
[[]]
);
building.pop();
// 상범이의 모험
const result = sol(L, R, C, building);
answer.push(result);
}
console.log(answer.join('\n'));
function sol(L, R, C, building) {
// 방문했던 곳 표시할 배열
let visited = Array.from(Array(L), () => Array.from(Array(R), () => Array(L).fill(false)));
// 상범이 위치와 목적지 찾기
let s = [];
let e = [];
for (let k = 0; k < L; k++) {
for (let i = 0; i < R; i++) {
for (let j = 0; j < C; j++) {
if (building[k][i][j] == 'S') {
s = [k, i, j];
visited[k][i][j] = true;
} else if (building[k][i][j] == 'E') {
e = [k, i, j];
}
}
}
}
const q = new Queue();
q.push([...s, 0]);
while (q.length > 0) {
const [z, x, y, t] = q.pop();
if (z == e[0] && x == e[1] && y == e[2]) {
return `Escaped in ${t} minute(s).`;
}
for (let k = 0; k < 6; k++) {
const nz = z + dz[k];
const nx = x + dx[k];
const ny = y + dy[k];
if (
nz < 0 ||
nz >= L ||
nx < 0 ||
nx >= R ||
ny < 0 ||
ny >= C ||
visited[nz][nx][ny] ||
building[nz][nx][ny] == '#'
)
continue;
visited[nz][nx][ny] = true;
q.push([nz, nx, ny, t + 1]);
}
}
return 'Trapped!';
}
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 2530번: 인공지능 시계 (0) | 2023.06.28 |
---|---|
Node.js)백준 2146번: 다리 만들기 (0) | 2023.06.26 |
Node.js)백준 5427번: 불 (0) | 2023.06.25 |
Node.js) 백준 9466번: 텀 프로젝트 (0) | 2023.06.25 |
Node.js) 백준 2573번: 빙산 (0) | 2023.06.25 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 최소공통조상
- 투포인터
- 다이나밍프로그래밍
- 다이나믹프로그래밍
- 서버점검
- 은둔청년체험
- 그래프
- 서버개발
- create databases;
- MOD
- 투포인터 연습
- create db
- 롱베케이션
- 면접비
- node.js
- 로드나인
- 동적프로그래밍
- MySQL
- DB 생성
- 개발자면접
- 면접질문
- BFS
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함