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

7 ** 8*
** ** ***
0
 

 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(迭代加深搜索)的更多相关文章

  1. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  2. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  3. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  4. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  5. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  6. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  7. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  8. 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神

    题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...

  9. C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains

    此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...

  10. UVA11212-Editing a Book(迭代加深搜索)

    Problem UVA11212-Editing a Book Accept:572  Submit:4428 Time Limit: 10000 mSec  Problem Description ...

随机推荐

  1. MyBatis:CRUD功能

    在前面已经自动生成了mapper和pojo,接下来我们实现mybatis的CRUD功能,先新建service.controller层的方法. 这里的sid是一个开源的id生成类,写完后,我们还需要在启 ...

  2. js异步编程终级解决方案 async/await

      在最新的ES7(ES2017)中提出的前端异步特性:async.await. async.await是什么 async顾名思义是“异步”的意思,async用于声明一个函数是异步的.而await从字 ...

  3. 反射demo(拷贝一个对象)

    经过了上一次对反射的初步认知,最近又接触到了后,做了一个小demo,感觉这次带了一点理解去做的,比第一次接触反射好了许多. 上次学习的链接,有一些反射用的基础语句.https://www.cnblog ...

  4. 用Python实现Zabbix-API 监控

    做运维的朋友应该知道,公司IDC机房经常有上架.下架.报修和报废的服务器.如果服务器数量很多的时候很容易造成监控遗漏.       大的互联网公司把监控系统和CMDB(资产管理系统|配置管理数据库系统 ...

  5. ListView子项点击无反应的解决办法

    在使用ListView控件的过程中,当子项包括Button或者CheckBoX等控件时,直接点击子项无反应,分析发现原来是Button,CheckBoX等控件会优先获取焦点,那么子项点击的焦点就被上述 ...

  6. 大事记 - 安卓微信浏览器 video 标签层级过高

    // 为什么叫<大事记>? // 以前总有面试官问这样一个问题:“你在项目中遇到过最头疼的问题是什么,是怎么解决的?” // 当时总觉得,已解决的问题都算不上头疼,所以回答总是不尽人意. ...

  7. 微信小程序 table 简单测试

    <view class='AutoTable'> <view id='AutoTableItem'> <block wx:for="{{array}}" ...

  8. select2 插件加载后端数据

    //html <select class="form-group form-control" name="roomId" id="roomLis ...

  9. win10电脑怎么录制视频 电脑录制视频软件

    win10电脑怎么录制视频?相信不少网友正在面临这个疑惑.现如今是网络信息科技时代,快速传播信息的途径和方式有很多种.其中,通过录制电脑视频,可以制作视频教程.游戏解说,还可以录制在线视频存储影视资源 ...

  10. Android为TV端助力 转载:Java 泛型

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...