题目链接:http://poj.org/problem?id=2676

Time Limit: 2000MS Memory Limit: 65536K

Description

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. 

Input

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.

Output

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.

Sample Input

  1. 1
  2. 103000509
  3. 002109400
  4. 000704000
  5. 300502006
  6. 060000050
  7. 700803004
  8. 000401000
  9. 009205800
  10. 804000107

Sample Output

  1. 143628579
  2. 572139468
  3. 986754231
  4. 391542786
  5. 468917352
  6. 725863914
  7. 237481695
  8. 619275843
  9. 854396127

题意:

你一定听说过“数独”游戏。
如【图1.png】,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个同色九宫内的数字均含1-9,不重复。

数独的答案都是唯一的,所以,多个解也称为无解。

本图的数字据说是芬兰数学家花了3个月的时间设计出来的较难的题目。但对会使用计算机编程的你来说,恐怕易如反掌了。

本题的要求就是输入数独题目,程序输出数独的唯一解。我们保证所有已知数据的格式都是合法的,并且题目有唯一的解。

格式要求:
输入9行,每行9个数字,0代表未知,其它数字为已知。
输出9行,每行9个数字表示数独的解。

题解:

从[1,1]到[9,9]地进行DFS

AC代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. int num[][];
  6. bool rowVis[][],colVis[][],matVis[][];
  7.  
  8. struct Pos{
  9. int row,col;
  10. Pos nextpos()
  11. {
  12. if(col==) return (Pos){row+,};
  13. else return (Pos){row,col+};
  14. }
  15. int MatID(){return (row-)/* + (col-)/+;}
  16. bool ok(int x)
  17. {
  18. if(!rowVis[row][x] && !colVis[col][x] && !matVis[MatID()][x]) return ;
  19. else return ;
  20. }
  21. };
  22.  
  23. void mark(Pos pos,int x,bool val)
  24. {
  25. rowVis[pos.row][x] = val;
  26. colVis[pos.col][x] = val;
  27. matVis[pos.MatID()][x] = val;
  28. }
  29.  
  30. bool dfs(Pos pos)
  31. {
  32. int row=pos.row,col=pos.col;
  33.  
  34. if(row==) return ;
  35. if(num[row][col]) return dfs(pos.nextpos());
  36.  
  37. for(int i=;i<=;i++)
  38. {
  39. if(pos.ok(i))
  40. {
  41. num[row][col]=i; mark(pos,i,);
  42. if(dfs(pos.nextpos())) return ;
  43. num[row][col]=; mark(pos,i,);
  44. }
  45. }
  46. return ;
  47. }
  48.  
  49. int main()
  50. {
  51. int t;
  52. scanf("%d",&t);
  53. while(t--)
  54. {
  55. memset(rowVis,,sizeof(rowVis));
  56. memset(colVis,,sizeof(colVis));
  57. memset(matVis,,sizeof(matVis));
  58. for(int row=;row<=;row++)
  59. {
  60. char tmp[]; scanf("%s",tmp);
  61. for(int col=;col<=;col++)
  62. {
  63. num[row][col]=tmp[col-]-'';
  64. if(num[row][col]) mark((Pos){row,col},num[row][col],);
  65. }
  66. }
  67.  
  68. dfs((Pos){,});
  69.  
  70. for(int row=;row<=;row++)
  71. {
  72. for(int col=;col<=;col++) printf("%d",num[row][col]);
  73. printf("\n");
  74. }
  75. }
  76. }

POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]的更多相关文章

  1. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  2. ACM : POJ 2676 SudoKu DFS - 数独

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

  3. 蓝桥杯---数独(模拟 || dfs)

    [编程题](满分33分) "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数 学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6 ...

  4. 蓝桥杯 带分数 DFS应用

    问题描述 100 可以表示为带分数的形式:100 = 3 + 69258 / 714. 还可以表示为:100 = 82 + 3546 / 197. 注意特征:带分数中,数字1~9分别出现且只出现一次( ...

  5. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  6. POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜

    Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...

  7. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

  8. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  9. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

随机推荐

  1. Linux+Redis实战教程_Linux上安装jdk,mysql,tomcat_安装jdk

    1. Linux上安装jdk,mysql,tomcat[重点] Windows 控制面板 添加/卸载程序 进行程序的安装.更新.卸载.查看 rpm命令:相当于windows的添加/卸载程序 进行程序的 ...

  2. ios开发之--AVAudioPlayer/AVPlayer的应用

    项目当中用到了音频播放器,所以就参考官方文档,写了一个,代码如下: .h #import <UIKit/UIKit.h> @interface hAudioPlayViewControll ...

  3. java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"

    一.问题 使用SpringMVC和MyBatis整合,将jdbc配置隔离出来的时候出现下面的错误,百度了很久没有找到解决方法,回家谷歌下,就找到解决方法了,不得不说谷歌就是强大,不废话,下面是具体的错 ...

  4. 如何构建日均千万PV Web站点 (一)

    其实大多数互联网网站起初的网站架构都是(Linux+Apache+MySQL+PHP). 不过随着时代的发展,科技的进步.互联网进入寻常百姓家的生活.所谓的用户的需求,铸就了一个个互联网大牛: htt ...

  5. PHP中$_SERVER的详细用法

    PHP中$_SERVER的详细用法 $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关. $_SERVER['argv'] #传递给该脚本的参数. ...

  6. 【PHP】常见算法

    1 冒泡排序 思路:在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒. 即,每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它 ...

  7. 【Linux】 解决报错: ImportError: libSM.so.6: cannot open shared object file: No such file or directory

    centos7 +  python3.6.4 我使用 pip3 install opencv-python 安装了opencv-python  之后,在使用 import cv2  报错如下 报错原因 ...

  8. 【python3】基于 qq邮箱的邮件发送

    脚本内容: #!/usr/bin/python3 # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText ...

  9. RAID在数据库存储上的应用

    随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...

  10. 【大数据系列】MapReduce详解

    MapReduce是hadoop中的一个计算框架,用来处理大数据.所谓大数据处理,即以价值为导向,对大数据加工,挖掘和优化等各种处理. MapReduce擅长处理大数据,这是由MapReduce的设计 ...