-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.js
More file actions
145 lines (119 loc) · 2.91 KB
/
bfs.js
File metadata and controls
145 lines (119 loc) · 2.91 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const makeBoard = (worldmap) => {
return worldmap.map((v) => v.split(''));
};
class Queue {
constructor() {
this.queue = [];
this.front = 0;
this.rear = 0;
}
enqueue(value) {
this.queue[this.rear] = value;
this.rear += 1;
}
dequeue() {
const value = this.queue[this.front];
delete this.queue[this.front];
this.front += 1;
return value;
}
get length() {
return this.rear - this.front;
}
}
/*
1. 방향 기억하기
*/
const LEFT = 'LEFT';
const RIGHT = 'RIGHT';
const UP = 'UP';
const DOWN = 'DOWN';
const Directions = {
LEFT: [-1, 0],
UP: [-1, 0],
RIGHT: [0, 1],
DOWN: [1, 0],
};
const nextDirections = {
LEFT: [Directions[DOWN], Directions[UP]],
UP: [Directions[LEFT], Directions[RIGHT]],
RIGHT: [Directions[UP], Directions[DOWN]],
DOWN: [Directions[RIGHT], Directions[LEFT]],
};
const nextDirectionNames = {
LEFT: [DOWN, UP],
UP: [LEFT, RIGHT],
RIGHT: [UP, DOWN],
DOWN: [RIGHT, LEFT],
};
function solution(worldmap) {
const rowLength = worldmap.length;
const colLength = worldmap[0].length;
const board = makeBoard(worldmap);
const visited = Array.from({ length: rowLength }, () =>
Array.from({ length: rowLength }, () => ({
LEFT: false,
RIGHT: false,
UP: false,
DOWN: false,
}))
);
const queue = new Queue();
// x, y, time, direction,
queue.enqueue([0, 0, 0, RIGHT, nextDirections[RIGHT]]);
visited[0][0] = true;
while (queue.length) {
const [x, y, time, nowDirectionName, nextDirection] = queue.dequeue();
if (x === rowLength - 1 && y === colLength - 1) {
return time;
}
const nowDirection = Directions[nowDirectionName];
const fx = x + nowDirection[0];
const fy = y + nowDirection[1];
if (
fx >= 0 &&
fy >= 0 &&
fx < rowLength &&
fy < colLength &&
!board[fx][fy] !== 'X'
) {
queue.enqueue([
fx,
fy,
time + 1,
nowDirectionName,
nextDirections[nowDirectionName],
]);
for (let i = 0; i < 2; i += 1) {
const [dx, dy] = nextDirection[i];
const nd = nextDirectionNames[nowDirectionName][i];
const ndx = Directions[nd][0] + x;
const ndy = Directions[nd][1] + y;
if (
ndx >= 0 &&
ndy >= 0 &&
ndx < rowLength &&
ndy < colLength &&
board[ndx][ndy] === 'X' &&
visited[ndx][ndy][nd]
)
continue;
const nx = fx + dx;
const ny = fy + dy;
if (
nx >= 0 &&
ny >= 0 &&
nx < rowLength &&
ny < colLength &&
!visited[nx][ny] &&
!board[nx][ny] !== 'X'
) {
visited[nx][ny][nowDirectionName] = true;
queue.enqueue([nx, ny, time + 1, nd, nextDirections[nd]]);
}
}
}
}
}
const worldmap = ['..XXX', '..XXX', '...XX', 'X....', 'XXX..'];
console.log(solution(worldmap));