Sudoku Killer

Problem Description

自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视。

据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会。

所以全球人民前仆后继,为了奖品日夜训练茶饭不思。当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的。



数独游戏的规则是这样的:在一个9x9的方格中,你需要把数字1-9填写到空格当中,并且使方格的每一行和每一列中都包含1-9这九个数字。同时还要保证,空格中用粗线划分成9个3x3的方格也同时包含1-9这九个数字。比如有这样一个题,大家可以仔细观察一下,在这里面每行、每列,以及每个3x3的方格都包含1-9这九个数字。



例题:





答案:


Input

本题包含多组测试,每组之间由一个空行隔开。每组测试会给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开。其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数。

Output

对于每组测试,请输出它的解,同一行相邻的两个数用一个空格分开。两组解之间要一个空行。

对于每组测试数据保证它有且只有一个解。

Sample Input

  1. 7 1 2 ? 6 ? 3 5 8
  2. ? 6 5 2 ? 7 1 ? 4
  3. ? ? 8 5 1 3 6 7 2
  4. 9 2 4 ? 5 6 ? 3 7
  5. 5 ? 6 ? ? ? 2 4 1
  6. 1 ? 3 7 2 ? 9 ? 5
  7. ? ? 1 9 7 5 4 8 6
  8. 6 ? 7 8 3 ? 5 1 9
  9. 8 5 9 ? 4 ? ? 2 3

Sample Output

  1. 7 1 2 4 6 9 3 5 8
  2. 3 6 5 2 8 7 1 9 4
  3. 4 9 8 5 1 3 6 7 2
  4. 9 2 4 1 5 6 8 3 7
  5. 5 7 6 3 9 8 2 4 1
  6. 1 8 3 7 2 4 9 6 5
  7. 2 3 1 9 7 5 4 8 6
  8. 6 4 7 8 3 2 5 1 9
  9. 8 5 9 6 4 1 7 2 3

——————————————————————————————————————————————————————————

题目是在?位置填上空格完成成数独,将?位置保存在一个结构体里处理。
注意9宫判重如下
  1. int xx = (x / 3) * 3;
  2. int yy = (y / 3) * 3;
  3. for (int i = xx; i<xx + 3; i++)
  4. for (int j = yy; j<yy + 3; j++)
  5. {
  6. if (mp[i][j] == a)
  7. return 0;
  8. }




  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<queue>
  6. #include<vector>
  7.  
  8. using namespace std;
  9.  
  10. int mp[12][12];
  11. int tot, cnt,flag,q;
  12. struct node
  13. {
  14. int x, y;
  15. }pl[100];
  16.  
  17. bool cheak(int x, int y, int a)
  18. {
  19. for (int i = 0; i<10; i++)
  20. {
  21. if (mp[i][y] == a)
  22. return 0;
  23. }
  24. for (int i = 0; i<10; i++)
  25. {
  26. if (mp[x][i] == a)
  27. return 0;
  28. }
  29. int xx = (x / 3) * 3;
  30. int yy = (y / 3) * 3;
  31. for (int i = xx; i<xx + 3; i++)
  32. for (int j = yy; j<yy + 3; j++)
  33. {
  34. if (mp[i][j] == a)
  35. return 0;
  36. }
  37. return 1;
  38. }
  39.  
  40. void print()
  41. {
  42. if (q++)
  43. printf("\n");
  44. for (int i = 0; i<9; i++)
  45. {
  46. for (int j = 0; j<8; j++)
  47. {
  48. printf("%d ", mp[i][j]);
  49. }
  50. printf("%d\n",mp[i][8]);
  51. }
  52. }
  53.  
  54. void dfs(int cnt)
  55. {
  56. int xx, yy;
  57. if (flag == 1)
  58. return;
  59. if (cnt == tot)
  60. {
  61. flag = 1;
  62. print();
  63. return;
  64. }
  65. for (int i = 1; i < 10; i++)
  66. {
  67. if (cheak(pl[cnt].x, pl[cnt].y, i))
  68. {
  69. mp[pl[cnt].x][pl[cnt].y] = i;
  70. dfs(cnt+1);
  71. mp[pl[cnt].x][pl[cnt].y] =0;
  72. }
  73. }
  74. }
  75.  
  76. int main()
  77. {
  78. char ch;
  79. q = 0;
  80. while (scanf("%s",&ch)!=EOF)
  81. {
  82. tot = 0;
  83. if (ch == '?')
  84. {
  85. mp[0][0] = 0;
  86. pl[tot].x = 0;
  87. pl[tot++].y = 0;
  88.  
  89. }
  90. else
  91. mp[0][0] = ch - '0';
  92. for (int i = 0; i < 9; i++)
  93. for (int j = 0; j < 9; j++)
  94. {
  95. if (i == 0 && j == 0)
  96. continue;
  97. scanf("%s",&ch);
  98. if (ch == '?')
  99. {
  100. mp[i][j] = 0;
  101. pl[tot].x = i;
  102. pl[tot++].y = j;
  103. }
  104. else
  105. mp[i][j] = ch - '0';
  106.  
  107. }
  108. cnt = 0;
  109. flag = 0;
  110. dfs(0);
  111. }
  112. return 0;
  113. }


HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏的更多相关文章

  1. HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏

    FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...

  2. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

    A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...

  3. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU1501 Zipper(DFS) 2016-07-24 15:04 65人阅读 评论(0) 收藏

    Zipper Problem Description Given three strings, you are to determine whether the third string can be ...

  5. HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏

    Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...

  6. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  7. Hdu1427 速算24点 2017-01-18 17:26 46人阅读 评论(0) 收藏

    速算24点 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  8. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  9. Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. 【337】Text Mining Using Twitter Streaming API and Python

    Reference: An Introduction to Text Mining using Twitter Streaming API and Python Reference: How to R ...

  2. jsp常见的指令总结

    一.三个编译指令 1.page指令: 首先,我们要明确一点就是page指令是一个全局指令,针对当前页面,其次我们再来深挖他的功能,它到底有哪些功能那,在我们程序中起到什么作用??? a.语法结构:&l ...

  3. Dotfuscator Professional Edition获取代码发布和混淆代码

    1 Dotfuscator Professional Edition 4.9 破解版 下载地址:http://www.pc0359.cn/downinfo/39815.html 备份地址:C:\D\9 ...

  4. Mysql count(1) group_concat 高级用法(count 过滤条件,group_concat过滤条件)

    1.官方文档: count:COUNT(expr) [over_clause] https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.h ...

  5. Java.sql.SQLException: 无效的列类型: 1111

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: ...

  6. c++泛型模板

    模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据成员或者成员函数的参数.返回值取得任意类型. 模板是一种对类型进行参数化的工具: 通常有两种形式:函 ...

  7. Volley下载图片存放在data/data下 networkImageView lrucache

    networkImageView 设置图片的方法  (有效) imageView.setImageUrl("https://www.baidu.com/img/bd_logo1.png&qu ...

  8. maven的配置及仓库的配置

    1.maven的配置 1.1.注意:电脑上需要安装jdk. 1.2.配置MAVEN_HOME,再在path中配置到bin这一层. (1)配置MAVEN_HOME:我的电脑--->右击---> ...

  9. 使用springmvc从页面中获取数据,然后根据获得的参数信息进行修改,如果修改的数据中含有不是基本数据类型的参数。比如传的参数中有Date类型的数据时,需要我们进行参数类型转换。

    1.1 需求 在商品修改页面可以修改商品的生产日期,并且根据业务需求自定义日期格式. 1.2 需求分析 由于日期数据有很多格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑 ...

  10. Underscore模版引擎的使用-template方法

    之前项目里有遇到在DOM中增加大量的html结构的时候,傻乎乎的在js中写一堆模版,然后用replace一个一个做替换.当时就是难看了点,不觉得啥,现在了解了模版引擎之后回头来看真的比较捉急了,以后是 ...