搜索 --- 数独求解 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 ...
随机推荐
- 用户管理 之 Linux 用户(user)和用户组(group)管理概述
一.理解Linux的单用户多任务,多用户多任务概念:Linux 是一个多用户.多任务的操作系统:我们应该了解单用户多任务和多用户多任务的概念: 1.Linux 的单用户多任务:单用户多任务:比如我们以 ...
- asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” http:// ...
- 支持取消操作和暂停操作的Backgroundworker
这也是今天讨论的话题.取消是默认就支持的,而暂停则默认不支持.但通过ManualResetEvent可以对其进行干预. using System; using System.Collections.G ...
- Kafka 消息监控 - Kafka Eagle
1.概述 在开发工作当中,消费 Kafka 集群中的消息时,数据的变动是我们所关心的,当业务并不复杂的前提下,我们可以使用 Kafka 提供的命令工具,配合 Zookeeper 客户端工具,可以很方便 ...
- webClient请求JAVA超时解决方案
private class MyWebClient: WebClient { protected override WebRequest GetWebRequest(Uri uri) { WebReq ...
- JS调用WebService
<html> <title>接口测试</title> <meta http-equiv="Content-Type" content=&q ...
- 如何将 Microsoft Bot Framework 链接至微信公共号
说到 Microsoft Bot Framework 其实微软发布了已经有一段时间了,有很多朋友可能还不太了解,微软Bot的功能今天我给大家简单的介绍一下,Bot Framework的开发基础以及如何 ...
- HBase 在HDFS 上的目录树
总所周知,HBase 是天生就是架设在 HDFS 上,在这个分布式文件系统中,HBase 是怎么去构建自己的目录树的呢? 这里只介绍系统级别的目录树. 一.0.94-cdh4.2.1版本 系 ...
- 由于无法创建应用程序域,因此未能执行请求。错误: 0x80070002 系统找不到指定的文件
前两天安装了一堆补丁后突然发现,原本正常使用了一年的应用系统打不开了,到事件日志中发现有.net framewok 2.0的补丁安装失败的日志,于从从重装补丁开始.到重新注册.net框架,再到所有.n ...
- mysql安装过程中出现错误ERROR 1820 (HY000): You must SET PASSWORD before executing this statement解决
mysql安装过程中出现错误ERROR 1820 (HY000): You must SET PASSWORD before executing this statement解决 最近新装好的my ...