-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinder#2.js
More file actions
149 lines (126 loc) · 2.91 KB
/
PathFinder#2.js
File metadata and controls
149 lines (126 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
146
147
148
149
// https://www.codewars.com/kata/57658bfa28ed87ecfa00058a/solutions/javascript
//TODO: duplicate find shoud be be optimized
function pathFinder(input) {
console.log(input);
const wall = 'W';
const directions = [[1, 0], [0, 1], [-1, 0], [0, -1]];
const maze = input.split('\n').map(s => s.split(''));
const steps = input.split('\n').map(s => s.split('').fill(-1));
//0: unaccessed, 1: accessed & true, 2: accessed & false
const states = input.split('\n').map(s => s.split('').fill(0));
console.log(states);
const width = maze[0].length;
const height = maze.length;
let count = 0;
const result = findExit({ x: 0, y: 0 }, 0);
console.log(steps);
console.log({ count });
// console.log(logMaze(states));
if (result) {
return steps[height - 1][width - 1];
}
else {
return false;
}
function findExit(position, step) {
count++;
const { x, y } = position;
console.log({position,step});
if (x < 0 || y < 0 || x > width - 1 || y > height - 1 || maze[y][x] == wall) {
return false;
}
else {
if (steps[y][x] >= 0 && step >= steps[y][x]) {
return false;
}
// if (steps[y][x] == -2) {
if (states[y][x] == 2) {
// return false;
}
steps[y][x] = step;
if (x == width - 1 && y == height - 1) {
return true;
}
else {
let result = false;
for (let index = 0; index < 4; index++) {
const [xOffset, yOffset] = directions[index];
result = findExit({ x: x + xOffset, y: y + yOffset }, step + 1) || result;
}
if (!result) {
// steps[y][x] = -2; //cause result error!
// states[y][x] = 2;
}
return result;
}
}
}
}
function logMaze(maze) {
const str = maze.map(a => a.join(' ')).join('\n');
console.log('maze:');
console.log(str);
}
function test(input) {
input = input.replace(/^\n/,'');
console.time();
const result = pathFinder(input);
console.timeEnd();
console.log({result} );
}
input1 = `
.W.
.W.
...`;
input2 =
`.W.
.W.
W..`;
input3 = `
......
......
......
......
......
......`;
input4 =
`......
......
......
......
.....W
....W.`;
//10
input5 =
`.....W
......
W..WWW
..W.W.
......
.W.W..`;
//16
input6 =
`.......WW
.W.W..W..
......W.W
...W....W
.W....W..
WW......W
..W..W.W.
..W......
.W..W..W.`;
// console.log(pathFinder(input1));
// console.log(pathFinder(input2));
// console.log(pathFinder(input3));
// console.log(pathFinder(input4));
// console.log(pathFinder(input5));
// console.log(pathFinder(input6));
// test(input1);
// test(input6);
// test(input3);
t1 = `
....
....
....
....`;
test(t1);