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 ...
随机推荐
- Win 7 下制作 mac 系统启动U盘
Win 7 下制作 mac 系统启动U盘 前几天因为工作需要,在mac 上安装了win7.后来因为习惯问题将win7 分区了,后来就是进不去mac os,只能进入win7 .可恶. 苹果客服说只能用m ...
- (8)UI(控件)
1.按钮: 按钮是游戏中最常用的控件类型之一,控制用户点击事件的开关,有正常.按下.禁用三种状态,您可以为他们设置样式及文本. 使用场景 按钮的使用十分普遍,以官方示例中的主场景示例为例, ...
- Third scrum meeting - 2015/10/28
在一天的工作中明显发现到,无法和网站开发团队进行交流会严重导致我们的进程拖延,所以我们在现有的情况下也把大家的goal初步完成了,我们也对代码规范进行了详细的讨论,以及UI的设计完成,所以整个团队都真 ...
- [codeforces 516]A. Drazil and Factorial
[codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define ...
- nginx学习(一):基本安装
转载自http://summervast.blog.51cto.com/690507/385511 注意:可能因版本不同,个别指令不起作用,需要注意版本灵活安装,我在安装时也遇到过此问题 开始学习ng ...
- linux下复制一个文件的内容到另一个文件
cat path/to/file/filename1 >> path/to/file/filename2 例如: cat id_rsa.pub >> ~/.ssh/author ...
- dubbo作为消费者注册过程分析
请支持原创: http://www.cnblogs.com/donlianli/p/3847676.html 作者当前分析的版本为2.5.x.作者在分析的时候,都是带着疑问去查看代码,debug进 ...
- Django对静态文件的处理——部署阶段
参考:http://blog.makto.me/post/2012-11-09/static-files-in-django-deployment HTML模板中的用法: {% load static ...
- Java for LeetCode 190 Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Java for LeetCode 068 Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters ...