#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. git在用https进行push时候免输账密的方法

    先新建一个文件 $ touch ~/.git-credentials $ vim ~/.git-credentials 进去添加内容(github为github.com,码云为gitee.com) h ...

  2. DZ的CURD

    Discuz二次开发-MySQL插入数据(insert) DB::insert($tableName,$data,$flag); $tableName:表名 $data:插入数据,以字段为键值的关联数 ...

  3. tagbar 调到函数定义再跳回

    首先要在源码文件夹下执行 ctags -R * 生成tags文件 齐次要安装 YouCompleteMe ctrl + ] 跳到函数定义 Ctrl-o 和 Ctrl-I 跳回.我试验的只有 Ctrl- ...

  4. 设定程序随windows启动

    Boot Trigger Example (C++) /******************************************************************** Thi ...

  5. JVM原理以及深度调优(二)

    JVM内存分配 内存分配其实真正来讲是有三种的.但对于JVM来说只有两种 栈内存分配: 大家在调优的过程中会发现有个参数是-Xss 默认是1m,这个内存是栈内存分配, 在工作中会发现栈OutOfMem ...

  6. 【JAVA基础】05 Java语言基础:数组

    1. 数组概述和定义格式说明 为什么要有数组(容器) 为了存储同种数据类型的多个值 数组概念 数组是存储同一种数据类型多个元素的集合.也可以看成是一个容器. 数组既可以存储基本数据类型,也可以存储引用 ...

  7. 【linux运维】rsync+inotify与sersync+rsync实时数据同步笔记

    Rsync(remote sync)远程同步工具,通过rsync可以实现对远程服务器数据的增量备份通过,但rsync自身也有缺陷,同步数据时,rsync采用核心算法对远程服务器的目标文件进行对比,只进 ...

  8. vue-cli3.0 gui初体验

    为什么80%的码农都做不了架构师?>>>   介绍 新版的vuecli3.0提供了一个vue ui这个命令,这个命令是做什么的呢,这里引用官网的一段介绍 vue ui 你可以通过 v ...

  9. Codeforces Round #460 (Div. 2)-A Supermaket(贪心)

    A. Supermarket time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  10. 数学--数论--HDU 1792 A New Change Problem (GCD+打表找规律)

    Problem Description Now given two kinds of coins A and B,which satisfy that GCD(A,B)=1.Here you can ...