-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.js
More file actions
49 lines (38 loc) ยท 974 Bytes
/
practice.js
File metadata and controls
49 lines (38 loc) ยท 974 Bytes
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
const fs = require("fs");
const input = fs.readFileSync("input.txt").toString().trim().split("\n");
let [n, r, c] = input[0].split(" ").map(Number);
let grid = input.slice(1, n + 1).map((line) => line.split(" ").map(Number));
// Please Write your code here.
[r, c] = [r - 1, c - 1];
const [dr, dc] = [
[-1, 1, 0, 0],
[0, 0, -1, 1],
];
// const convertToDirNum = {
// 'N': 0,
// 'S': 1,
// 'W': 2,
// 'E': 3
// };
const answerArr = [grid[r][c]];
function solution() {
let flag = true;
while (flag) {
for (let dirNum = 0; dirNum < 4; dirNum++) {
const [nr, nc] = [r + dr[dirNum], c + dc[dirNum]];
if (inRange(nr, nc) && grid[nr][nc] > grid[r][c]) {
[r, c] = [nr, nc];
answerArr.push(grid[r][c]);
break;
}
}
flag = false;
}
answerArr.forEach((elem) => {
process.stdout.write(`${elem} `);
});
}
function inRange(r, c) {
return r >= 0 && r < n && c >= 0 && c < n;
}
solution();