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_文件操作
说明:如有转载,请标明出处!! 一.文件操作 1.文件常用操作方法 open() f=open('文件名','r',encoding='utf-8') #三个参数,第一个文件详细路径,需要写明文件格式 ...
- Netty实战十一之预置的ChannelHandler和编解码器
Netty为许多通用协议提供了编解码器和处理器,几乎可以开箱即用,这减少了你在那些相当繁琐的事务上本来会花费的时间与精力.我们将探讨这些工具以及它们所带来的好处,其中包括Netty对于SSL/TLS和 ...
- CSS3动画属性:动画(animation)
一:动画(animation)的参数详解 由于上面用到了animation动画,这里详细介绍下这个animation的参数. 简介 CSS动画(Animations)简单说就是在一段固定的动画时间内暗 ...
- 「Android」消息驱动Looper和Handler类分析
Android系统中的消息驱动工作原理: 1.有一个消息队列,可以往这个消息队列中投递消息; 2.有一个消息循环,不断的从消息队列中取得消息,然后处理. 工作流程: 1.事件源将待处理的消息加入到消息 ...
- C# Params的使用
using System; namespace Params { class Program { static void Main(string[] args) { PrintMany("H ...
- springboot 学习之路 6(定时任务)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- (网页)angularJs中怎么模拟jQuery中的this?(转)
转载自mini_fan博客园: 今天想在Angular项目中使用jQuery的this功能,发现undefined.代码如下: HTML部分: <p ng-click="testCli ...
- 2015年6月6日,杨学明老师《IT技术人才管理角色转型与实践》专题培训在苏宁云商成功举办!
2015.6.6,在中国南京苏宁总部,研发资深顾问.资深讲师为苏宁易购IT事业部全体产品总监.研发总监进行了为期一天的<IT技术人才管理角色转型与实践>的内训服务. 杨学明老师分别从技术人 ...
- 编程一小时 code.org [六一关注]
编程一小时活动的组织者是Code.org, 它是一个面向公众的公益组织,致力于在更多的学校推广计算机科学教育,并为女性和就业率低的有色人种学生学习计算机的机会.同时,一个空前强大的合作伙伴联盟也在支持 ...
- Python 魔法方法简介
1.什么是魔法方法? 魔法方法就是可以给你的类增加魔力的特殊方法,如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会在特殊的情况下被 Python 所调用,你可以定义自己想要的行为,而这一 ...