一、问题描述
二、问题分析
没仔细看题,于是一开始还是当作计数题,尝试分解为小问题,而后发现是输出所有的可能那么就得遍历,在遍历的基础上对条件(“仪式铺法”)进行限制达到剪枝的效果,而后就是条件的代码实现问题,另一方面如何进行输出和记录中间结果也十分重要,在实现中,我使用的方法与原书一致,主要差别在于书中使用“自然数组边界”的方法,而我对数组访问进行判断。
三、代码实现
1.C/C++实现
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cctype>
using namespace std;
// const int MAX_M = 4;
// const int MAX_N = 7;
const int MAX_M = 5;
const int MAX_N = 6;
int mapping[MAX_M][MAX_N];
char chars[MAX_M][MAX_N + 1];
void print_mapping()
{
// 将数字 id 标识转化为目标输出字符
for (int i = 0; i < MAX_M; i++)
{
for (int j = 0; j < MAX_N; j++)
{
if (j + 1 < MAX_N && mapping[i][j] == mapping[i][j + 1])
chars[i][j] = chars[i][j + 1] = '-';
else if (i + 1 < MAX_M && mapping[i][j] == mapping[i + 1][j])
chars[i][j] = chars[i + 1][j] = '|';
}
}
// 输出
for (int i = 0; i < MAX_M; i++)
cout << chars[i] << endl;
cout << endl;
}
// 按当前坐标(x,y)进行尝试
void put_block(int x, int y, int id)
{
if (x >= MAX_M) // 全部填完,进行输出
print_mapping();
else if (y >= MAX_N) // 当前行填完,填下一行
put_block(x + 1, 0, id + 1);
else if (mapping[x][y]) // 当前格已填过
put_block(x, y + 1, id + 1);
else
{
// 当前坐标不满足非十字要求
if (x > 0 && y > 0 && (mapping[x - 1][y - 1] != mapping[x - 1][y] && mapping[x - 1][y - 1] != mapping[x][y - 1]))
return;
// 尝试横着
if (y + 1 < MAX_N && !(mapping[x][y] | mapping[x][y + 1]))
{
mapping[x][y] = mapping[x][y + 1] = id;
put_block(x, y + 2, id + 1);
mapping[x][y] = mapping[x][y + 1] = 0;
}
// 尝试竖着
if (x + 1 < MAX_M && !(mapping[x][y] | mapping[x + 1][y]))
{
mapping[x][y] = mapping[x + 1][y] = id;
put_block(x, y + 1, id + 1);
mapping[x][y] = mapping[x + 1][y] = 0;
}
}
}
int main()
{
memset(mapping, 0, sizeof(mapping));
memset(chars, 0, sizeof(chars));
put_block(0, 0, 1);
return 0;
}
2.Python实现
# coding = utf-8
def my_print(mapping):
max_x, max_y = len(mapping), len(mapping[0])
for i in range(max_x):
for j in range(max_y):
if mapping[i][j] == '-' or mapping[i][j] == '|':
continue
if j + 1 < max_y and mapping[i][j] == mapping[i][j + 1]:
mapping[i][j] = mapping[i][j + 1] = '-'
elif i + 1 < max_x and mapping[i][j] == mapping[i + 1][j]:
mapping[i][j] = mapping[i + 1][j] = '|'
print(''.join(mapping[i]))
print()
def next_fix(mapping, x, y, index):
max_x, max_y = len(mapping), len(mapping[0])
if x < 0:
my_print([item[:] for item in mapping])
elif y < 0:
next_fix(mapping, x - 1, max_y - 1, index + 1)
elif mapping[x][y]:
next_fix(mapping, x, y - 1, index + 1)
else:
if x + 1 >= max_x or y + 1 >= max_y \
or mapping[x + 1][y + 1] == mapping[x][y + 1] \
or mapping[x + 1][y + 1] == mapping[x + 1][y]:
if y - 1 >= 0 and mapping[x][y] + mapping[x][y - 1] == 0:
mapping[x][y] = mapping[x][y - 1] = index
next_fix(mapping, x, y - 2, index + 1)
mapping[x][y] = mapping[x][y - 1] = 0
if x - 1 >= 0 and mapping[x][y] + mapping[x - 1][y] == 0:
mapping[x][y] = mapping[x - 1][y] = index
next_fix(mapping, x, y - 1, index + 1)
mapping[x][y] = mapping[x - 1][y] = 0
def get(max_x, max_y):
mapping = [[0] * max_y for i in range(max_x)]
next_fix(mapping, max_x - 1, max_y - 1, 1)
if __name__ == '__main__':
get(4, 7)
get(5, 6)
pass