题目传送门

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

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

/************************************************
* Author :Running_Time
* Created Time :2015/11/10 星期二 15:43:47
* File Name :POJ_2676.cpp
************************************************/ #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
struct Pos {
int x, y;
Pos () {}
Pos (int x, int y) : x (x), y (y) {}
};
vector<Pos> blank;
int mp[11][11];
int rflag[11][11], cflag[11][11], bflag[11][11]; int get_id(int x, int y) {
int xx = x / 3;
int yy = y / 3;
return xx * 3 + yy;
} void set_num(int x, int y, int num, int f) {
rflag[x][num] = f;
cflag[y][num] = f;
bflag[get_id (x, y)][num] = f;
} bool ok(int x, int y, int num) {
return !rflag[x][num] && !cflag[y][num] && !bflag[get_id (x, y)][num];
} bool DFS(int cnt) {
if (cnt < 0) return true;
int x = blank[cnt].x, y = blank[cnt].y;
for (int i=1; i<=9; ++i) {
if (!ok (x, y, i)) continue;
mp[x][y] = i;
set_num (x, y, i, 1);
if (DFS (cnt - 1)) return true;
set_num (x, y, i, 0);
}
return false;
} int main(void) {
int T; scanf ("%d", &T);
while (T--) {
for (int i=0; i<9; ++i) {
for (int j=0; j<9; ++j) {
scanf ("%1d", &mp[i][j]);
}
}
blank.clear ();
memset (rflag, 0, sizeof (rflag));
memset (cflag, 0, sizeof (cflag));
memset (bflag, 0, sizeof (bflag));
for (int i=0; i<9; ++i) {
for (int j=0; j<9; ++j) {
if (mp[i][j] == 0) blank.push_back (Pos (i, j));
else {
set_num (i, j, mp[i][j], 1);
}
}
}
if (DFS (blank.size () - 1)) {
for (int i=0; i<9; ++i) {
for (int j=0; j<9; ++j) {
printf ("%d", mp[i][j]);
}
puts ("");
}
}
else puts ("233");
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}

  

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. 最近win7更新后出现第二次打开IDE(delphi2007)的时候提示无法打开"EditorLineEnds.ttr"这个文件

    kb2982791 - 2014年8月12日更新 - http://support.microsoft.com/kb/2982791kb2970228 - 2014年8月12日更新 - http:// ...

  2. PHP 遍历数组的方法汇总

    1. foreach() foreach()是一个用来遍历数组中数据的最简单有效的方法. #example1: <?php $colors= array('red','blue','green' ...

  3. TCP/IP详解学习笔记(5)-- ICMP:internet 控制报文协议

    1.概述      ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制 ...

  4. java类的封装 继承 多态

    1.猜数字小游戏 package cn.jiemoxiaodi_02; import java.util.Scanner; /** * 猜数字小游戏 * * @author huli * */ pub ...

  5. 百度编辑器ueditor每次编辑后多一个空行的解决办法

    用ueditor进行编辑文章时,每次编辑后文章前面都会多出一个空行. <script id="editor" type="text/plain" styl ...

  6. Android 相机对焦模式

    Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE  Camer ...

  7. windows下安装Appserv等php套件之后无法进入数据库管理的问题

    在win7下安装Wamp或者Appserv后无法进入数据库管理,但是php.Apache运行全都没问题,mysql可以在命令行中管理,但是就是无法打开phpmyadmin数据库管理,点击后浏览器就显示 ...

  8. python chm 中文帮助 (2.7 和 3.4)

    sphinx-build 生成的(htmlhelp) 存在2个问题 1.生成的html 编码 cp2152,需要修改/sphinx/builders/html.py ctx['encoding'] = ...

  9. elk平台分析nginx日志的基本搭建

    一.elk套件介绍 ELK 由 ElasticSearch . Logstash 和 Kiabana 三个开源工具组成.官方网站: https://www.elastic.co/products El ...

  10. Greedy:Fence Repair(POJ 3252)

    Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...