我太喜欢逻辑练习了,不能让这个通过我,所以我花了一段时间在 javascript 中制定一个解决方案。首先,代码创建一个表格来显示结果并用作数据结构,然后有四个函数用于检查水平线、垂直线和两条对角线。这四个函数中的每一个都具有相同的形式:在每一行上,找到未设置值的空闲单元格的数量,以及包含主体的完整单元格的数量。然后,如果有足够的空闲单元格用于包含身体的剩余单元格,则填充这些单元格。最后,如果没有包含正文的剩余单元格,则将剩余的空闲单元格标记为空。
之后,剩下的就是冲洗并重复。每次运行四个函数中的一个时,都会有更多的单元格被标记为已满或为空,从而使下一个函数能够在更多约束条件下执行相同的操作。所有四个函数的三遍解决了您的示例问题,尽管更大和更复杂的形状肯定需要更多,如果它们可以解决的话。我可以很容易地想象这种方法无法解决的形状。
function create(rows, cols) {
var table = document.createElement('table');
for (var i = 0; i < rows; i++) {
var row = table.insertRow(-1);
for (var k = 0; k < cols; k++) {
var cell = row.insertCell(-1);
cell.value = null;
cell.innerHTML = ' ';
cell.style.width = '15px';
cell.style.backgroundColor = '#cccccc';
}
}
table.maxrow = rows - 1;
table.maxcol = cols - 1;
document.body.appendChild(table);
return table;
}
function checkRows(table, rows) {
for (var i = 0; i < rows.length; i++) {
var free = 0;
var full = 0;
for (var k = 0; k <= table.maxcol; k++) {
if (table.rows[i].cells[k].value == null) {
free++;
} else if (table.rows[i].cells[k].value == 1) {
full++;
}
}
if (free == 0) {
continue;
} else if (rows[i] - full == free) {
for (var k = 0; k <= table.maxcol; k++) {
if (table.rows[i].cells[k].value == null) {
table.rows[i].cells[k].style.backgroundColor = '#ffcccc';
table.rows[i].cells[k].value = 1;
}
}
} else if (rows[i] - full == 0) {
for (var k = 0; k <= table.maxcol; k++) {
if (table.rows[i].cells[k].value == null) {
table.rows[i].cells[k].style.backgroundColor = '#ccffcc';
table.rows[i].cells[k].value = 0;
}
}
}
}
}
function checkCols(table, cols) {
for (var i = 0; i < cols.length; i++) {
var free = 0;
var full = 0;
for (var k = 0; k <= table.maxrow; k++) {
if (table.rows[k].cells[i].value == null) {
free++;
} else if (table.rows[k].cells[i].value == 1) {
full++;
}
}
if (free == 0) {
continue;
} else if (cols[i] - full == free) {
for (var k = 0; k <= table.maxrow; k++) {
if (table.rows[k].cells[i].value == null) {
table.rows[k].cells[i].style.backgroundColor = '#ffcccc';
table.rows[k].cells[i].value = 1;
}
}
} else if (cols[i] - full == 0) {
for (var k = 0; k <= table.maxrow; k++) {
if (table.rows[k].cells[i].value == null) {
table.rows[k].cells[i].style.backgroundColor = '#ccffcc';
table.rows[k].cells[i].value = 0;
}
}
}
}
}
function checkDiagonals1(table, diagonals) {
for (var i = 0; i < diagonals.length; i++) {
var row = i;
var col = 0;
if (i > table.maxrow) {
row = table.maxrow;
col = i - row;
}
var free = 0;
var full = 0;
for (var k = 0; k <= row && col + k <= table.maxcol; k++) {
if (table.rows[row - k].cells[col + k].value == null) {
free++;
} else if (table.rows[row - k].cells[col + k].value == 1) {
full++;
}
}
if (free == 0) {
continue;
} else if (diagonals[i] - full == free) {
for (var k = 0; k <= row && col + k <= table.maxcol; k++) {
if (table.rows[row - k].cells[col + k].value == null) {
table.rows[row - k].cells[col + k].style.backgroundColor = '#ffcccc';
table.rows[row - k].cells[col + k].value = 1;
}
}
} else if (diagonals[i] - full == 0) {
for (var k = 0; k <= row && col + k <= table.maxcol; k++) {
if (table.rows[row - k].cells[col + k].value == null) {
table.rows[row - k].cells[col + k].style.backgroundColor = '#ccffcc';
table.rows[row - k].cells[col + k].value = 0;
}
}
}
}
}
function checkDiagonals2(table, diagonals) {
for (var i = 0; i < diagonals.length; i++) {
var row = table.maxrow;
var col = i;
if (i > table.maxcol) {
row = table.maxrow - i + table.maxcol;
col = table.maxcol;
}
var free = 0;
var full = 0;
for (var k = 0; k <= row && k <= col; k++) {
if (table.rows[row - k].cells[col - k].value == null) {
free++;
} else if (table.rows[row - k].cells[col - k].value == 1) {
full++;
}
}
if (free == 0) {
continue;
} else if (diagonals[i] - full == free) {
for (var k = 0; k <= row && k <= col; k++) {
if (table.rows[row - k].cells[col - k].value == null) {
table.rows[row - k].cells[col - k].style.backgroundColor = '#ffcccc';
table.rows[row - k].cells[col - k].value = 1;
}
}
} else if (diagonals[i] - full == 0) {
for (var k = 0; k <= row && k <= col; k++) {
if (table.rows[row - k].cells[col - k].value == null) {
table.rows[row - k].cells[col - k].style.backgroundColor = '#ccffcc';
table.rows[row - k].cells[col - k].value = 0;
}
}
}
}
}
var rows = new Array(10, 10, 6, 4, 6, 8, 13, 15, 11, 6);
var cols = new Array(2, 4, 5, 5, 7, 6, 7, 10, 10, 10, 7, 3, 3, 5, 5);
var diagonals1 = new Array(0, 1, 2, 2, 2, 2, 4, 5, 5, 6, 7, 6, 5, 6, 6, 5, 5, 6, 6, 3, 2, 2, 1, 0);
var diagonals2 = new Array(0, 0, 1, 3, 4, 4, 4, 4, 3, 4, 5, 7, 8, 8, 9, 9, 6, 4, 4, 2, 0, 0, 0, 0);
var table = create(rows.length, cols.length);
checkRows(table, rows);
checkCols(table, cols);
checkDiagonals1(table, diagonals1);
checkDiagonals2(table, diagonals2);