solve 3*3 magic square problem using recursive backtracking(using Stanford CS106b ADT library)

这是一个使用斯坦福大学CS106b库的递归回溯算法来解决3x3魔方问题的C++程序。程序首先定义了显示网格、检查结果和递归求解的辅助函数。在主函数中,通过创建一个包含1到9的集合,并初始化3x3网格,然后调用递归求解函数进行求解。检查结果函数验证行、列和对角线的数字之和是否等于目标值15。
摘要由CSDN通过智能技术生成
/* 
 * solve 3*3 magic matrix problem, using recursive backtracking 
 * algorithm, using Stanford cs106b library, may needs improvement 
 * for potential bug & performance
 * version: 0.1, date: May-30-2015, author: Wei Xing, 
 */
#include <iostream>
#include <string>
#include "genlib.h"
#include "Grid.h"
#include "Set.h"

using std::cout;
using std::endl;
using std::string;

/* Private function prototype */
template<typename T>
void DisplayGrid(Grid<T> g);

bool CheckResult(Grid<int>& g);
void RecursiveSolve(Grid<int> g, Set<int> s);

/* Main function */
int main()
{
	Set<int> s;
	for (int i = 0; i < 9; i++) {
		s.add(i+1);
	}
	Grid<int> g(3,3);

	RecursiveSolve(g, s);
	cout << "now all done!" << endl;

	return 0;
}

/* Private function implementation */
template<typename T>
void DisplayGrid(Grid<T> g) {
	for (size_t r
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值