算法趣题-Q32

这篇博客探讨了如何用C/C++和Python解决一个棋盘填字游戏的问题。作者首先分析问题,指出需要遍历并限制条件以减少搜索空间。接着,展示了两种语言的代码实现,分别详细解释了横置和竖置方块的逻辑,并给出了输出结果的函数。通过递归函数,实现了所有可能的布局输出。
摘要由CSDN通过智能技术生成

一、问题描述

二、问题分析

        没仔细看题,于是一开始还是当作计数题,尝试分解为小问题,而后发现是输出所有的可能那么就得遍历,在遍历的基础上对条件(“仪式铺法”)进行限制达到剪枝的效果,而后就是条件的代码实现问题,另一方面如何进行输出和记录中间结果也十分重要,在实现中,我使用的方法与原书一致,主要差别在于书中使用“自然数组边界”的方法,而我对数组访问进行判断。

三、代码实现

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值