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 ...
随机推荐
- 【Spring】22、Spring缓存注解@Cache使用
缓存注解有以下三个: @Cacheable @CacheEvict @CachePut @Cacheable(value=”accountCache”),这个注释的意思是,当调用这个 ...
- Java设计模式 - 单例模式详解(下)
单例模式引发相关整理 关联线程安全 在多线程下,懒汉式会有一定修改.当两个线程在if(null == instance)语句阻塞的时候,可能由两个线程进入创建实例,从而返回了两个对象.对此,我们可以加 ...
- Reinforcement Learning: An Introduction读书笔记(4)--动态规划
> 目 录 < Dynamic programming Policy Evaluation (Prediction) Policy Improvement Policy Iterat ...
- mysql length和char_length
length和char_length都是为了统计字符串的长度,length是按照字节来统计,char_lenght是按照字符来统计. 位(bit):计算机储存的最小单位. 字节(byte):计算机处理 ...
- Vue 2.5 发布了:15篇前端热文回看
Vue 2.5 发布了:15篇前端热文回看 2017-11-02 前端大全 (点击上方公众号,可快速关注) 本文精选了「前端大全」2017 年 10 月的 15 篇热门文章.其中有职场分享.技术分享和 ...
- JS命名空间模式解析
简介 在SF上看到这样一个提问: 如题,因为不得已的原因,需要写若干个全局函数.但又不想这样: window.a = function(){} window.b = function(){} wind ...
- Cartfile学习参考博客
1.http://www.cnblogs.com/xuruofan/p/6000864.html 2.http://www.jianshu.com/p/5ccde5f22a17
- [Python][小知识][NO.1] Python字符串前 加 u、r、b 的含义
1.字符串前加 u 例:u"我是含有中文字符组成的字符串." 作用:后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出 ...
- 如何在数据表当中找出被删掉的数据行ID
这个问题是一年前我刚步入IT行业的一个面试题,当时抓破头皮都想不到的问题,但现在回想过去自身不禁感到可笑,不多扯直接写解决方案.如何在数据表当中找出被删掉的数据行ID,意思是:在一堆的数据当中,让你找 ...
- [20180918]文件格式与sql_id.txt
[20180918]文件格式与sql_id.txt --//记录测试中遇到的一个问题.这是我在探究SQL*Net more data from client遇到的问题.--//就是实际oracle会把 ...