HDU - 5547 Sudoku(数独搜索)
Description
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!!!
Input
It's guaranteed that there will be exactly one way to recover the board.
Output
Sample Input **** *
*
*
*
**
***
*
**
Sample Output
Case #: Case #: Case #:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5547
The 2015 China Collegiate Programming Contest
***********************************************
分析:一道比较简单的数独题目,这里简化了问题,变成了一个4*4的图,然后小图变成了2*2的图,这里问题就简洁了很多。
地图辣么小,直接枚举每一个点就行了。
从左上角开始,一直枚举到右下角,然后输出了就行了。
每次遇到一个“*”我们就枚举他变成1,2,3,4然后判断是否合法,如果合法就进行下一个点,否则回溯。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
using namespace std;
#define N 120000
#define INF 0x3f3f3f3f char a[][]; int judge(int x,int y)
{
int i,j; for(i=;i<;i++)///列判断,不能有相同的数字
if(a[x][i]==a[x][y]&&i!=y)
return ; for(i=;i<;i++)///行判断,不能有相同的数字
if(a[i][y]==a[x][y]&&i!=x)
return ; int row=x;
int col=y;
if(x%==)x-=;///找到小矩阵的左上角
if(y%==)y-=; for(i=x;i<=x+;i++)///小矩阵判断,不能有相同的数字
for(j=y;j<=y+;j++)
if(a[i][j]==a[row][col]&&i!=row&&j!=col)
return ; return ;
} void dfs(int x)///回溯dfs
{
int i,j; if(x==*)///如果遍历了所有点,就输出最终图形
{
for(i=;i<;i++)
{
for(j=;j<;j++)
printf("%c", a[i][j]);
printf("\n");
}
return ;
} int row=x/;///行的计算方法
int col=x%;///列的计算方法 if(a[row][col]=='*')
{
for(j=;j<=;j++)///枚举4个数字
{
a[row][col]=j+'';
if(judge(row,col))///如果当前这个点符合规则
dfs(x+);///进行下一步
a[row][col]='*';///记得要取消标记。(深搜尝试问题,涉及回溯)
}
}
else///如果不是,跳过,进行下一步
dfs(x+);
} int main()
{
int T,k=,i; scanf("%d", &T); while(T--)
{
for(i=;i<;i++)
scanf("%s", a[i]); printf("Case #%d:\n", k++); dfs();
}
return ;
}
HDU - 5547 Sudoku(数独搜索)的更多相关文章
- HDU 5547 Sudoku(DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5547 题目: Sudoku Time Limit: 3000/1000 MS (Java/Others ...
- HDU 1426 Sudoku Killer(搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 题意很明确,让你解一个9*9的数独. DFS即可. #include <cstdio> ...
- HDU 5547 Sudoku (暴力)
题意:数独. 析:由于只是4*4,完全可以暴力,要注意一下一些条件,比如2*2的小方格也得是1234 代码如下: #pragma comment(linker, "/STACK:102400 ...
- (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 ...
- E - Sudoku HDU - 5547 (搜索+暴力)
题目链接:https://cn.vjudge.net/problem/HDU-5547 具体思路:对于每一位上,我们可以从1到4挨着去试, 具体判断这一位可不可以的时候,看当前这一位上的行和列有没有冲 ...
- 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 ...
- 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 数独(回溯法)
题目链接:HDU-5547 http://acm.hdu.edu.cn/showproblem.php?pid=5547 正所谓:骗分过样例,暴力出奇迹. 解题思想(暴力出奇迹(DFS+回溯)): 1 ...
随机推荐
- 主机无法访问虚拟机的httpd服务
症状:虚拟机装的centos6.3 通过桥接的方式与主机连接 虚拟机通过yum安装httpd服务 在主机浏览器中输入 虚拟机ip 无法访问虚拟机Apache 虚拟机和主机可以相互ping通 解决:关 ...
- 4、Xcode8中的钥匙串保存数据取出时候为空的问题
Xcode7以及之前的版本直接使用Keychain存储数据即可,但是从Xcode8开始,再用之前的方法会发现,读取不到存进去的数据了,或者说,存储不进去了,原因是苹果加强了隐私保护,这个东西需要打开开 ...
- ABAP字符串操作 截取字符长度 取位数
ABAP字符串操作 ABAP對字串的操作方法與其他語言的操作有較大差別,以下是較常用的對字串操作的方法: 1. 字串的連接:CONCATENATEDATA: t1 TYPE c LENGTH 10 ...
- Kubernetes 认证
openssl genrsa -out ca.key 2048openssl req -x509 -new -nodes -key ca.key -subj "/CN=cluster.loc ...
- github本地库及clone常用命令
新建本地库 1. cd d: 2. mkdir git 3. cd git 4. git init 5. git remote add origin git@github.com:swportal/ ...
- 织梦dedecms文章发布日期时间调用标签大全
dedecms首页时间标签: 1.12-27 样式 [field:pubdate function='strftime("%m-%d",@me)'/] 2.May 15, 2012 ...
- Oracle使用虚拟表dual一次插入多条记录
从一个CSV文件中读取所有的数据,并且插入到一个Oracle数据库中,并且几分钟内完成,大约有60万条.网上有人说了,你可以循环insert然后插入几千条以后Commit一次,我靠,你自己试试看!!如 ...
- 导航栏底部黑线隐藏 UINavigationBar hidden Bottom Line
3种方法: 1.大杀器 ,iOS 10.2 最新系统亲测无问题( 添加导航栏分类) https://github.com/samwize/UINavigationBar-Addition/ 2.io ...
- js跨域请求获得数据
很多时候我们想访问其它站点下的数据怎么办? 由于javascript语言安全限制即同源策略造成的. 在使用ajax请求访问其他服务器的数据,此时客户端会出现跨域问题. 在js中,我们直接用XMLHtt ...
- linux安装包资源库
最近发现了一个很不错的linux的rpm资源库,可以在里面找到rpm安装过程中缺失的资源! 网址:http://pkgs.org/