zoukankan      html  css  js  c++  java
  • 【习题 7-8 UVA-12107】Digit Puzzle

    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    迭代加深搜索。 枚举最大层数。(也即改变的数字个数 然后枚举第一个改哪个数字,第二个改哪个数字。。 一定要注意字典序问题。 每次优先改成较小的字典序(也即顺序枚举 然后注意这个字符不改的情况。 不要算改变数。 最后改完之后。 只需要枚举a和b的情况。 看看a*b是不是等于c就好 ->查看这样的a*b数量是不是1 如果是1的话.就说明是正确的。直接输出那个ans就好(我们已经是从小到大枚举了,找到的一定是答案

    【代码】

    /*
      	1.Shoud it use long long ?
      	2.Have you ever test several sample(at least therr) yourself?
      	3.Can you promise that the solution is right? At least,the main ideal
      	4.use the puts("") or putchar() or printf and such things?
      	5.init the used array or any value?
      	6.use error MAX_VALUE?
      	7.use scanf instead of cin/cout?
      	8.whatch out the detail input require
    */
    /*
        一定在这里写完思路再敲代码!!!
    */
    #include <bits/stdc++.h>
    using namespace std;
    
    string x,y,z,s,ans;
    int belong[10];
    int maxdep,lenx,leny,lenz,tot;
    
    string inttostring(int x){
        string s = "";
        while (x > 0){
            s = (char)(x%10+'0')+ s;
            x/=10;
        }
        return s;
    }
    
    int stringtoint(string s){
        int x = 0;
        int len = s.size();
        for (int i = 0;i < len;i++){
            x = x *10 + s[i]-'0';
        }
        return x;
    }
    
    void ok(int dep){
        if (tot>1) return;
        if (dep==lenx+leny){
            string v[3];
            for (int i = 0;i < 3;i++) v[i]="";
            for (int i = 0;i < lenx+leny+lenz;i++) v[belong[i]]+=s[i];
            int tx = stringtoint(v[0]),ty = stringtoint(v[1]);
            tx = tx*ty;
            string tz = inttostring(tx);
            if ((int)tz.size()!=(int)v[2].size()) return;
            for (int i = 0;i < (int) v[2].size();i++)
                if (tz[i]!=v[2][i] && v[2][i]!='*') return;
            tot++;
            return;
        }
        if (s[dep]=='*'){
            int qidian = 0;
            if (dep==0 || dep == lenx || dep==lenx+leny) qidian = 1;
            for (int i = qidian;i <= 9;i++){
                s[dep] = i+'0';
                ok(dep+1);
                s[dep] = '*';
            }
        }else ok(dep+1);
    }
    
    bool dfs1(int dep,int nex){
        if (dep==maxdep){
            tot = 0;
            string tans = s;
            ok(0);
            if(tot==1) {
                ans = s;
                return true;
            }
            return false;
        }
        if (nex>=lenx+leny+lenz) return false;
        char temp;
    
        temp = s[nex];
        s[nex] = '*';
        int cnt = 1;
        if (s[nex]==temp) cnt = 0;
        if (dfs1(dep+cnt,nex+1)) return true;
        s[nex] = temp;
    
    
        int qidian = 0;
        if (nex==0 || nex == lenx || nex == lenx+leny) qidian = 1;
        for (int i = qidian;i <= 9;i++){
            temp = s[nex];
            s[nex] = i+'0';
            int cnt = 1;
            if (s[nex]==temp) cnt = 0;
            if (dfs1(dep+cnt,nex+1)) return true;
            s[nex] = temp;
        }
    
        return false;
    }
    
    int main(){
    	#ifdef LOCAL_DEFINE
    	    freopen("rush_in.txt", "r", stdin);
    	#endif
    	ios::sync_with_stdio(0),cin.tie(0);
    	int kase = 0;
        while (cin >> x && x[0]!='0'){
            ans.clear();
            cin >> y >> z;
            lenx = x.size(),leny = y.size(),lenz = z.size();
            s = x + y + z;
            for (int i = 0;i < lenx;i++) belong[i] = 0;
            for (int i = lenx;i < lenx+leny;i++) belong[i] = 1;
            for (int i = lenx+leny;i < lenx+leny+lenz;i++) belong[i] = 2;
    
            for (maxdep = 0;;maxdep++)
                if (dfs1(0,0)) break;
    
            cout <<"Case "<<++kase<<": ";
            for (int i = 0;i < lenx;i++) cout << ans[i];cout <<' ';
            for (int i = lenx;i < lenx+leny;i++) cout << ans[i];cout <<' ';
            for (int i = lenx+leny;i < lenx+leny+lenz;i++) cout << ans[i];
            cout << endl;
        }
    	return 0;
    }
    
    
  • 相关阅读:
    Android studio ocr初级app开发问题汇总(含工程代码)
    面试题思路,25匹马 5个跑道取前三,前五,最少比赛次数--七次
    中国计算机学会推荐国际学术会议和期刊目录(CCF)2019年
    swift 数组部分排序
    SceneKit下关于修改SCNNode 的Shader展示自定义图形
    汇编语言实现字母下落效果
    组合数计算-java
    miniblast_hash算法c语言实现
    日常开发知识点备忘
    骑士周游问题跳马问题C#实现(附带WPF工程代码)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/8166417.html
Copyright © 2011-2022 走看看