用C++实现的解数独(Sudoku)程序
我是一个C++初学者,控制台实现了一个解数独的小程序。
代码如下:
//"数独游戏"V1.0
//李国良于2016年11月11日编写完成
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
const int ArSize = 9;
string loadFile(int arr[ArSize][ArSize]);//读取文件,返回文件名
void printMap(int arr[ArSize][ArSize]);//显示数独
void solve(int arr[ArSize][ArSize], int enumer[ArSize], int i, int j);//计算当前单元格的候选数
bool solveV(int arr[ArSize][ArSize], int i, int j);//判断当前单元格是否可填
bool lopMap(int arr[ArSize][ArSize]);//循环遍历未解单元格,调用solveV求解
bool loopMap(int arr[ArSize][ArSize]);//暴力求解!!!
void saveFile(int arr[ArSize][ArSize], string str);//保存文件
int main()
{
SetConsoleTitle("数独游戏");
int map[ArSize][ArSize];
for (auto &row : map)
for (auto &ival : row)
ival = -1;
string name = loadFile(map);
printMap(map);
bool surplus = lopMap(map);
cout << "lopMap()解答完毕" << endl;
printMap(map);
if (!surplus)
{
loopMap(map);
cout << "loopMap()解答完毕" << endl;
printMap(map);
}
saveFile(map, name);
cin.get();
cin.get();
return 0;
}
string loadFile(int arr[ArSize][ArSize])
{
string fileName;
ifstream inFile;
cout << "请输入文件名:" << endl;
while (true)
{
cin >> fileName;
inFile.open(fileName + ".txt");
if (!inFile.is_open())
{
cout << "文件未能成功打开,请重新输入文件名:" << endl;
continue;
}
bool judg = true;
for (int i = 0; i < ArSize; ++i)
{
for (int j = 0; j < ArSize; ++j)
{
inFile >> arr[i][j];
if (arr[i][j] < 0 || arr[i][j] > 9)
judg = false;
}
}
if (judg)
{
cout << "文件\"" << fileName << ".txt" << "\"载入成功!" << endl;
inFile.close();
break;
}
else
{
cout << "文件内容有误,请重新输入文件名:" << endl;
inFile.close();
continue;
}
}
return fileName;
}
void printMap(int arr[ArSize][ArSize])
{
cout << endl;
int num = 0;
for (int i = 0; i < ArSize; ++i)
{
for (int j = 0; j < ArSize; ++j)
{
cout << arr[i][j];
if (j != 8)
cout << " ";
if (!arr[i][j])
num += 1;
}
cout << endl;
}
cout << num << "剩余单元格!" << endl << endl;
}
void solve(int arr[ArSize][ArSize], int enumer[ArSize], int i, int j)
{
for (int num = 0; num < ArSize; ++num)
enumer[num] = num + 1;
for (int m = 0; m < ArSize; ++m)
{
if (arr[m][j])
enumer[arr[m][j] - 1] = 0;
}
for (int n = 0; n < ArSize; ++n)
{
if (arr[i][n])
enumer[arr[i][n] - 1] = 0;
}
for (int m = i / 3 * 3; m < i / 3 * 3 + 3; ++m)
{
for (int n = j / 3 * 3; n < j / 3 * 3 + 3; ++n)
{
if (arr[m][n])
enumer[arr[m][n] - 1] = 0;
}
}
}
bool solveV(int arr[ArSize][ArSize], int i, int j)
{
int enumeration[ArSize];
int ation[ArSize];
solve(arr, enumeration, i, j);
int x = 0;
int y;
for (int i = 0; i < ArSize; ++i)
{
if (enumeration[i])
{
y = i;
x += 1;
}
}
if (x == 1)
{
arr[i][j] = enumeration[y];
return true;
}
else
{
for (y = 0; y < ArSize; ++y)
{
if (enumeration[y])
{
for (int m = 0; m < ArSize; ++m)
{
if (arr[m][j] == 0 && m != i)
{
solve(arr, ation, m, j);
if (ation[y])
{
break;
}
}
}
if (!ation[y])
{
arr[i][j] = enumeration[y];
return true;
}
for (int n = 0; n < ArSize; ++n)
{
if (arr[i][n] == 0 && n != j)
{
solve(arr, ation, i, n);
if (ation[y])
{
break;
}
}
}
if (!ation[y])
{
arr[i][j] = enumeration[y];
return true;
}
bool judge = false;
for (int m = i / 3 * 3; m < i / 3 * 3 + 3; ++m)
{
for (int n = j / 3 * 3; n < j / 3 * 3 + 3; ++n)
{
if (arr[m][n] == 0 && (m != i || n != j))
{
solve(arr, ation, m, n);
if (ation[y])
{
goto label;
}
}
}
}
label:
if (!ation[y])
{
arr[i][j] = enumeration[y];
return true;
}
}
}
}
return false;
}
bool lopMap(int arr[ArSize][ArSize])
{
int num = 0;
while (true)
{
int number = 0;
for (int i = 0; i < ArSize; ++i)
{
for (int j = 0; j < ArSize; ++j)
{
if (!arr[i][j])
{
if (!solveV(arr, i, j))
{
number += 1;
}
}
}
}
if (!number || num == number)
{
num = number;
break;
}
num = number;
}
return num == 0 ? true : false;
}
bool loopMap(int arr[ArSize][ArSize])
{
for (int i = 0; i < ArSize; ++i)
{
for (int j = 0; j < ArSize; ++j)
{
if (!arr[i][j])
{
int enumer[ArSize];
solve(arr, enumer, i, j);
for (int n = 0; n < ArSize; ++n)
{
if (enumer[n])
{
int maps[ArSize][ArSize];
for (int x = 0; x < ArSize; ++x)
{
for (int y = 0; y < ArSize; ++y)
{
maps[x][y] = arr[x][y];
}
}
maps[i][j] = enumer[n];
if (lopMap(maps))
{
for (int x = 0; x < ArSize; ++x)
{
for (int y = 0; y < ArSize; ++y)
{
arr[x][y] = maps[x][y];
}
}
return true;
}
else
{
bool judge = true;
for (int i = 0; i < ArSize; ++i)
{
for (int j = 0; j < ArSize; ++j)
{
if (!maps[i][j])
{
int num = 0;
int enumerat[ArSize];
solve(maps, enumerat, i, j);
for (auto n : enumerat)
{
num += n;
}
if (!num)
{
judge = false;
}
}
}
}
if (judge)
{
if (loopMap(maps))
{
for (int x = 0; x < ArSize; ++x)
{
for (int y = 0; y < ArSize; ++y)
{
arr[x][y] = maps[x][y];
}
}
return true;
}
}
}
}
}
return false;
}
}
}
}
void saveFile(int arr[ArSize][ArSize], string str)
{
ofstream outFile;
outFile.open(str + "answer.txt");
if (!outFile.is_open())
{
cout << "文件保存失败!" << endl;
return;
}
for (int i = 0; i < ArSize; ++i)
{
for (int j = 0; j < ArSize; ++j)
{
outFile << arr[i][j];
if (j != 8)
outFile << " ";
}
outFile << endl;
}
cout << "文件\"" << str << "answer.txt" << "\"保存成功!" << endl;
outFile.close();
}
loopMap()函数使用了递归,递归函数写的非常伤脑筋,感觉这个函数写的不好,目前还没找到改进的办法,姑且能用。
用C++实现的解数独(Sudoku)程序的更多相关文章
- [Swift]LeetCode37. 解数独 | Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
- 【原创】一个基于简单剪枝的DFS解数独程序
问题来源:leetCode Sudoku Solver Write a program to solve aSudoku puzzle by filling the empty cells. Empt ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 解数独(Python)
0.目录 1.介绍 2.一些通用函数 3.全局变量(宏变量) 4.数独预处理(约束传播) 5.解数独(深度优先搜索+最小代价优先) 6.主函数 7.总代码 1.介绍 数独是一个非常有趣味性的智力游戏, ...
- SudokuSolver 1.0:用C++实现的数独解题程序 【二】
本篇是 SudokuSolver 1.0:用C++实现的数独解题程序 [一] 的续篇. CQuizDealer::loadQuiz 接口实现 1 CQuizDealer* CQuizDealer::s ...
- 用C++实现的数独解题程序 SudokuSolver 2.1 及实例分析
SudokuSolver 2.1 程序实现 在 2.0 版的基础上,2.1 版在输出信息上做了一些改进,并增加了 runtil <steps> 命令,方便做实例分析. CQuizDeale ...
- 用C++实现的数独解题程序 SudokuSolver 2.4 及实例分析
SudokuSolver 2.4 程序实现 本次版本实现了 用C++实现的数独解题程序 SudokuSolver 2.3 及实例分析 里发现的第三个不完全收缩 grp 算法 thirdGreenWor ...
随机推荐
- Hibernate Hql 总结(2)---laoyang
package com.etc.test; import java.util.Iterator; import java.util.List; import org.hibernate.Query; ...
- word转化jpg
用画图复制粘贴word内容,然后保存为jpg 即可~亲测可行,比截图清洗太多.
- jdk 安装 环境变量配置
右键选择 计算机→属性→高级系统设置→高级→环境变量 1.系统变量→新建 变量名:JAVA_HOME 变量值:(变量值填写你的jdk的安装目录,例如本人是 C:\Program Files\Java\ ...
- NOSDK--一键打包的实现(五)
1.5 mac下的脚本环境配置及脚本的使用 脚本环境主要依赖sdk,ndk和ant,前两个可以在android官方网站下载(被墙了的话只能FQ了). android-sdk-macosx androi ...
- [Linux] Linux常用文本操作命令整理
简单的总结一下常用的一些实用的Linux文本操作命令,包括wc(统计).cut(切分).sort(排序).uniq(去重).grep(查找).sed(替换.插入.删除).awk(文本分析). 1.统计 ...
- Redis实战阅读笔记——开始
Redis实战这本书,看完以后最大的不是redis本身的东西,而是作者面对实际问题的分析而给出的设计方案,可以看成NoSql设计的应用.个人从这方面收获很多,至于Redis本身的东西,这个就花一两个小 ...
- jQuery动画slideUp()不正常位移原因
用jQuery写一个列表.当点击底部按钮时,列表中序号超过6的项目可以向下拉出或者向上收起. 用slideUp(),遇见一个问题.展开列表项会产生不正常位移,如下图所示.动画结束发生位移. 出现这个问 ...
- 通过akRegionCode找到对应的程序
在Oracle EBS中,有一些程序是从上一个版本的Web开发框架(AK)转成OAF的,而这部分程序定义的功能与一般的OAF页面不同,OAF页面使用 OA.jsp?page=/oracle/apps/ ...
- 【bzoj1688】[USACO2005 Open]Disease Manangement 疾病管理
题目描述 Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is running through the farm. Far ...
- 转VS2010解决方案转换到VS2008
原文链接地址:http://www.codeproject.com/Tips/80953/Converting-VS2010-Solution-to-VS2008 如果你使用VS2010的任何版本 ...