题目传送门

题意:数独问题,每行每列以及每块都有1~9的数字

分析:一个一个遍历会很慢。先将0的位子用vector存起来,然后用rflag[i][num] = 1 / 0表示在第i行数字num是否出现过,其他的类似,这样在用DFS就很快了,数据问题,反着搜索会更快。。。

  1. /************************************************
  2. * Author :Running_Time
  3. * Created Time :2015/11/10 星期二 15:43:47
  4. * File Name :POJ_2676.cpp
  5. ************************************************/
  6.  
  7. #include <iostream>
  8. #include <vector>
  9. #include <algorithm>
  10. #include <cstring>
  11. #include <cstdio>
  12. #include <cmath>
  13. using namespace std;
  14.  
  15. #define lson l, mid, rt << 1
  16. #define rson mid + 1, r, rt << 1 | 1
  17. typedef long long ll;
  18. const int N = 1e5 + 10;
  19. const int INF = 0x3f3f3f3f;
  20. const int MOD = 1e9 + 7;
  21. const double EPS = 1e-10;
  22. const double PI = acos (-1.0);
  23. struct Pos {
  24. int x, y;
  25. Pos () {}
  26. Pos (int x, int y) : x (x), y (y) {}
  27. };
  28. vector<Pos> blank;
  29. int mp[11][11];
  30. int rflag[11][11], cflag[11][11], bflag[11][11];
  31.  
  32. int get_id(int x, int y) {
  33. int xx = x / 3;
  34. int yy = y / 3;
  35. return xx * 3 + yy;
  36. }
  37.  
  38. void set_num(int x, int y, int num, int f) {
  39. rflag[x][num] = f;
  40. cflag[y][num] = f;
  41. bflag[get_id (x, y)][num] = f;
  42. }
  43.  
  44. bool ok(int x, int y, int num) {
  45. return !rflag[x][num] && !cflag[y][num] && !bflag[get_id (x, y)][num];
  46. }
  47.  
  48. bool DFS(int cnt) {
  49. if (cnt < 0) return true;
  50. int x = blank[cnt].x, y = blank[cnt].y;
  51. for (int i=1; i<=9; ++i) {
  52. if (!ok (x, y, i)) continue;
  53. mp[x][y] = i;
  54. set_num (x, y, i, 1);
  55. if (DFS (cnt - 1)) return true;
  56. set_num (x, y, i, 0);
  57. }
  58. return false;
  59. }
  60.  
  61. int main(void) {
  62. int T; scanf ("%d", &T);
  63. while (T--) {
  64. for (int i=0; i<9; ++i) {
  65. for (int j=0; j<9; ++j) {
  66. scanf ("%1d", &mp[i][j]);
  67. }
  68. }
  69. blank.clear ();
  70. memset (rflag, 0, sizeof (rflag));
  71. memset (cflag, 0, sizeof (cflag));
  72. memset (bflag, 0, sizeof (bflag));
  73. for (int i=0; i<9; ++i) {
  74. for (int j=0; j<9; ++j) {
  75. if (mp[i][j] == 0) blank.push_back (Pos (i, j));
  76. else {
  77. set_num (i, j, mp[i][j], 1);
  78. }
  79. }
  80. }
  81. if (DFS (blank.size () - 1)) {
  82. for (int i=0; i<9; ++i) {
  83. for (int j=0; j<9; ++j) {
  84. printf ("%d", mp[i][j]);
  85. }
  86. puts ("");
  87. }
  88. }
  89. else puts ("233");
  90. }
  91.  
  92. //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  93.  
  94. return 0;
  95. }

  

DFS POJ 2676 Sudoku的更多相关文章

  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. POJ 2676 Sudoku (数独 DFS)

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

  4. POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]

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

  5. poj 2676 Sudoku ( dfs )

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

  6. POJ 2676 Sudoku (DFS)

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

  7. 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 ...

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

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

  9. POJ 2676 Sudoku

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12005   Accepted: 5984   Special ...

随机推荐

  1. Toast工具类,Android中不用再每次都写烦人的Toast了

    package com.zhanggeng.contact.tools; /** * Toasttool can make you use Toast more easy ; * * @author ...

  2. 总结使用Unity 3D优化游戏运行性能的经验

    原地址:http://www.gameres.com/msg_221889.html 作者:Amir Fasshihi 流畅的游戏玩法来自流畅的帧率,而我们即将推出的动作平台游戏<Shadow ...

  3. Procrustes Analysis普氏分析法

    选取N幅同类目标物体的二维图像,并用上一篇博文的方法标注轮廓点,这样就得到训练样本集: 由于图像中目标物体的形状和位置存在较大偏差,因此所得到的数据并不具有仿射不变性,需要对其进行归一化处理.这里采用 ...

  4. python 的编码问题

    老是碰到这个问题,决定好好给整理一番思路. 翻阅资料和实践证明,以下论述为真理: 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将 ...

  5. SSM框架Web程序的流程(Spring SpringMVC Mybatis)

    SSM框架的Web程序主要用到了三个技术: Spring:用到了注解和自动装配,就是Spring的两个精髓IOC(反向控制)和 AOP(面向切面编程). SpringMVC:用到了MVC模型,将逻辑代 ...

  6. mysql中binary相加的问题

    我在mysql中有这样一段代码 SQL code   ? 1 2 3 4 5 6 7 8 declare @byte1 binary(1) declare @byte2 binary(1) decla ...

  7. C++多态公有继承

    面向对象的三个基本特征 面向对象的三个基本特征是:封装.继承.多态.其中,封装可以隐藏实现细节,使得代码模块化:继承可以扩展已存在的代码模块(类):它们的目的都是为了——代码重用.而多态则是为了实现另 ...

  8. linux 学习一:安装jdk和tomcat

    使用secureCRT 一.首先安装centos的rzsz. 1.yum自动安装:(yum安装rzsz) yum install lrzsz 2.手动安装方法如下:(包有问题,还是采用第一种方式) 2 ...

  9. poj 2136 Vertical Histogram 解题报告

    题目链接:http://poj.org/problem?id=2136 题意不难理解,就是输入四行字符串(每行字符总数不超过72个),统计26个英文字母的数目,并按柱状图的形式输出.我的思路就是,先用 ...

  10. AJAX,JSON用户名校验

    效果 开发结构 1,src部分有两个包dao和servlet 1.1dao包下有两个数据库工具类文件 SqlHelper.java package org.guangsoft.dao; import ...