-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14503.js
64 lines (60 loc) · 1.17 KB
/
14503.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Title: 로봇 청소기
// Time: O(nm)
// Space: O(nm)
let input = require("fs")
.readFileSync(__dirname + "/input.txt")
.toString()
.trim()
.split("\n");
input = input.map((i) => i.split(" ").map(Number));
input.shift();
const [x, y, d] = input.shift();
const dir = [
[-1, 0],
[0, -1],
[1, 0],
[0, 1],
];
const rotate = {
0: [0, -1, 3],
3: [1, 0, 2],
2: [0, 1, 1],
1: [-1, 0, 0],
};
const back = {
0: [1, 0],
3: [0, 1],
2: [-1, 0],
1: [0, -1],
};
let result = 0;
const stack = [[x, y, d]];
while (stack.length) {
const [x, y, d] = stack.pop();
if (input[x][y] === 0) {
input[x][y] = 2;
result++;
}
let flag = false;
for (let i = 0; i < 4; i++) {
const [dx, dy] = dir[i];
const nx = x + dx;
const ny = y + dy;
if (input[nx][ny] === 0) flag = true;
}
if (flag) {
const [dx, dy, dd] = rotate[d];
const nx = x + dx;
const ny = y + dy;
if (input[nx][ny] === 0) stack.push([nx, ny, dd]);
else stack.push([x, y, dd]);
}
if (!flag) {
const [dx, dy] = back[d];
const nx = x + dx;
const ny = y + dy;
if (input[nx][ny] === 1) break;
else stack.push([nx, ny, d]);
}
}
console.log(result);