#include <bits/stdc++.h>
using namespace std;
const int maxn = 10;
bool maps[maxn][maxn], row[maxn][maxn], col[maxn][maxn];
int mat[maxn][maxn];
void print() {
for (int i = 1; i <= 9; ++i) {
for (int j = 1; j <= 9; ++j) {
if (j != 1) cout << " ";
cout << mat[i][j];
}
cout << endl;
}
}
void dfs(int x, int y) {
if (x > 9) {
print();
exit(0);
}
if (mat[x][y] != 0) {
if (y == 9) dfs(x+1,1);
else dfs(x,y+1);
}
else {
for (int i = 1; i <= 9; ++i) {
int k = (x-1)/3*3 + (y-1)/3 + 1;
// 如果i这个数字没有被使用过
if (!maps[k][i] && !row[x][i] && !col[y][i]) {
mat[x][y] = i;
maps[k][i] = row[x][i] = col[y][i] = true;
if (y == 9) dfs(x+1,1);
else dfs(x,y+1);
mat[x][y] = 0;
maps[k][i] = row[x][i] = col[y][i] = false;
}
}
}
}
int main() {
freopen("input.txt","r",stdin);
for (int i = 1; i <= 9; ++i) {
for (int j = 1; j <= 9; ++j) {
cin >> mat[i][j];
if (mat[i][j] == 0) continue; row[i][mat[i][j]] = true; // 第i行mat[i][j]被使用
col[j][mat[i][j]] = true; // 第j列mat[i][j]被使用
int k = (i-1)/3*3 + (j-1)/3 + 1;
maps[k][mat[i][j]] = true; // 第k格mat[i][j]被使用
}
}
cout << "原数独:" << endl;
print();
cout << "输出数独:" << endl;
dfs(1,1);
return 0;
}

数独c++的更多相关文章

  1. LintCode389.判断数独是否合法

    LintCode简单题:判断数独是否合法 问题描述: 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 注意事项: 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...

  2. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  4. 数独 JAVA实现

    数独游戏的规则从很久之前就知道,但是一直都没怎么玩过,然后到了大学,大一下学期自己学dfs的时候,刚刚好碰到了一个数独的题目,做出来后,感觉还是挺有成就感的 然后大二学了JAVA,看了下那个一些有关于 ...

  5. 用C++实现的解数独(Sudoku)程序

    我是一个C++初学者,控制台实现了一个解数独的小程序. 代码如下: //"数独游戏"V1.0 //李国良于2016年11月11日编写完成 #include <iostream ...

  6. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  7. ACM: ICPC/CCPC Sudoku DFS - 数独

    Sudoku Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Submis ...

  8. codevs 2924 数独挑战

    2924 数独挑战 http://codevs.cn/problem/2924/ 题目描述 Description "芬兰数学家因卡拉,花费3个月时间设计出了世界上迄今难度最大的数独游戏,而 ...

  9. 用html5 canvas和JS写个数独游戏

    为啥要写这个游戏? 因为我儿子二年级数字下册最后一章讲到了数独.他想玩儿. 因为我也想玩有提示功能的数独. 因为我也正想决定要把HTML5和JS搞搞熟.熟悉一个编程平台,最好的办法,就是了解其原理与思 ...

  10. Sudoku 数独游戏

    #include<iostream> using namespace std; bool heng(int **sudo, int a, int b, int value) { bool ...

随机推荐

  1. js前端加密,php后端解密(crypto-js,openssl_decrypt)

    来源:https://blog.csdn.net/morninghapppy/article/details/79044026 案例:https://blog.csdn.net/zhihua_w/ar ...

  2. SweetAlert - 演示6种不同的提示框效果

    http://www.sucaihuo.com/js/190.html http://www.cnblogs.com/beiz/p/5238124.html

  3. tp5--model的坑

    先上代码: class Article extends Model { //获取全部文章 public function getArticleAll($id,$page) { $cate = new ...

  4. tcp的重传与超时

    TCP协议是一种面向连接的可靠的传输层协议,它保证了数据的可靠传输,对于一些出错,超时丢包等问题TCP设计的超时与重传机制. 其基本原理:在发送一个数据之后,就开启一个定时器,若是在这个时间内没有收到 ...

  5. [Qt] 打开Diskmgmt

    QProcess mOpenDiskMgmt; QString program = "cmd"; QStringList arguments; arguments << ...

  6. 讲讲python中函数的参数

    python中函数的参数 形参:定义函数时代表函数的形式参数 实参:调用函数时传入的实际参数 列如: def f(x,y): # x,y形参 print(x, y) f(1, 2) # 1, 2 实参 ...

  7. java stream中Collectors的用法

    目录 简介 Collectors.toList() Collectors.toSet() Collectors.toCollection() Collectors.toMap() Collectors ...

  8. Spring Cloud sleuth with zipkin over RabbitMQ教程

    文章目录 Spring Cloud sleuth with zipkin over RabbitMQ demo zipkin server的搭建(基于mysql和rabbitMQ) 客户端环境的依赖 ...

  9. Vue 结合 echarts 原生 html5 实现拖拽排版报表系统

    前言 不知道各位 coder 有没有碰到过许多重复的业务需求,比如排版相类似的报表,只不过是顺序稍微换了一下,就是一个新的页面,虽然基于模板思想来写的话也能减少不少代码,但是相对的不那么方便,笔者最近 ...

  10. 高性能服务器开发基础系列 (二)Reactor模式

    系列目录 第01篇 主线程与工作线程的分工 第02篇 Reactor模式 第03篇 一个服务器程序的架构介绍 第04篇 如何将socket设置为非阻塞模式 第05篇 如何编写高性能日志 第06篇 关于 ...