-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb17406.js
More file actions
88 lines (70 loc) · 1.93 KB
/
b17406.js
File metadata and controls
88 lines (70 loc) · 1.93 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
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const [N, M, K] = input[0].split(" ").map(Number);
const arr = input.slice(1, N + 1).map((row) => row.split(" "));
const operations = input
.slice(N + 1, N + 1 + K)
.map((row) => row.split(" ").map(Number));
function rotate(tpLft, btmRght, arr) {
const [lr, lc] = tpLft;
const [rr, rc] = btmRght;
if (lr === rr && lc === rc) return;
let curr = arr[lr][lc];
let next = arr[lr][lc + 1];
for (let i = lc; i < rc; i++) {
next = arr[lr][i + 1];
arr[lr][i + 1] = curr;
curr = next;
}
for (let i = lr; i < rr; i++) {
next = arr[i + 1][rc];
arr[i + 1][rc] = curr;
curr = next;
}
for (let i = rc; i > lc; i--) {
next = arr[rr][i - 1];
arr[rr][i - 1] = curr;
curr = next;
}
for (let i = rr; i > lr; i--) {
next = arr[i - 1][lc];
arr[i - 1][lc] = curr;
curr = next;
}
rotate([lr + 1, lc + 1], [rr - 1, rc - 1], arr);
}
const permutations = [];
const used = Array(operations.length).fill(false);
function getPermutat(dpth, currOrder) {
if (dpth === used.length) {
permutations.push(currOrder);
return;
}
for (let i = 0; i < operations.length; i++) {
if (!used[i]) {
used[i] = true;
getPermutat(dpth + 1, currOrder + i);
used[i] = false;
}
}
}
getPermutat(0, "");
let min = Infinity;
permutations.forEach((pmt) => {
const sortedOperations = [];
for (const i of pmt) {
sortedOperations.push(operations[i]);
}
const newArr = arr.map((row) => row.slice());
sortedOperations.forEach(([r, c, s]) => {
const topLeft = [r - s - 1, c - s - 1];
const rightBtm = [r + s - 1, c + s - 1];
rotate(topLeft, rightBtm, newArr);
});
const currMin = newArr.reduce((acc, row) => {
const sum = row.reduce((acc, v) => acc + +v, 0);
return acc > sum ? sum : acc;
}, Infinity);
min = Math.min(currMin, min);
});
console.log(min);