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 ...
随机推荐
- Toast工具类,Android中不用再每次都写烦人的Toast了
package com.zhanggeng.contact.tools; /** * Toasttool can make you use Toast more easy ; * * @author ...
- 总结使用Unity 3D优化游戏运行性能的经验
原地址:http://www.gameres.com/msg_221889.html 作者:Amir Fasshihi 流畅的游戏玩法来自流畅的帧率,而我们即将推出的动作平台游戏<Shadow ...
- Procrustes Analysis普氏分析法
选取N幅同类目标物体的二维图像,并用上一篇博文的方法标注轮廓点,这样就得到训练样本集: 由于图像中目标物体的形状和位置存在较大偏差,因此所得到的数据并不具有仿射不变性,需要对其进行归一化处理.这里采用 ...
- python 的编码问题
老是碰到这个问题,决定好好给整理一番思路. 翻阅资料和实践证明,以下论述为真理: 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将 ...
- SSM框架Web程序的流程(Spring SpringMVC Mybatis)
SSM框架的Web程序主要用到了三个技术: Spring:用到了注解和自动装配,就是Spring的两个精髓IOC(反向控制)和 AOP(面向切面编程). SpringMVC:用到了MVC模型,将逻辑代 ...
- mysql中binary相加的问题
我在mysql中有这样一段代码 SQL code ? 1 2 3 4 5 6 7 8 declare @byte1 binary(1) declare @byte2 binary(1) decla ...
- C++多态公有继承
面向对象的三个基本特征 面向对象的三个基本特征是:封装.继承.多态.其中,封装可以隐藏实现细节,使得代码模块化:继承可以扩展已存在的代码模块(类):它们的目的都是为了——代码重用.而多态则是为了实现另 ...
- linux 学习一:安装jdk和tomcat
使用secureCRT 一.首先安装centos的rzsz. 1.yum自动安装:(yum安装rzsz) yum install lrzsz 2.手动安装方法如下:(包有问题,还是采用第一种方式) 2 ...
- poj 2136 Vertical Histogram 解题报告
题目链接:http://poj.org/problem?id=2136 题意不难理解,就是输入四行字符串(每行字符总数不超过72个),统计26个英文字母的数目,并按柱状图的形式输出.我的思路就是,先用 ...
- AJAX,JSON用户名校验
效果 开发结构 1,src部分有两个包dao和servlet 1.1dao包下有两个数据库工具类文件 SqlHelper.java package org.guangsoft.dao; import ...