/*
* 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
solve 3*3 magic square problem using recursive backtracking(using Stanford CS106b ADT library)
最新推荐文章于 2022-12-02 14:24:56 发布
这是一个使用斯坦福大学CS106b库的递归回溯算法来解决3x3魔方问题的C++程序。程序首先定义了显示网格、检查结果和递归求解的辅助函数。在主函数中,通过创建一个包含1到9的集合,并初始化3x3网格,然后调用递归求解函数进行求解。检查结果函数验证行、列和对角线的数字之和是否等于目标值15。
摘要由CSDN通过智能技术生成