HDU 5547 Sudoku(DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5547
题目:
Sudoku
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2372 Accepted Submission(s): 804
Actually, Yi Sima was playing it different. First of all, he tried to generate a 4×4 board with every row contains 1 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four 2×2 pieces, every piece contains 1 to 4.
Then, he removed several numbers from the board and gave it to another guy to recover it. As other counselors are not as smart as Yi Sima, Yi Sima always made sure that the board only has one way to recover.
Actually, you are seeing this because you've passed through to the Three-Kingdom Age. You can recover the board to make Yi Sima happy and be promoted. Go and do it!!!
It's guaranteed that there will be exactly one way to recover the board.
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
struct node{
int r,c;
};
vector<node>v;
char chess[][];
int col[][];//col[i][x]标记第i列 x数是否已经用过
int row[][];//row[i][x]标记第i行 x数是否已经用过
int block[][];//block[i][x]标记第i个分块,x数是否已经用过
int ok;
void dfs(int num){
if(ok) return ;
if(num==v.size()){//所有数都填好
for (int i=; i<; i++) {
puts(chess[i]);
}
ok=;
return ;
}
for(int i=;i<=;i++){
int r=v[num].r;
int c=v[num].c;
if(!col[c][i] && !row[r][i] && !block[r/*+c/][i]){
chess[r][c]=i+'';
col[c][i]=row[r][i]=;
block[r/*+c/][i]=;
dfs(num+);
col[c][i]=row[r][i]=;//回溯还原状态
block[r/*+c/][i]=;
chess[r][c]='*';
}
}
}
int main(){
int t;
scanf("%d",&t);
for (int i=; i<=t; i++) {
printf("Case #%d:\n",i);
v.clear();
memset(block, , sizeof(block));//初始值都为0,即都未用过
memset(col, , sizeof(col));
memset(row, , sizeof(row));
ok=;
for (int j=; j<; j++) {
scanf("%s",chess[j]);
for (int k=; k<; k++) {
if(chess[j][k]=='*'){//将需要填空的点放入vector容器
node x;
x.r=j;x.c=k;
v.push_back(x);
}else{
int x=chess[j][k]-'';//标记已经用过的点
block[j/*+k/][x]=;
row[j][x]=;
col[k][x]=;
}
}
}
dfs();//0表示在填空v[0]点
}
return ;
}
HDU 5547 Sudoku(DFS)的更多相关文章
- HDU - 5547 Sudoku(数独搜索)
Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself ...
- HDU 5547 Sudoku (暴力)
题意:数独. 析:由于只是4*4,完全可以暴力,要注意一下一些条件,比如2*2的小方格也得是1234 代码如下: #pragma comment(linker, "/STACK:102400 ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1426 Sudoku Killer(dfs 解数独)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Oth ...
- hdu 1426 Sudoku Killer (dfs)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547
Sudoku Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- (hdu)5547 Sudoku (4*4方格的 数独 深搜)
Problem Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game ...
- HDU 1426 Sudoku Killer【DFS 数独】
自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品— ...
随机推荐
- 【Python的迭代器,生成器】
一.可迭代对象和迭代器 1.迭代的概念 上一次输出的结果为下一次输入的初始值,重复的过程称为迭代,每次重复即一次迭代,并且每次迭代的结果是下一次迭代的初始值 注:循环不是迭代 while True: ...
- STL语法——映射:map 反片语(Ananagrams,UVa 156)
Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters i ...
- nginx安装文档
1. 安装依赖包 yum -y install pcre-devel yum -y install openssl-devel yum -y install gcc yum -y install ...
- GPUImage的filter 响应处理链 的理解笔记
GPUImage的filter的textures处理链式结构 两个最重要的的地方: 最重要的一个类GPUImageOutput(所有的filter的父类,其他也有继承它的,如GPUImageUIEle ...
- python基础操作_方法(函数)
#函数,方法#普通方法def hello(): print('hello')hello()#带形参的方法def hello1(name): print('hello%s'%name)hello1('布 ...
- MyBatis源码分析之环境准备篇
前言 之前一段时间写了[Spring源码分析]系列的文章,感觉对Spring的原理及使用各方面都掌握了不少,趁热打铁,开始下一个系列的文章[MyBatis源码分析],在[MyBatis源码分析]文章的 ...
- ipython及Python初体验
阅读目录: Python环境体验 Python编辑器 ipython安装 Python提示符 Python初体验 print和变量 变量操作 内建函数:方法 数学运算:简单算术.随机数 关于模块 一. ...
- Datatable插件的简单的使用方式 和 学习方式
gridyxz = $("#sjjg_table_yxz").DataTable({ "searching" : false, "info&quo ...
- 用Nodejs做一个简单的小爬虫
Nodejs将JavaScript语言带到了服务器端,作为js主力用户的前端们,因此获得了服务器端的开发能力,但除了用express搭建一个博客外,还有什么好玩的项目可以做呢?不如就做一个网络爬虫吧. ...
- scala环境配置+hello world!
下载地址: http://www.scala-lang.org/download/ 我下载的是zip 配置环境变量 需要jdk支持,jdk的安装配置此处略过 控制台命令 scala -version ...