zoukankan      html  css  js  c++  java
  • UVA12107-Digit Puzzle(迭代加深搜索)

    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 first 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
    ", s[0], s[1], s[2]);
    94     }
    95     return 0;
    96 }
  • 相关阅读:
    MongoDB 介绍和操作
    公钥私钥的那点事儿
    这是你了解的 print()函数吗
    Windows应急响应和系统加固(7)——Windows操作系统日志分析
    Windows应急响应和系统加固(6)——Windows历年高危漏洞介绍和分析
    Windows应急响应和系统加固(4)——Windows帐号角色权限的安全检查分析以及PowerShell的使用介绍
    Windows应急响应和系统加固(3)——Windows操作系统的帐号角色权限
    Windows应急响应和系统加固(2)——Windows应急响应的命令使用和安全检查分析
    Windwos应急响应和系统加固(1)——Windwos操作系统版本介绍
    Pycharm中配置鼠标悬停快速提示方法参数
  • 原文地址:https://www.cnblogs.com/npugen/p/9589338.html
Copyright © 2011-2022 走看看