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 ...
随机推荐
- Ansible hostvars
1. inventory hosts file 中的server 变量会覆盖group变量. hostvars: { "iaas_name": "test", ...
- git和SVN的区别
1)Git是分布式的,SVN不是: 这 是GIT和其它非分布式的版本控制系统,例如SVN,CVS等,最核心的区别.好处是跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并, ...
- Linux 服务器系统监控脚本 Shell【转】
转自: Linux 服务器系统监控脚本 Shell - 今日头条(www.toutiao.com)http://www.toutiao.com/i6373134402163048961/ 本程序在Ce ...
- ueditor1.4.3 在IE8下的 BUG
ueditor1.4.3 .net 版 在IE8 下,多图片上传完成后,点击确认时报错,无法插入图片到编辑器中 原因是 ueditor.all.js 中的 24835 行 if (whitList[ ...
- php 微信 自定义分享接口
<?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appS ...
- SQL防漏洞注入攻击小结
3/// 4/// 判断字符串中是否有SQL攻击代码 5/// 6/// 传入用户提交数据 7/// true-安全:false-有注入攻击现有: 8public bool Proces ...
- 你会用::before、::after吗
::before和::after伪元素的用法 一.介绍 css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类——:hover,:link,:active,:target,:not(),:fo ...
- 深入了解——CSS3新增属性
CSS3 选择器(Selector) 写过 CSS 的人应该对 CSS 选择器不陌生,我们所定义的 CSS 属性之所以能应用到相应的节点上,就是因为 CSS 选择器模式.参考下述代码: 清单 1. C ...
- FZU 1627 Revival's road
矩阵快速幂. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inc ...
- ios 从前台返回到回台 从后台返回到前台 或者 支付宝支付订单后 对界面进行操作
正常情况下,在AppDelegate中实现下面两个方法,能够监听从后台恢复到前台 - (void)applicationDidEnterBackground:(UIApplication *)appl ...