码迷,mamicode.com
首页 > 其他好文 > 详细

UVA12107-Digit Puzzle(迭代加深搜索)

时间:2018-09-05 09:05:05      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:sub   always   proc   cal   name   实现   com   turn   sample   

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 ?rst 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.

 

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const char table[] = "*0123456789";
 6 const int maxn = 10;
 7 
 8 int maxd;
 9 int len[3];
10 char s[maxn][maxn];
11 
12 int cal() {
13     int aa = atoi(s[0]), bb = atoi(s[1]);
14     int cc = aa * bb;
15     char tmp[maxn];
16     for (int i = 0; i < len[2];i++) {
17         tmp[len[2] - i - 1] = cc % 10 + 0;
18         cc /= 10;
19     }
20     if (cc != 0 || tmp[0] == 0) return 0;
21 
22     for (int i = 0; i < len[2]; i++) {
23         if (s[2][i] != *) {
24             if (s[2][i] != tmp[i]) return 0;
25         }
26     }
27     return 1;
28 }
29 
30 int check(int id, int pos) {
31     if (id == 2) return cal();
32     
33     int ta, tb, cnt = 0;
34     if (pos == len[id] - 1) ta = id + 1, tb = 0;
35     else ta = id, tb = pos + 1;
36 
37     char t = s[id][pos];
38     if (isdigit(s[id][pos])) {
39         cnt += check(ta, tb);
40     }
41     else {
42         for (int i = 1; i < 11; i++) {
43             if (i == 1 && pos == 0) continue;
44             s[id][pos] = table[i];
45             cnt += check(ta, tb);
46             if (cnt > 1) break;
47         }
48     }
49 
50     s[id][pos] = t;
51     return cnt;
52 }
53 
54 bool dfs(int d, int id, int pos) {
55     if (d == maxd) return check(0, 0) == 1;
56     if (id == 3) return false;
57 
58     int ta, tb;
59     if (pos == len[id] - 1) ta = id + 1, tb = 0;
60     else ta = id, tb = pos + 1;
61 
62     char t = s[id][pos];
63     for (int i = 0; i < 11; i++) {
64         if (i == 1 && pos == 0) continue;
65         if (s[id][pos] == table[i]) {
66             if (dfs(d, ta, tb)) return true;
67         }
68         else {
69             s[id][pos] = table[i];
70             if (dfs(d + 1, ta, tb)) return true;
71             s[id][pos] = t;
72         }
73     }
74 
75     return false;
76 }
77 
78 int main()
79 {
80     int iCase = 1;
81     while (~scanf("%s", s[0])) {
82         if (s[0][0] == 0) break;
83         scanf("%s%s", s[1], s[2]);
84         for (int i = 0; i < 3; i++) {
85             len[i] = strlen(s[i]);
86         }
87 
88         for (maxd = 0;; maxd++) {
89             if (dfs(0, 0, 0)) break;
90         }
91 
92         printf("Case %d: ", iCase++);
93         printf("%s %s %s\n", s[0], s[1], s[2]);
94     }
95     return 0;
96 }

 

UVA12107-Digit Puzzle(迭代加深搜索)

标签:sub   always   proc   cal   name   实现   com   turn   sample   

原文地址:https://www.cnblogs.com/npugen/p/9589338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!