DFS POJ 2676 Sudoku
题意:数独问题,每行每列以及每块都有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的更多相关文章
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14368 Accepted: 7102 Special Judg ...
- POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]
题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...
- poj 2676 Sudoku ( dfs )
dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...
- POJ 2676 Sudoku (DFS)
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11694 Accepted: 5812 Special ...
- 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 ...
- 搜索 --- 数独求解 POJ 2676 Sudoku
Sudoku Problem's Link: http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...
- POJ 2676 Sudoku
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12005 Accepted: 5984 Special ...
随机推荐
- PHP数字格式化,每三位逗号分隔数字,可以保留小数
在报价的时候为了给浏览者更清晰明确的数字,所以需要用到数字格式化,有两种方法,一种自己写函数,另一种当然是系统自带的,其实我更喜欢系统自带的. 先来系统简单的: string number_forma ...
- linux 客户端 Socket 非阻塞connect编程
开发测试环境:虚拟机CentOS,windows网络调试助手 非阻塞模式有3种用途 1.三次握手同时做其他的处理.connect要花一个往返时间完成,从几毫秒的局域网到几百 ...
- [Effective JavaScript 笔记] 第4条:原始类型优于封闭对象
js有5种原始值类型:布尔值.数字.字符串.null和undefined. 用typeof检测一下: typeof true; //"boolean" typeof 2; //&q ...
- unity3d iPhone文件目录介绍
原地址:http://cl314413.blog.163.com/blog/static/190507976201210259126559/ 如何查看iPhone文件存放目录?首先需要越狱,越狱后打开 ...
- linux 搭建hexo博客
搭建环境: CentOS 6.5 1.安装git的编译包 yum -y install gcc zlib-devel openssl-devel perl cpio expat-devel gette ...
- php页面打开响应时间
$start_time = array_sum(explode(' ',microtime())); //your code here $end_time = array_sum(explode( ...
- 破解php-screw加密过的文件有效方法
今天终于搞定更改过密钥的php-screw解密问题,乐呵一下! 改进下 这样就可以解密任何加密过的PHP源码(包括更改过密钥的),解密的原理稍后具体列出,先说下如何加密 列出之前写使用php scre ...
- What is the difference between provider network and self-service network in OpenStack?
"self-service networking" allows users to create their own virtual networks, subnets, rout ...
- chrome控制台支持多行js模式
shift + 回车 是换行 转自: http://zhidao.baidu.com/link?url=MYjGRwvVQYJwnr38VTHPJdzRNtF1COyqpeuAtBYbxFYJcu6p ...
- SQL union和union all的区别
Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...