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 ...
随机推荐
- Java_文件夹分割与合并
一.思路: 1.文件切割: 使用类RandomAccessFile ,其中方法seek可以自定义读取位置,读一段,通过字节输出流(我使用BufferedOutputStream)写一段 2.文件合并 ...
- 继承、接口、static、abstract
继承: 1.用extends来完成继承 2.子类可以继承父类全部的数据域但是只有部分的数据域对子类可见 3.在java中支持单继承 4.单继承和多继承的比较 (1)多继承比单继承能够更好的提高代码的复 ...
- 前端入门3-CSS基础
本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 声明 本系列文章内容全部梳理自以下四个来源: <HTML5权威指南> <JavaScript权威指南> MD ...
- angular ng-click防止重复提交
方法一:点击后,让button的状态变为disable js指令: .directive('clickAndDisable', function() { return { scope: { click ...
- es6 语法 (数组扩展)
{ let arr = Array.of(3, 4, 7, 9, 11); console.log('arr', arr); //[3,4,7,9,11] let empty = Array.of() ...
- 51nod1238 最小公倍数之和 V3(莫比乌斯反演)
题意 题目链接 Sol 不想打公式了,最后就是求一个 \(\sum_{i=1}^n ig(\frac{N}{i})\) \(g(i) = \sum_{i=1}^n \phi(i) i^2\) 拉个\( ...
- 洛谷P4492 [HAOI2018]苹果树(组合数)
题意 题目链接 Sol 有点自闭,.我好像对组合数一窍不通(~~~~) Orz shadowice // luogu-judger-enable-o2 #include<bits/stdc++. ...
- springboot 常用配置文件
1.连接数据库 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...
- 上了IPD和CMMI,为什么还要搞敏捷?
文/资深顾问 杨学明 现在国内许多产品创新型企业一旦研发团队上了规模,就会进行IPD体系的变革或CMMI的认证,但现在还有一种更加流程的开发模式,就是敏捷,华为公司早在2009年正式发文在全公司现在流 ...
- Linux中DNS的设置
1.查看本机的域名 hostname 2.修改DNS 临时修改: hostname desktop0.example.com 永久修改: hostnamectl set-hostname deskto ...