搜索 --- 数独求解 POJ 2676 Sudoku
Sudoku
Problem's Link: http://poj.org/problem?id=2676
Mean:
略
analyse:
记录所有空位置,判断当前空位置是否可以填某个数,然后直接DFS,注意从后往前搜索,时间比正向搜快很多。16ms水过
Time complexity: O(n)
Source code:
// Memory Time
// 1347K 0MS
// by : crazyacking
// 2015-04-10-14.30
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#define MAXN 1000010
#define LL long long
using namespace std;
struct Node
{
int x,y;
};
Node b[];
int g[][];
int idx;
bool flag;
void input()
{
char s[];
idx=;
flag=;
for(int i=;i<;++i)
{
scanf("%s",s);
for(int j=;j<;++j)
{
g[i][j]=s[j]-'';
if(g[i][j]==)
{
b[idx].x=i;
b[idx].y=j;
idx++;
}
}
}
idx--;
} bool Check(int x,int y,int num)
{
for(int i=;i<;++i)
{
if(g[x][i]==num) return false;
if(g[i][y]==num) return false;
}
int sta_x=x/*;
int sta_y=y/*;
for(int i=sta_x;i<sta_x+;++i)
{
for(int j=sta_y;j<sta_y+;++j)
{
if(g[i][j]==num) return false;
}
}
return true;
}
void DFS(int n)
{
if(n==-)
{
flag=;
return ;
}
int x=b[n].x;
int y=b[n].y;
for(int i=;i<=;++i)
{
if(Check(x,y,i))
{
g[x][y]=i;
DFS(n-);
if(flag) return ;
g[x][y]=;
}
}
return;
}
void output()
{
for(int i=;i<;++i)
{
for(int j=;j<;++j)
{
printf("%d",g[i][j]);
}
puts("");
}
}
int main()
{
int Cas;
scanf("%d",&Cas);
while(Cas--)
{
input();
DFS(idx);
output();
}
return ;
}
搜索 --- 数独求解 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神奇的反搜
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 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12005 Accepted: 5984 Special ...
- DFS POJ 2676 Sudoku
题目传送门 题意:数独问题,每行每列以及每块都有1~9的数字 分析:一个一个遍历会很慢.先将0的位子用vector存起来,然后用rflag[i][num] = 1 / 0表示在第i行数字num是否出现 ...
- poj 2676 Sudoku ( dfs )
dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...
- POJ 2676 Sudoku(深搜)
Sudoku Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submi ...
随机推荐
- 定时从远程的数据库中取数据,然后把取出来的数据插入或更新本地的oracle数据库的表
最近项目中有一种需求: 大致需求是这样的 通过给定的 用户名和密码 要定时从远程的数据库中取数据,然后把取出来的数据插入或更新本地的oracle数据库的表 项目的结构式struts1 hibernat ...
- 【html】button按钮的一些问题
问题: button 按钮在不设置 type 属性时,在不同的浏览器作用不一样.举个例子: html: <!doctype html> <html lang="en&quo ...
- Intellij IDEA IDE部署Servlet项目
1.设置Project Structure 2.修改Modules中的Web项目文件默认class编译之后输出位置 3.给Modules中的Web项目添加Web模块 4.修改Web项目Web.xml文 ...
- Linux流量监控工具-iftop教程
Linux流量监控工具-iftop教程http://automationqa.com/forum.php?mod=viewthread&tid=2854&fromuid=2
- Base64的编码转换方式
下面,详细介绍Base64的编码转换方式. 所谓Base64,就是说选出64个字符----小写字母a-z.大写字母A-Z.数字0-9.符号"+"."/"(再加上 ...
- 将数据库表导入到solr索引
将数据库表导入到solr索引 编辑solrcofnig.xml添加处理器 <requestHandler name="/dataimport" class="org ...
- LevelDB(v1.3) 源码阅读之 Arena(内存管理器)
LevelDB(v1.3) 源码阅读系列使用 LevelDB v1.3 版本的代码,可以通过如下方式下载并切换到 v1.3 版本的代码: $ git clone https://github.com/ ...
- Ubuntu 中搭建 LAMP 及 php 开发工具
所谓 LAMP,指的是:Linux+Apache+Mysql+Php 仅以此文做一个备忘录 Step1. 安装 Apache 1. 在 terminal 中输入一下命令并执行: sudo apt-ge ...
- 使用UIKit制作卡牌游戏(一)ios游戏篇
转自朋友Tommy 的翻译,自己只翻译了第三篇教程. 译者: Tommy | 原文作者: Matthijs Hollemans写于2012/06/29 原文地址: http://www.raywend ...
- CENTOS 基础指令——查看系统环境
1.查看内核版本 # cat /proc/version # uname -a # uname -r 2.查看linux版本 # cat /etc/issue # cat /etc/redhat-re ...