描述

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 

输入

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

输出

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

样例输入

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

样例输出

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

题目来源

Southeastern Europe 2005

数独规则:横向,纵向,3*3的小方格里,1-9各出现一次。

题意:0的位置填补数,输出一种填法。3*(i/3)+j/3是计算在第几个小方格里。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define LL long long
int a[20][20],flag;
int r[20][20],l[20][20],s[20][20];//r行 l列 s小方阵
void dfs(int i,int j)
{if(i==9&&j==0)flag=1; else{if(a[i][j]!=0){if(j<8)dfs(i,j+1);elsedfs(i+1,0);}else{for(int k=1;k<=9;k++){if(!r[i][k]&&!l[j][k]&&!s[3*(i/3)+j/3][k]){a[i][j]=k;r[i][k]=1;l[j][k]=1;s[3*(i/3)+j/3][k]=1;if(j<8)dfs(i,j+1);elsedfs(i+1,0);if(flag)return;//回溯重置 a[i][j]=0;r[i][k]=0;l[j][k]=0;s[3*(i/3)+j/3][k]=0;}}}}
}
int main()
{int t,i,j;scanf("%d",&t);while(t--){memset(r,0,sizeof r);memset(l,0,sizeof l);       memset(s,0,sizeof s);for(i=0;i<9;i++){for(j=0;j<9;j++){scanf("%1d",&a[i][j]);if(a[i][j]){r[i][a[i][j]]=1;l[j][a[i][j]]=1;s[3*(i/3)+j/3][a[i][j]]=1;}}}flag=0;dfs(0,0);for(i=0;i<9;i++){for(j=0;j<9;j++){printf("%d",a[i][j]);}printf("\n");}}
}

TOJ 3287 Sudoku 9*9数独 dfs相关推荐

  1. TOJ 3287 Sudoku dfs

    3287: Sudoku 描述 Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to ...

  2. POJ-2676 Sudoku(简单数独-dfs深搜)

    Sudoku Time Limit: 2000MS Memory Limit: 65536K 题目链接http://poj.org/problem?id=2676 Description Sudoku ...

  3. leetcode之Valid Sudoku有效的数独(一步步改进代码)

    题目链接:Valid Sudoku有效的数独 题目已经十分确定的说了只有1~9,因此标记法无疑是非常好的选择. 基本思路:对行.列.小数独块分别用一个size为9的数组来标记数字1~9在本行(列/块) ...

  4. POJ 2676 Sudoku【DancingLinks,数独】

    http://poj.org/problem?id=2676 POJ 2676 Sudoku 也是求解规模为9*9的数独问题,与POJ 3074 Sudoku相同的问题,只是修改了输入输出格式而已.. ...

  5. [kuangbin]专题三 Dancing Links Squiggly Sudoku HDU - 4069【DFS】【精确覆盖】

    [题目描述] Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that each ...

  6. leetcode 36. Valid Sudoku | 37. Sudoku Solver(数独)

    36. Valid Sudoku https://leetcode.com/problems/valid-sudoku/ 题解 class Solution {public boolean isVal ...

  7. 数独(DFS优化练习)

    描述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个3*3宫内的数字均含1-9,不重复. 每一道合格的数独谜题都有且 ...

  8. 洛谷 P1784 数独 dfs

    题目描述 数独是根据 9×9 盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含 1 - 9 ,不重复.每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为 ...

  9. LeetCode 36 Valid Sudoku(有效数独)(*)

    翻译 数独板被部分填充,空格部分用'.'来填充.一个部分填充的数组是否有效只需要看其填充的部分即可. 原文 代码 这道题写了一会,错了--因为输入太懒搞了,就直接看了别人写的-- class Solu ...

最新文章

  1. 深度隐式表达:为什么不用mesh,点云,体素等表示方法?
  2. 无冬连接不上账号服务器,电脑中玩无冬online掉线怎么回事_电脑中玩无冬online游戏总是掉线如何修复...
  3. 2021年秋季学期“大数据能力提升项目”证书办理及领取通知
  4. centos 7 yum 安装 python3
  5. mysql 创建用户 %_mysql-创建用户和授权
  6. 如何使用计算机中的导出,如何将iPhone手机中的音乐导出至电脑
  7. 接口之间传递inputstream_接口测试 | 接口测试入门
  8. PUSHAD和POPAD,以及PUSHA和POPA
  9. 前端学习(3276):js中this的使用
  10. ELK学习总结(1-1)ELK是什么
  11. 22_IO_第22天(File、递归)_讲义
  12. layui获取input信息_python爬虫—用selenium爬取京东商品信息
  13. ios 取出数组中前两个元素_看图学NumPy:掌握n维数组基础知识点,看这一篇就够了...
  14. 分布式通信-tcp/ip 单播
  15. getSupportFragmentManager要用在FragmentActivity及其子类中
  16. python怎么连接linux服务器,python连接linux服务器
  17. 如何理解移动数据和移动计算
  18. 用python写菜单_用Python创建菜单
  19. 关于类在3D世界或更加宽广维度中的视角1
  20. 考虑下面两个JSP文件代码片断: test1.jsp:

热门文章

  1. 献给佛山敏捷DevOps志愿者:今天能读到唐诗你知道有多幸运吗?
  2. 删除多余的启动项(Ubuntu)
  3. 数字信号处理翻转课堂笔记6
  4. 一句话解释清楚什么是正向传播和反向传播
  5. iOS 10中不推荐使用UILocalNotification(UILocalNotification is deprecated in iOS 10)
  6. html页面禁止滚动条,css如何不让出现滚动条?
  7. ICA,体素的含义以及核磁扫描的原理
  8. oracle ipcs mt,ipcs命令以及oracle内存段
  9. Macronix MX30LF1G18AC替换Spansion S34ML01G200参考!
  10. 移动端开启调试器 手机上也能看到console