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的更多相关文章

  1. UVA - 12107 Digit Puzzle(数字谜)(IDA*)

    题意:给出一个数字谜,要求修改尽量少的数,使修改后的数字谜只有唯一解.空格和数字可以随意替换,但不能增删,数字谜中所有涉及的数必须是没有前导零的正数.输入数字谜一定形如a*b=c,其中a.b.c分别最 ...

  2. uva 227 Puzzle (UVA - 227)

    感慨 这个题实在是一个大水题(虽然说是世界决赛真题),但是它给出的输入输出数据,标示着老子世界决赛真题虽然题目很水但是数据就能卡死你...一直pe pe直到今天上午AC...无比感慨...就是因为最后 ...

  3. Puzzle UVA - 227 PE代码求大佬指点

    ​ A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smal ...

  4. 紫书 习题7-8 UVa 12107 (IDA*)

    参考了这哥们的博客 https://blog.csdn.net/hyqsblog/article/details/46980287  (1)atoi可以char数组转int, 头文件 cstdlib ...

  5. uva 227 Puzzle

     Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...

  6. UVA 227 Puzzle - 输入输出

    题目: acm.hust.edu.cn/vjudge/roblem/viewProblem.action?id=19191 这道题本身难度不大,但输入输出时需要特别小心,一不留神就会出问题. 对于输入 ...

  7. UVA 277 Puzzle

    题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串. 注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句. ②除了最后一次的输出外,其他输出均要在后面空一行. ③操作 ...

  8. UVA 227 Puzzle(基础字符串处理)

    题目链接: https://cn.vjudge.net/problem/UVA-227 /* 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串 的指令,如果指令合法,能够得 ...

  9. uva live 12846 A Daisy Puzzle Game

    假设下一个状态有必败.那么此时状态一定是必胜,否则此时状态一定是必败 状压DP #include<iostream> #include<map> #include<str ...

随机推荐

  1. bzoj1060 [ZJOI2007]时态同步

    Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板 ...

  2. Remove Invalid Parentheses 解答

    Question Remove the minimum number of invalid parentheses in order to make the input string valid. R ...

  3. 选择排序(Selection Sort)

    选择排序就是在选择数组元素上做文章,关键是如何选择?选择的标准是什么?选择之后放在哪?所有这些都是选择排序的问题. 选择排序算法中,通常会有以下操作: 从数组第一个元素开始. 遍历整个数组,找到最小的 ...

  4. Hdu1401-Solitaire(双向bfs)

    Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...

  5. 第05讲- DDMS中logcat的使用

    第05讲 DDMS中logcat的使用 1.DDMS DDMS 的全称是Dalvik Debug Monitor Service,是 Android 开发环境中的Dalvik虚拟机调试监控服务.DDM ...

  6. python 给lambda命名(网友处学习)

    from os import * def set_name(**k): assert len(k)==1 name,obj=k.items()[0] obj.func_name=name return ...

  7. Sort(归并)

    Sort 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 You want to processe a sequence of n distinct integers ...

  8. Sprite Kit编程指南(1)-深入Sprite Kit

    深入Sprite Kit 学习Sprite Kit最好的方法是在实践中观察它.此示例创建一对场景和各自的动画内容.通过这个例子,你将学习使用Sprite Kit内容的一些基础技术,包括: ·      ...

  9. ASP.NET实现IE下禁用浏览器后退按钮办法

    在page_load方法里面增加如下代码: Response.Buffer = true; Response.ExpiresAbsolute = DateTime.Parse("2010-1 ...

  10. eclipse中多个工程编译到同一个目录下

    1.点击link source  2.选择Java(ps:Java文件目录)或者resource(ps:配置文件目录)  3.最后结果,然后使用project中的clean进行编译,就可以把两个工程编 ...