UVA12107-Digit Puzzle(迭代加深搜索)
Problem UVA12107-Digit Puzzle
Accept:85 Submit:612
Time Limit: 3000 mSec
Problem Description

Input
The input contains several test cases. Each test case contains three non-empty strings, x, y, z, having at most 2, 2 and 4 characters respectively. Each character is a digit or a wildcard ‘*’, x will not begin with a zero character. The last test case is followed by a single zero, which should not be processed.
Output
For each test case, print the case number and the converted puzzle. If more than one optimal solution is found, the lexicographically first one should be printed (remember that “*” is before “0”). There is always a solution.
Sample Input
Sample Ouput
Case 1: 7 ** 8*
Case 2: ** ** 1*
题解:题目思路不难,实现起来略显困难。用IDA*控制修改个数,用另一个dfs函数判断解是否可行,在这里称为check。字典序很简单,就是搜的时候从小到大就行,第一个找到的答案肯定字典序最小。
由于最后输出的是待填空的字符串,因此在check的过程中,临时修改的全局变量一定要记得改回来。由于必须是唯一解才是最终的可行解,因此check函数在编写的过程中,绝不能找到一组解就return true.
记录解的组数,大于1就break,改回全局变量之后return cnt.
#include <bits/stdc++.h> using namespace std; const char table[] = "*0123456789";
const int maxn = ; int maxd;
int len[];
char s[maxn][maxn]; int cal() {
int aa = atoi(s[]), bb = atoi(s[]);
int cc = aa * bb;
char tmp[maxn];
for (int i = ; i < len[];i++) {
tmp[len[] - i - ] = cc % + '';
cc /= ;
}
if (cc != || tmp[] == '') return ; for (int i = ; i < len[]; i++) {
if (s[][i] != '*') {
if (s[][i] != tmp[i]) return ;
}
}
return ;
} int check(int id, int pos) {
if (id == ) return cal(); int ta, tb, cnt = ;
if (pos == len[id] - ) ta = id + , tb = ;
else ta = id, tb = pos + ; char t = s[id][pos];
if (isdigit(s[id][pos])) {
cnt += check(ta, tb);
}
else {
for (int i = ; i < ; i++) {
if (i == && pos == ) continue;
s[id][pos] = table[i];
cnt += check(ta, tb);
if (cnt > ) break;
}
} s[id][pos] = t;
return cnt;
} bool dfs(int d, int id, int pos) {
if (d == maxd) return check(, ) == ;
if (id == ) return false; int ta, tb;
if (pos == len[id] - ) ta = id + , tb = ;
else ta = id, tb = pos + ; char t = s[id][pos];
for (int i = ; i < ; i++) {
if (i == && pos == ) continue;
if (s[id][pos] == table[i]) {
if (dfs(d, ta, tb)) return true;
}
else {
s[id][pos] = table[i];
if (dfs(d + , ta, tb)) return true;
s[id][pos] = t;
}
} return false;
} int main()
{
int iCase = ;
while (~scanf("%s", s[])) {
if (s[][] == '') break;
scanf("%s%s", s[], s[]);
for (int i = ; i < ; i++) {
len[i] = strlen(s[i]);
} for (maxd = ;; maxd++) {
if (dfs(, , )) break;
} printf("Case %d: ", iCase++);
printf("%s %s %s\n", s[], s[], s[]);
}
return ;
}
UVA12107-Digit Puzzle(迭代加深搜索)的更多相关文章
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...
- C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains
此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...
- UVA11212-Editing a Book(迭代加深搜索)
Problem UVA11212-Editing a Book Accept:572 Submit:4428 Time Limit: 10000 mSec Problem Description ...
随机推荐
- python基础学习(八)元组
元组的定义 Tuple(元组)与列表类似,不同之处在于元组的 元素不能修改 元组 表示多个元素组成的序列 元组 在 Python 开发中,有特定的应用场景 用于存储 一串 信息,数据 之间使用 , 分 ...
- js操作css样式,null和undefined的区别?
1.js操作css的样式 div.style.width="100px"在div标签内我们添加了一个style属性,并设定了width值.这种写法会给标签带来大量的style属性, ...
- java实现无序数组结构
一.数组的2种定义方式 数据类型 [] 数组名称 = new 数据类型[数组长度]; 这里 [] 可以放在数组名称的前面,也可以放在数组名称的后面,一般放在名称的前面 数据类型 [] 数组名称 = ...
- CPU 实用工具
系统版本:CentOS 7.4 top 17:49:04 // 当前时间 up 3:55 // 系统运行时间,格式为时:分 2 users // 当前登录用户数 load average // 三个值 ...
- CSS选择器【记录】
1.基本选择器 2.组合选择器 3.伪类选择器 4.伪元素选择器 CSS选择器规定了CSS规则会应用到哪些元素上 1.基本选择器 基本选择器:通配选择器.元素选择器.类选择器.ID选择器.属性选择器 ...
- Mysql 常用数据类型
double:浮点型,double(5,2) 表示最多5位,必须包含两位小数,最大值是 999.99 char:定长字符串类型,char(10) 表示必须放 10 的字节,没有就用空格补充 varch ...
- [转] vi/vim命令模式和编辑模式各种操作
摘要:vi 编辑器是最常用的文档创建和编辑工具,初学者应该学会简单应用vi ,学会在vi 中做简单的修改.删除.插入.搜索及替换作业:如果您是新手,不妨看看本文,或许这篇文档能让您在最短的时间内学会v ...
- MyBatis(傻瓜式)框架
log4j的配置文件: 使用一个log4j.properties的配置文件,会设定log4j的设置信息,例如日志级别,日志输出方式,日志格式等等: # Set root category priori ...
- JavaScript中,JSON格式的字符串与JSON格式的对象相互转化
前言:JSON是一个独立于任何语言的数据格式,因此,严格来说,没有“JSON对象”和“JSON字符串”这个说法(然而”菜鸟教程“和”W3school“使用了“JSON对象”和“JSON字符串”这个说法 ...
- SQL Server的优化器会缓存标量子查询结果集吗
在这篇博客"ORACLE当中自定义函数性优化浅析"中,我们介绍了通过标量子查询缓存来优化函数性能: 标量子查询缓存(scalar subquery caching)会通过缓存结果减 ...