forked from CmdBlockZQG/cbjq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
173 lines (164 loc) · 5.78 KB
/
index.html
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>尘白禁区信源研析</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.global.prod.min.js"></script>
<script src="./calc.min.js"></script>
</head>
<body>
<a href="https://space.bilibili.com/277401945" target="_blank">Bilibili 方形的块状代码</a>
<div id="app">
<p>行数:<input type="text" v-model="row" /></p>
<p>列数:<input type="text" v-model="col" /></p>
<input type="button" value="确定" @click="confirmBoard">
<p>红色表示需要摆放的格子,白色表示这里没有格子,点击切换</p>
<div v-for="(r, i) in board" style="height: 32px;">
<div
class="box"
v-for="(c, j) in r"
@click="tuneBox(i, j)"
:style="{ 'background-color': c === 0 ? 'white' : 'red' }"
></div>
</div>
<div v-if="board.length" style="margin-top: 8px;">
<p v-for="(b, i) in num">方块{{ i + 1 }}个数:<input type="text" v-model.number="num[i]" /></p>
<input type="button" value="计算完美方案" @click="calc">
</div>
<div v-if="res !== false">
<p>方案数:{{ res.length }}</p>
</div>
<div v-if="res.length">
<p>当前展示方案:{{ now + 1 }} / {{ res.length }}</p>
<input type="button" value="<-" @click="now -= (now > 0)">
<input type="button" value="->" @click="now += (now + 1 < res.length)">
<div v-for="(r, i) in sol" style="height: 32px;">
<div
class="box"
v-for="(c, j) in r"
:style="{ 'background-color': color[c] }"
>{{ c }}</div>
</div>
</div>
</div>
<script>
const { createApp } = Vue
createApp({
data() {
return {
row: 5,
col: 6,
board: [],
num: new Array(11).fill(0),
res: false,
now: 0,
color: ['white', '#75C0FF', '#3B66CF', '#78ACC5', '#C8FAFD', '#FDFF00', '#4BFF00', '#FF9800', '#B9B24B', '#FF00AE', '#8A2BE2', '#FF00FF']
}
},
methods: {
confirmBoard() {
const row = Number(this.row)
const col = Number(this.col)
this.board = new Array(row)
for (let i = 0; i < row; ++i) {
const r = new Array(col).fill(-1)
this.board[i] = r
}
},
tuneBox(x, y) {
if (this.board[x][y] === -1) {
this.board[x][y] = 0
} else {
this.board[x][y] = -1
}
},
calc() {
const num_8 = new Array(11).fill(0)
num_8[7] = Number(this.num[7])
num_8[8] = Number(this.row) * Number(this.col)
const num_n8 = this.num.map(el => el)
num_n8[7] = 0
const board_8 = Solve(this.board, num_8)
let mapped = board_8.map((el, i) => {
return { index: i, value: (el.flat().filter(el => el === 8).length / 5) }
})
mapped.sort((a, b) => b.value - a.value)
let sorted = []
let s_max8 = []
let c_8 = mapped[0].value
while (c_8 >= 0) {
sorted = mapped.filter(el => el.value === c_8).map(el => board_8[el.index])
for (let s = 0; s < sorted.length ; ++s) {
for (let r = 0; r < Number(this.row); ++r) {
sorted[s][r].forEach((el, i, arr) => {
if (arr[i] === 9) arr[i] = -1
})
}
let s_min9 = []
for (let c_9 = 0; c_9 <= Number(this.num[8]); ++c_9) {
num_n8[8] = c_9
let rf_base = Math.max(...num_n8.slice(0,-4))
let s_rf = []
for (let rf = 0; rf <= rf_base; ++rf) {
let num_rf = num_n8.map(el => el)
for (let i = 0; i < num_rf.length - 4; ++i) {
num_rf[i] -= (rf_base - rf)
if (num_rf[i] < 0) num_rf[i] = 0
}
//console.log(num_rf)
s_rf = s_rf.concat(Solve(sorted[s], num_rf))
if (s_rf.length > 0) break
}
s_min9 = s_min9.concat(s_rf)
if (s_min9.length > 0) break
}
s_max8 = s_max8.concat(s_min9)
}
if (s_max8.length > 0) break
--c_8
}
// Sort res by the number of distinct elements in each matrix, in descending order
// If the number of distinct elements is the same,
// sort by the highest number in l for each element used in A and B
s_max8.sort((A, B) => {
let distinctA = new Set(A.flat()).size
let distinctB = new Set(B.flat()).size
if (distinctA !== distinctB) {
return distinctB - distinctA
} else {
let maxA = Math.max(...A.flat().map(x => this.num[x-1]))
let maxB = Math.max(...B.flat().map(x => this.num[x-1]))
return maxB - maxA
}
})
s_max8.sort((a, b) => {
return (b.flat().filter(el => el === 10).length / 2 + b.flat().filter(el => el === 11).length / 3) - (a.flat().filter(el => el === 10).length / 2 + a.flat().filter(el => el === 11).length / 3)
})
s_max8.sort((a, b) => a.flat().filter(el => el === 9).length - b.flat().filter(el => el === 9).length)
this.res = s_max8
this.now = 0
}
},
computed: {
sol() {
return this.res[this.now]
}
}
}).mount('#app')
</script>
<style>
p {
margin: 4px 0;
}
.box {
display: inline-block;
box-sizing: border-box;
width: 32px;
height: 32px;
border: 1px black solid;
cursor: pointer;
}
</style>
</body>
</html>