• UVa-12107 Digit Puzzle


    “借鉴”了别人的代码,优化十分巧妙——只需递归前两个数,就可知道能不能凑一个第三个数使这种情况成立。

      1 #include <bits/stdc++.h>
      2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
      3 #define pb push_back
      4 using namespace std;
      5 //-2 stands for the blank ;-1 stands for the *
      6 vector<int> p[3];
      7 
      8 bool ok()
      9 {
     10     int d[2] {0};
     11     _for(i,0,2)
     12     _for(j,0,p[i].size())
     13     if(p[i][j]==-1)
     14         return false;
     15     else
     16         d[i] = d[i]*10+p[i][j];
     17         
     18     int num = d[0]*d[1];
     19     int str[39];
     20     
     21     _for(i, 0, p[2].size())
     22     {
     23         str[p[2].size()-i-1] = num % 10;
     24         num /= 10;
     25     }
     26     
     27     if(num != 0 || str[0] == 0) return 0;
     28     _for(i, 0, p[2].size())
     29         if(p[2][i] != -1 && str[i] != p[2][i])
     30             return 0;
     31             
     32     return 1;
     33 }
     34 
     35 int dfs1(int i,int j)
     36 {
     37     if(i==2)
     38         return ok();
     39     int rnt = 0;
     40     if(p[i][j]==-1)
     41     {
     42         _for(k,0,10)
     43         {
     44             if(k==0&&j==0)
     45                 continue;
     46             p[i][j] = k;
     47             if(j==p[i].size()-1)
     48             {
     49                 rnt += dfs1(i+1,0);
     50                 p[i][j] = -1;
     51                 if(rnt>1) return 2;
     52             }
     53             else
     54             {
     55                 rnt += dfs1(i,j+1);
     56                 p[i][j] = -1;
     57                 if(rnt>1) return 2;
     58             }
     59         }
     60     }
     61     else
     62     {
     63         if(j==p[i].size()-1)
     64         {
     65             rnt += dfs1(i+1,0);
     66             if(rnt>1) return 2;
     67         }
     68         else
     69         {
     70             rnt += dfs1(i,j+1);
     71             if(rnt>1) return 2;
     72         }
     73     }
     74     return rnt;
     75 }
     76 
     77 int maxd;
     78 const char* word = "*0123456789";
     79 bool dfs2(int i,int j,int d)
     80 {
     81     if(d==maxd) return (dfs1(0,0)==1);
     82     if(i==3) return false;
     83 
     84     int ti, tj;
     85     if(j == p[i].size() - 1){ti = i + 1;tj = 0;}
     86     else{ti = i;tj = j + 1;}
     87     
     88     char t;
     89     if(p[i][j]==-1)
     90         t = '*';
     91     else
     92         t = p[i][j] + '0';
     93     _for(k, 0, 11)
     94     {
     95         if(word[k] == '0' && j == 0) continue;
     96         if(t == word[k])
     97         {
     98             if(dfs2(ti, tj, d))
     99                 return true;
    100         }
    101         else
    102         {
    103             if(k==0)
    104             {
    105                 p[i][j] = -1;
    106                 if(dfs2(ti, tj, d + 1)) return true;
    107             }
    108             else
    109             {
    110                 p[i][j] = word[k]-'0';
    111                 if(dfs2(ti, tj, d + 1)) return true;
    112             }
    113             if(t=='*')
    114                 p[i][j] = -1;
    115             else
    116                 p[i][j] = t-'0';
    117         }
    118     }
    119     return false;
    120 }
    121 
    122 void ID()
    123 {
    124     for(maxd=0; ; maxd ++)
    125         if(dfs2(0,0,0)) return ;
    126 }
    127 
    128 int kase = 1;
    129 void output()
    130 {
    131     printf("Case %d: ",kase++);
    132     _for(i,0,3)
    133     {
    134         _for(j,0,p[i].size())
    135         {
    136             if(p[i][j]==-1)
    137                 cout << '*';
    138             else
    139                 cout << p[i][j];
    140         }
    141         if(i!=2)
    142             cout << " ";
    143     }
    144     cout << endl;
    145 }
    146 
    147 int main()
    148 {
    149     string str;
    150     while(getline(cin,str)&&str[0]!='0')
    151     {
    152         _for(i,0,3)
    153         p[i].clear();
    154         int pEnd = 0;
    155         _for(i,0,str.size())
    156         {
    157             if(str[i]=='*')
    158                 p[pEnd].pb(-1);
    159             else if(str[i]==' ')
    160                 pEnd ++;
    161             else
    162                 p[pEnd].pb(str[i]-'0');
    163         }
    164         ID();
    165         output();
    166     }
    167     return 0;
    168 }
  • 相关阅读:
    JBoss+MyEclipse+Mysql 的J2EE环境配置。
    编写一个Session EJB
    编写一个MDB (Message Drive Bean)
    轻便的客户端本地文件数据库 SQLite
    SSH(Struts+Spring+Hibernate)Summary/Profile
    Linux test命令
    mysql权限相关
    linux shell 查看进程的可执行程序路径
    添加dns
    Linux的shell变量
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10105175.html
Copyright © 2020-2023  润新知