UVA_Digit Puzzle UVA 12107
If you hide some digits in an integer equation, you create a digit puzzle. The figure below shows two valid
digit puzzles. Hidden digits are represented by squares, and other digits are shown. The numbers involved in
this problem are all positive integers, written in decimal forms without leading zeros.
Fig 1. two good digit puzzles
If a digit puzzle has a unique solution, we call it a good puzzle. Both puzzles shown above are good puzzles.
The solution to the first puzzle is 7 * 12 = 84 , while the solution to the second one is 11 * 11 = 121 .
You are already given some digit puzzles, but some of them are not good. Your task is to convert these
puzzles into good ones. You can change any wildcard character (i.e. hidden digits) into a real digit, any real
digit to a wildcard character, or a real digit to another real digit, but you cannot insert or remove any character
at any place. The number of changed characters should be minimized.
In this problem, the puzzle is always in the form `` a×b = c ", and `` a×b " and `` b×a " should be considered
different if a is not equal to b . It is allowed that all digits of both a and b are shown (e.g 12×34 = * * * * ),
though that puzzle is actually a simple multiplication problem. Write a program to make good puzzles.
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.
3784 - Digit Puzzle 1/2
Sample Input
7 ** 8*
** ** ***
0
Sample Output
Case 1: 7 ** 8*
Case 2: ** ** 1*1
解题报告
采用迭代加深搜索,一个dfs用于构造,一个dfs用于检查解的合理性。
注意检查的dfs如果发现有两组及以上解可行就直接返回(不唯一,肯定不合法)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
char s[][],len[];
int caculate[] = {,,,,,},maxd;
char change[] = {'*','','','','','','','','','',''};
int lk = ; int check_result()
{
char check_str[];
char sss[];
int t0 = ,t1 = ,t2,cot = ;
for(int i = ; i < len[]; ++ i)
t0 = t0* + s[][i] - '';
for(int i = ; i < len[]; ++ i)
t1 = t1* + s[][i] - '';
t2 = t0*t1;
for(int i = ; i <len[];++i)
{
check_str[len[] - i - ] = t2 % + '';
t2 /= ;
}
if (t2 != || check_str[] == '') /*Not Equal Length with Len[2]*/
return ;
for(int i = ;i<len[];++i)
if(check_str[i] != s[][i] && s[][i] != '*')
return ;
return ;
} int check_(int a,int b)
{
int flag = ;
if (a == )
{
flag = check_result();
return flag;
}
int ta,tb;
char ch = s[a][b];
if (b == len[a] - )
{
ta = a +;
tb = ;
}
else
{
ta = a;
tb = b+;
}
if (s[a][b] == '*')
{
for(int i = ; i <= ; ++i)
if (b == && i == ) continue;
else
{
s[a][b] = change[i];
flag += check_(ta,tb);
if (flag > ) /* Not Found Solution */
break;
}
}
else
{
flag += check_(ta,tb);
}
s[a][b] = ch; /*Recover The Spot*/
return flag;
} int dfs(int a,int b,int d)
{
int flag;
if (d == maxd)
{
flag = check_(,);
if (flag == )
return ;
else
return ;
}
if (a == )
return ;
int ta,tb;
char ori = s[a][b];
if (b == len[a] -)
{
ta = a + ;
tb = ;
}
else
{
ta = a;
tb = b + ;
}
for(int i = ; i <= ; ++ i)
{
if (b == && i == ) continue;
if (ori == change[i])
{
s[a][b] = ori;
flag = dfs(ta,tb,d);
} else
{
s[a][b] = change[i];
flag = dfs(ta,tb,d+);
}
if (flag)
break;
}
if (!flag)
s[a][b] = ori; /*Not Found Sloution,Recover Spot */
return flag;
} int main(int argc, char * argv[])
{
int cas = ;
memset(s,,sizeof(s));
while(scanf("%s%s%s",s[],s[],s[]) == )
{
for(int i = ; i < ;++ i)
len[i] = strlen(s[i]);
/*Because input is always has a solution,so there is no limit to deep_Max */
printf("Case %d: ",++cas);
for(int i = ; ; ++ i)
{
maxd = i; /*iterative deepening */
if (dfs(,,))
{
printf("%s %s %s\n",s[],s[],s[]);
break;
}
}
memset(s,,sizeof(s));
}
return ;
}
UVA_Digit Puzzle UVA 12107的更多相关文章
- UVA - 12107 Digit Puzzle(数字谜)(IDA*)
题意:给出一个数字谜,要求修改尽量少的数,使修改后的数字谜只有唯一解.空格和数字可以随意替换,但不能增删,数字谜中所有涉及的数必须是没有前导零的正数.输入数字谜一定形如a*b=c,其中a.b.c分别最 ...
- uva 227 Puzzle (UVA - 227)
感慨 这个题实在是一个大水题(虽然说是世界决赛真题),但是它给出的输入输出数据,标示着老子世界决赛真题虽然题目很水但是数据就能卡死你...一直pe pe直到今天上午AC...无比感慨...就是因为最后 ...
- Puzzle UVA - 227 PE代码求大佬指点
A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smal ...
- 紫书 习题7-8 UVa 12107 (IDA*)
参考了这哥们的博客 https://blog.csdn.net/hyqsblog/article/details/46980287 (1)atoi可以char数组转int, 头文件 cstdlib ...
- uva 227 Puzzle
Puzzle A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...
- UVA 227 Puzzle - 输入输出
题目: acm.hust.edu.cn/vjudge/roblem/viewProblem.action?id=19191 这道题本身难度不大,但输入输出时需要特别小心,一不留神就会出问题. 对于输入 ...
- UVA 277 Puzzle
题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串. 注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句. ②除了最后一次的输出外,其他输出均要在后面空一行. ③操作 ...
- UVA 227 Puzzle(基础字符串处理)
题目链接: https://cn.vjudge.net/problem/UVA-227 /* 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串 的指令,如果指令合法,能够得 ...
- uva live 12846 A Daisy Puzzle Game
假设下一个状态有必败.那么此时状态一定是必胜,否则此时状态一定是必败 状压DP #include<iostream> #include<map> #include<str ...
随机推荐
- 2015第15周六Java线程池
Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 比较重要的几个类: Ex ...
- 利用智能手机(Android)追踪一块磁铁(一)
之前看到一个外国人用iPhone做了一个追踪磁铁的Demo感觉不错(参考视频:http://v.youku.com/v_show/id_XODM2MjczNzE2.html),然后我就参考做了一个An ...
- 本地plsqldev.exe连接远端oracle数据库
先看百度经验:http://jingyan.baidu.com/article/48b558e3540ecf7f38c09a3c.html 这里如果我们只有安装plsql工具,下载oracle精简版本 ...
- Hibernate框架增删改查测试类归为一个类
package cn.happy.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org ...
- 基础总结篇之九:Intent应用详解
看似尋常最奇崛,成如容易卻艱辛.北宋.王安石 看似普通的事情其实最不同寻常,并不是简简单单就可以做好的:成功看起来似乎很容易,而成功的过程却充满着艰辛. 对于我们认为很普通的事情,不屑一顾,就永远不会 ...
- [转]Laravel 4之路由
Laravel 4之路由 http://dingjiannan.com/2013/laravel-routing/ Laravel 4路由是一种支持RESTful的路由体系, 基于symfony2的R ...
- nodejs之简介及安装(一)
@[nodejs|个人学习笔记] nodejs简介 什么是node.js Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. 参考网站 一.nodejs.cn 二 ...
- NuGet学习笔记(3)——搭建属于自己的NuGet服务器(转)
在上一篇NuGet学习笔记(2) 使用图形化界面打包自己的类库 中讲解了如何打包自己的类库,接下来进行最重要的一步,从零开始搭建属于自己的NuGet服务器,诚然园子里及其它很多地方已经有完全写好的Nu ...
- Js Json 互转
推荐: //js对象转换为 JSON 文本 var text = '[{"id":1,"name":"C","size" ...
- 《JavaScript 闯关记》之单体内置对象
ECMA-262 对内置对象的定义是「由 JavaScript 实现提供的.不依赖于宿主环境的对象,这些对象在 JavaScript 程序执行之前就已经存在了」.意思就是说,开发人员不必显式地实例化内 ...