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 ...
随机推荐
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase
原文地址:http://www.51csharp.com/MVC/882.html ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL 引言-- 在初级篇中,我们 ...
- C++函数后面加const修饰
声明一个成员函数的时候用const关键字是用来说明这个函数是 "只读(read-only)"函数,也就是说明这个函数不会修改任何数据成员(object). 为了声明一个const成 ...
- iOS opencv
1.在iPhone上使用 OpenCV http://blog.csdn.net/kmyhy/article/details/7560472 2. OpenCV iOS Hello¶ http://d ...
- Live555 分析(三):客服端
live555的客服端流程:建立任务计划对象--建立环境对象--处理用户输入的参数(RTSP地址)--创建RTSPClient实例--发出DESCRIBE--发出SETUP--发出PLAY--进入Lo ...
- spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例
在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...
- textChanged(*)重点
# -*- coding: cp936 -*- import sys from PyQt4 import QtCore, QtGui class MyDialog(QtGui.QDialog): de ...
- 用户向导页面实现左右滑动的ImageSwitcher
当你第一次打开app时刻,通常有使用向导现在演示APK基本功能和用法,该向导是非常重要的,用户可以知道并调整到速度app如何. 实现此使用向导有非常多种方法,比方用ImageSwitcher.View ...
- [core java学习笔记][第十一章异常断言日志调试]
第11章 异常,断言,日志,调试 处理错误 捕获异常 使用异常机制的技巧 使用断言 日志 测试技巧 GUI程序排错技巧 使用调试器 11.1 处理错误 11.1.1异常分类 都继承自Throwable ...
- 1215.1——动态分配内存的补充realloc
当再次在原来申请的内存基础上再加内存的时候用realloc,如果第一次分配的内存后面存储地方够用,则连着原来的申请,如果不够用,就重新找到一块够用的地方,然后把原来的复制过去 int main(int ...
- 最强烈推荐-我的java收藏夹(内有国内最好的java论坛)
原地址: http://bbs.chinaitlab.com/dispbbs.asp?boardid=148&id=34276 国内: www.chinajavaworld.com-论坛人很多 ...