HDU - 5547 数独(回溯法)
题目链接:HDU-5547 http://acm.hdu.edu.cn/showproblem.php?pid=5547
正所谓:骗分过样例,暴力出奇迹。
解题思想(暴力出奇迹(DFS+回溯)):
1. 依次在空格里面填上“1~9”,并检查这个数字是否合法(其所在的行、列,以及3X3的子区域里不存在重复的数字)。如果合法,则前进到下一个格子。
2. 如果在某个格子里,从“1”到“9”都不合法,这说明前面某个格子填错了。这时就回退到上一格,继续试。例如,如果上一格已填的数字是3,就继续试4,5,6,… 是否合法。如果找到一个合法的数字,则又前进到下一格。如果找不到,说明前面还有格子也填错了,则继续回退到更前面一格,… 如此反复。
4. 如果这个数独是有解的,我们总会遇到“蒙对了”的情况。
HDU 的这题实在是太坑了,题目描述里有一句:Each test case starts with an empty line followed by 4 lines,没仔细看输入样例里也没有空行 。66666666666666666。告辞,我错了。
#include <iostream>
#include <string>
using namespace std; int M[][];
bool flag = false; int check(int row, int column, int x) {
for (int i = ; i < ; i++) {
if (M[i][column] == x || M[row][i] == x)
return ;
}
int r = row / * , c = column / * ;
for (int i = r; i < r + ; i++) {
for (int j = c; j < c + ; j++) {
if (M[i][j] == x) return ;
}
}
return ;
} void DFS(int row, int column) {
if (row == ) {
flag = true;
return;
} if (M[row][column] == -) {
int i;
for (i = ; i <= ; i++) {
if (check(row, column, i)) {
M[row][column] = i;
DFS(row + (column + ) / , (column + ) % );
if (flag) return;
}
}
if (i == ) {
M[row][column] = -;
return;
}
}
DFS(row + (column + ) / , (column + ) % );
}
int main() {
int i = ,n;
cin >> n; string s;
cin.ignore();
while (n--) {
flag = false;
for (int i = ; i < ; i++) {
getline(cin, s);
if (s.empty()) {
i--;
continue;
}
for (int j = ; j < ; j++) {
M[i][j] = s[j]-'';
}
}
DFS(, );
cout << "Case #" << i++ << ":" << endl;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
cout << M[i][j];
}
cout << endl;
}
}
return ;
}
HDU - 5547 数独(回溯法)的更多相关文章
- 回溯法、数独与N阶可达问题
回溯法是剪了枝的穷举,这是字面上的说法,不太好理解,不如讲解实例来的酸爽,于是引出了N阶可达问题: 有N个国家,每个国家有若干城市,小明要从中国(任意一个城市)出发,遍历所有国家(假设这个遍历顺序已经 ...
- P1074 靶形数独 dfs回溯法
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
- HDU 2553 n皇后问题(回溯法)
DFS Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- python常用算法(7)——动态规划,回溯法
引言:从斐波那契数列看动态规划 斐波那契数列:Fn = Fn-1 + Fn-2 ( n = 1,2 fib(1) = fib(2) = 1) 练习:使用递归和非递归的方法来求解斐波那契数 ...
- 【LeetCode】回溯法 backtracking(共39题)
[10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...
- 回溯法解决N皇后问题(以四皇后为例)
以4皇后为例,其他的N皇后问题以此类推.所谓4皇后问题就是求解如何在4×4的棋盘上无冲突的摆放4个皇后棋子.在国际象棋中,皇后的移动方式为横竖交叉的,因此在任意一个皇后所在位置的水平.竖直.以及45度 ...
- leetcode_401_Binary Watch_回溯法_java实现
题目: A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bot ...
随机推荐
- 微信小程序button选中改样式-实现单选/多选
小程序实现多button单选/多选 红色为选中状态 单选 多选 ①wxss /* pages/button-select/button-select.wxss */ .button_container ...
- linux-ubuntu安装配置uwsgi
参考原文 对于 Python2.x 版本:(测试通过) 第一步:sudo apt-get install python-dev 第二步:sudo apt-get install python-pip ...
- SQL Server Metadata
http://www.devart.com/dotconnect/sqlserver/docs/MetaData.htmlhttps://msdn.microsoft.com/en-us/librar ...
- 移动前端调试页面–weinre
安装 npm install -g weinre 启动 weinre --boundHost -all- 浏览器查看 http://localhost:8080 插入相关文件 index.html d ...
- AngularJS开发人员最常犯的10个错误
简介AngularJS是目前最为活跃的Javascript框架之一,AngularJS的目标之一是简化开发过程,这使得AngularJS非常善于构建小型app原型,但AngularJS对于全功能的客户 ...
- Java基础之引用(String,char[],Integer)总结于牛客网的专项练习题
1.String的引用: 下列代码执行后的结果为: public class Test { public static void main(String[] args) { StringBuffer ...
- [转]Linux芯片级移植与底层驱动(基于3.7.4内核)
1. SoC Linux底层驱动的组成和现状 为了让Linux在一个全新的ARM SoC上运行,需要提供大量的底层支撑,如定时器节拍.中断控制器.SMP启动.CPU hotplug以及底层的G ...
- Android热修复之AndFix使用教程
AndFix的github地址 AndFix 全称Android hot-fix,是alibaba的Android热修复框架,支持Android 2.3到6.0的版本,支持arm与X86系统架构,支持 ...
- org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.core.convert.support.ConversionServiceFactory.cr
spring-data-mongo 和spring core包冲突.解决办法: <dependency> <groupId>org.springframework.data&l ...
- libcrypto.so.10内容丢失导致sshd无法运行
今天启动虚拟机,ssh服务起不来,提示如下: Starting sshd: /usr/sbin/sshd: error while loading shared libraries: libcrypt ...