-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb2580.js
More file actions
67 lines (56 loc) · 1.34 KB
/
b2580.js
File metadata and controls
67 lines (56 loc) · 1.34 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
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const board = input.map((line) => line.split(" ").map(Number));
const N = board.length;
const empties = [];
let finish = false;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (board[i][j] === 0) {
empties.push([i, j]);
}
}
}
sudoku(0);
function sudoku(cursorIdx) {
if (finish) {
return;
}
if (empties.length === cursorIdx) {
console.log(board.map((line) => line.join(" ")).join("\n"));
finish = true;
return;
}
for (let k = 1; k <= 9; k++) {
const nx = empties[cursorIdx][0];
const ny = empties[cursorIdx][1];
if (isVerticalHorizontalOk(nx, ny, k) && isThreeByThreeOk(nx, ny, k)) {
board[nx][ny] = k;
sudoku(cursorIdx + 1);
if (finish) {
return;
}
board[nx][ny] = 0;
}
}
}
function isVerticalHorizontalOk(x, y, v) {
for (let k = 0; k < 9; k++) {
if (board[k][y] === v || board[x][k] === v) {
return false;
}
}
return true;
}
function isThreeByThreeOk(x, y, v) {
const xStart = Math.floor(x / 3) * 3;
const yStart = Math.floor(y / 3) * 3;
for (let i = xStart; i < xStart + 3; i++) {
for (let j = yStart; j < yStart + 3; j++) {
if (board[i][j] === v) {
return false;
}
}
}
return true;
}