题目传送门

题意:数独问题,每行每列以及每块都有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. [BZOJ3670][UOJ#5][NOI2014]动物园

    [BZOJ3670][UOJ#5][NOI2014]动物园 试题描述 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学 ...

  2. myeclipse2014破解过程

    之前装的是10,后来没事试试装了2014,然后再破解2014后发现2010的证书就失效了,之前在网上也没找到方法,这段时间也没管,今天又自己想办法试了试,发现成功了!下边是我在网上找的破解方法的破解步 ...

  3. hibernate.cfg.xml配置文件和hbm.xml配置文件 模板

    hibernate.cfg.xml配置文件格式 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE ...

  4. encode与decode,unicode与中文乱码的问题

    encode是指将unicode字符编码成其他字符集的字符,如utf-8,ascii等: 而decode是指将其他字符编码,如utf-8转换成unicode编码. encode是指将人类用的语言(字符 ...

  5. php如何将数组保存为文件的方法? 三个方法让你快速把数组保存成为文件存储

    php 缓存数组形式的变量,实际上就是将 php 将数组写入到一个文本文件或者后缀名为 .php 存储起来,使用的时候直接调用这个文件.那么如何使用 php 将数组保存为文本格式的文件呢?下面分享三种 ...

  6. 《C#高级编程》学习笔记------C#中的事件和委托

    本文转载自张子阳 目录 委托的作用 将方法绑定到委托 事件的来由 Observer设计模式 .Net Framework中的委托与事件   引言 委托 和 事件在 .Net Framework中的应用 ...

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

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

  8. TokuDB的特点验证

    随着数据量越来越大,越来越频繁的遇到需要进行结构拆分的情况,每一次拆分都耗时很久,并且需要多方配合,非常的不想搞这个事情.于是在@zolker的提醒下想到了13年开源tokuDB,来解决我们迫在眉睫的 ...

  9. codeforces 483C.Diverse Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/C 题目意思:给出 n 和 k,要求输出一个含有 n 个数的排列 p1, p2, ...,pn,使得 ...

  10. fedora yum 使用代理的方法

    配置yum: 编辑/etc/yum.conf添加下列一行: proxy=http://domain/user:passwd@<proxy ip>:80 <proxy ip>:代 ...