-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobotProblem.js
114 lines (91 loc) · 3.05 KB
/
robotProblem.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
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
const { traverse } = require("@babel/types");
/**
* Given two inputs,
First input is the location map, a 2D array
X
| O | E | E | E | X |
X | E | O | X | X | X |
| E | E | E | E | E |
| X | E | O | E | E |
| X | E | X | E | X |
X
// [2, 2, 1, 3]
O = Robot, E = Empty, X = Blocker
three blockers
O(row * col) * (m + n)
Second input is the query. It’s a 1D array consisting of distance to the closest blocker in the order from left, top, bottom and right
[2, 2, 4, 1] -> This means distance of 2 to the left closest blocker, 2 to the top closest blocker, 4 to the bottom closest blocker and 1 to the right closest blocker
Write a function that takes these two inputs and returns the index of the robots (if any) that matches the query that we’re looking for.
*
*
* touching walls
* hitting a blocker in between or robot
* if there is no robot match the query
*
*
* [['O','E','E','E','X'],['E','O','X','X','X'],['E','E','E','E','E'],['X','E','O','E','E'],['X','E','X','E','X']]
*
*/
const Robot = 'O';
const Empty = 'E';
const Blocker = 'X';
function GetRobot(grid, query) {
const rowLen = grid.length;
const colLen = grid.length;
for(let i = 0; i < rowLen; i++) {
for(let j = 0; j < colLen; j++) {
if (grid[i][j] == Robot && IsQueryMatches(grid, i, j, query)) return [i, j];
}
}
return [-1, -1];
}
function IsQueryMatches(grid, row, col, query) {
const [left, top, bottom, right] = query;
console.log(row, col, query);
console.log(traveseTop(grid, row, col, top));
console.log(traverseLeft(grid, row, col, left));
console.log(traverseBottom(grid, row, col, bottom));
console.log(traveseright(grid, row, col, right));
if (!(traveseTop(grid, row, col, top) && traverseLeft(grid, row, col, left) && traverseBottom(grid, row, col, bottom) && traveseright(grid, row, col, right))) return false;
return true;
}
function traveseTop(grid, row, col, top) {
for(var i = row - 1; i >= 0 && i >= row - top; i--) {
if (grid[i][col] != Empty) return true;
}
if (i < 0) return true;
return false;
}
function traverseLeft(grid, row, col, left) {
for(var i = col - 1; i >= 0 && i >= col - left; i--) {
console.log(i);
if (grid[row][i] != Empty) return true;
}
if (i < 0) return true;
return false;
}
function traverseBottom(grid, row, col, bottom) {
const rowLen = grid.length;
for(var i = row + 1; i < rowLen && i <= row + bottom; i++) {
if (grid[i][col] != Empty) return true;
}
if (i >= rowLen) return true;
return false;
}
function traveseright(grid, row, col, right) {
const colLen = grid.length;
for(var i = col + 1; i < colLen && i <= col + right; i++) {
if (grid[row][i] != Empty) return true;
}
if (i >= colLen) return true;
return false;
}
const grid = [
['O','E','E','E','X'],
['E','O','X','X','X'],
['E','E','E','E','E'],
['X','E','O','E','E'],
['X','E','X','E','X']
];
const query = [2, 2, 4, 1];
console.log(GetRobot(grid, query));