搜索 --- 数独求解 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 ...
随机推荐
- Conway's Game of Life: An Exercise in WPF, MVVM and C#
This blog post was written for the Lockheed Martin Insight blog, sharing here for the external audie ...
- MapReduce的一点理解
对于MapReduce编程,大概率的流程用过的人或多或少都清楚,但是归结到细节上,就有的地方不清楚了,下面根据自己的疑问,加上从网上各处,找到的被人的描述,最自己的疑问做出回答. 1. MapRedu ...
- WPF调用图片路径,或资源图片
一.加载本项目的图片WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源.其中较为常见的情况是用Uri加载图像.Uri表达式的一般形式为:协议+授权 ...
- mac系统如何关闭root账户
第一步:系统偏好设置 ->用户与群组 第二步:登录选项 ->解锁 ->单击网络帐户服务器加入 第三步:打开目录实用工具 第四步:菜单栏 ->编辑 ->停用 Root 用户 ...
- Struts2返回json
Action怎么返回json类型数据?方法1,使用struts2的插件struts2-json-plugin-2.3.8.jar(在下载的strut2库文件夹中). 在struts2.xml中对要返回 ...
- git第一次提交代码到远程仓库
博客搬家了,本文新地址:http://www.zicheng.net/article/4 感谢支持 本操作说明是先有代码,后来创建git仓库,然后把本地代码提交到远程仓库的操作步骤: 1.初始化 在当 ...
- python-根据左右值获取城市区域编码
def get_adcode(city): """ 根据城市获取区域编码 :param city: :return: """ db = db ...
- C primer plus 练习题 第六章
16. #include <stdio.h> int main() { double remain = 100.0; ; ) { remain = remain * 0.08 + rema ...
- 程序员的复仇:11行代码如何让Node.js社区鸡飞狗跳
来源自:http://www.techug.com/node-js-community 几天前,一名 NPM(Node.js Package Manager)社区的贡献者 Azer Koçulu 出于 ...
- ubuntu系统从中文环境改成英文环境
我们在 安装ubuntu server版的时候,有人可能选择了中文环境安装,因为那样好设置时区等参数,可是安装好了后,运行某些命令的时候会有中文乱码提示,看起很是头蛋疼, 我们就需要将其改成英文环 ...