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 ...
随机推荐
- 通过Excel来集中管理资源文件
在支持双语或多语种项目中,常常需要编辑多个文件来添加资源项,感觉比较繁琐,所以想做一个可以集中管理资源文件的工具.借助Excel,使用Excel来记录,并且通过Excel可以进行分页分模块来规划 ...
- 利用内存结构及多线程优化多图片下载(IOS篇)
利用内存结构及多线程优化多图片下载(IOS篇) 前言 下载地址, 后续发布, 请继续关注本blog 在IOS中,我们常常遇到多图片下载的问题.最简单的解决方案是直接利用别人写好的框架.但是这如同练武, ...
- Unity SendMessage方法
我们今天研究下SendMessage方法, 如果我们需要执行某一个组件的方法时候可以使用SendMessage gameObject.SendMessage("A"); 即可通知当 ...
- iOS获取一个方法的执行时间
#import <Foundation/Foundation.h> #import <mach/mach_time.h> typedef void (^block)(void) ...
- 使用maven编译的时候提示 maven-source 1.3 中不支持注释请使用 -source 5 或更高版本以启用注释的错误。
在编译的模块的pom文件中加上 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins ...
- 测试MD5的加密功能
测试md5主要用于数据库加密.图片修改为RAR格式有源程序.
- UML学习-时序图
时序图(Sequence Diagram)是显示对象之间交互的图,这些对象是按时间顺序排列的.顺序图中显示的是参与交互的对象及其对象之间消息交互的顺序.时序图中包括的建模元素主要有:对象(Actor) ...
- Linux中Firefox——Httpfox插件安装及使用
Httpfox插件安装步骤: 1.打开firefox浏览器,点击左上方"工具"中的"附加组件" 2.在弹出页中搜索"Httpfox",点击下 ...
- poj2778DNA Sequence (AC自动机+矩阵快速幂)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud DNA Sequence Time Limit: 1000MS Memory ...
- postman接口测试工具3.0版本的坑
今天用postman接口测试工具3.0版本被坑,找了半天,原来postman这个新版本有个坑啊 下面的get参数,第一行不管你填不填,都是无效的,可能是postman的一个bug吧