-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10429 copy.js
More file actions
79 lines (63 loc) · 2.03 KB
/
10429 copy.js
File metadata and controls
79 lines (63 loc) · 2.03 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
/*
숫자부터 dfs를 돌리면 필연적으로 수식을 완성할 수 있음
숫자가 5개니까 무식하게 돌리면 됨
*/
const INPUT_FILE = process.platform === 'linux' ? '/dev/stdin' : './input';
const [info, ...game] = require('fs').readFileSync(INPUT_FILE).toString().trim().split('\n');
const [target, numbers] = info.split(' ').map(Number);
const board = game.map((line) => line.split('').map((char) => Number(char) || char));
const DIRECTIONS = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const calculate = (path) => {
let result = board[path[0][0]][path[0][1]];
let i = 1;
while (i < path.length) {
const operator = board[path[i][0]][path[i][1]];
const value = board[path[i + 1][0]][path[i + 1][1]];
result += (operator === '+' ? 1 : -1) * value;
i += 2;
}
return result;
};
const isPossible = (startR, startC, targetValue, maxCount) => {
const path = [[startR, startC]];
const visited = Array.from({ length: 3 }).map(() => new Array(3));
visited[startR][startC] = true;
const dfs = (r, c) => {
if (path.length === maxCount * 2 - 1) {
return calculate(path) === targetValue ? path : [];
}
for (let i = 0; i < DIRECTIONS.length; i += 1) {
const [dr, dc] = DIRECTIONS[i];
const r2 = r + dr;
const c2 = c + dc;
if (r2 < 0 || c2 < 0 || r2 >= 3 || c2 >= 3 || visited[r2][c2]) continue;
path.push([r2, c2]);
visited[r2][c2] = true;
const result = dfs(r2, c2);
if (result.length) return result;
visited[r2][c2] = null;
path.pop();
}
return [];
};
return dfs(startR, startC);
};
const getPath = (targetValue, maxCount) => {
for (let r = 0; r < 3; r += 1) {
for (let c = 0; c < 3; c += 1) {
if (typeof board[r][c] === 'number') {
const result = isPossible(r, c, targetValue, maxCount);
if (result.length) return result;
}
}
}
return [];
};
const sol = getPath(target, numbers);
console.log(sol.length ? 1 : 0);
if (sol.length) console.log(sol.map((line) => line.join(' ')).join('\n'));