-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5ae2ea
commit adc53ac
Showing
3 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
const blocks = [ | ||
[ | ||
[ | ||
[1, 1], | ||
[1, 1] | ||
] | ||
], | ||
[ | ||
[ | ||
[2, 2, 2, 2] | ||
], | ||
[ | ||
[2], | ||
[2], | ||
[2], | ||
[2] | ||
] | ||
], | ||
[ | ||
[ | ||
[3, 3, 0], | ||
[0, 3, 3] | ||
], | ||
[ | ||
[0, 3], | ||
[3, 3], | ||
[3, 0] | ||
] | ||
], | ||
[ | ||
[ | ||
[0, 4, 4], | ||
[4, 4, 0] | ||
], | ||
[ | ||
[4, 0], | ||
[4, 4], | ||
[0, 4] | ||
] | ||
], | ||
[ | ||
[ | ||
[5, 0, 0], | ||
[5, 5, 5] | ||
], | ||
[ | ||
[5, 5], | ||
[5, 0], | ||
[5, 0] | ||
], | ||
[ | ||
[5, 5, 5], | ||
[0, 0, 5] | ||
], | ||
[ | ||
[0, 5], | ||
[0, 5], | ||
[5, 5] | ||
] | ||
], | ||
[ | ||
[ | ||
[0, 0, 6], | ||
[6, 6, 6] | ||
], | ||
[ | ||
[6, 6], | ||
[0, 6], | ||
[0, 6] | ||
], | ||
[ | ||
[6, 6, 6], | ||
[6, 0, 0] | ||
], | ||
[ | ||
[6, 0], | ||
[6, 0], | ||
[6, 6] | ||
] | ||
], | ||
[ | ||
[ | ||
[0, 7, 0], | ||
[7, 7, 7] | ||
], | ||
[ | ||
[7, 7, 7], | ||
[0, 7, 0] | ||
], | ||
[ | ||
[7, 0], | ||
[7, 7], | ||
[7, 0] | ||
], | ||
[ | ||
[0, 7], | ||
[7, 7], | ||
[0, 7] | ||
] | ||
], | ||
[ | ||
[ | ||
[0, 8, 0], | ||
[8, 8, 8], | ||
[0, 8, 0] | ||
] | ||
], | ||
[ | ||
[ | ||
[9] | ||
] | ||
] | ||
]; | ||
|
||
let m, n, a, l, res; | ||
|
||
function Solve(arr, num) { | ||
res = []; | ||
m = arr.length; | ||
n = arr[0].length; | ||
|
||
a = new Array(m); | ||
for (let i = 0; i < m; ++i) { | ||
a[i] = arr[i].map(x => x); | ||
} | ||
|
||
l = num.map(x => x); | ||
|
||
dfs(0); | ||
|
||
return res; | ||
} | ||
|
||
function canPlaceBlock(x, y, b, d) { | ||
const pat = blocks[b][d]; | ||
let offset = 0; | ||
while (!pat[0][offset]) ++offset; | ||
y -= offset; | ||
if (y < 0) return false; | ||
for (let i = 0; i < pat.length; ++i) { | ||
for (let j = 0; j < pat[0].length; ++j) { | ||
if (pat[i][j] && (x + i >= m || y + j >= n || a[x + i][y + j] !== -1)) return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
function placeBlock(x, y, b, d, v) { | ||
const pat = blocks[b][d]; | ||
let offset = 0; | ||
while (!pat[0][offset]) ++offset; | ||
y -= offset; | ||
for (let i = 0; i < pat.length; ++i) { | ||
for (let j = 0; j < pat[0].length; ++j) { | ||
if (pat[i][j]) a[x + i][y + j] = v; | ||
} | ||
} | ||
} | ||
|
||
function dfs(p) { | ||
if (p === m * n) { | ||
const x = new Array(m); | ||
for (let i = 0; i < m; ++i) { | ||
x[i] = a[i].map(x => x); | ||
} | ||
res.push(x); | ||
return; | ||
} | ||
const x = Math.floor(p / n), y = p % n; | ||
if (a[x][y] !== -1) { | ||
dfs(p + 1); | ||
return; | ||
} | ||
for (let b = 0; b < 9; ++b) { | ||
if (!l[b]) continue; | ||
for (let d = 0; d < blocks[b].length; ++d) { | ||
if (!canPlaceBlock(x, y, b, d)) continue; | ||
placeBlock(x, y, b, d, b + 1); | ||
--l[b]; | ||
dfs(p + 1); | ||
++l[b]; | ||
placeBlock(x, y, b, d, -1); | ||
} | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<!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.bootcdn.net/ajax/libs/vue/3.2.47/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: [0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
res: false, | ||
now: 0, | ||
color: ['white', '#75C0FF', '#3B66CF', '#78ACC5', '#C8FAFD', '#FDFF00', '#4BFF00', '#FF9800', '#B9B24B', '#FF00AE'] | ||
} | ||
}, | ||
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() { | ||
this.res = Solve(this.board, this.num) | ||
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> |