• HDU 1104 Remainder


    BFS。

    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<string>
    #include<algorithm>
    #include<map>
    #include<iostream>
    using namespace std;
    
    int n,m,k;
    struct Node
    {
        int nowN;
        int tot;
        string op;
        Node(int t,int nown,string o)
        {
            tot=t;
            nowN=nown;
            op=o;
        }
    };
    queue<Node>Q;
    map<int,int>t;
    
    int MOD(int a,int b)
    {
        if(a>=0) return a%b;
        if(abs(a)%b==0) return 0;
        return (a+b*(abs(a)/b+1));
    }
    
    void BFS()
    {
        int flag=0;
        while(!Q.empty()) Q.pop();
        t.clear();
        Node s(0,MOD(n,m*k),"");
        Q.push(s);
        while(!Q.empty())
        {
            Node head=Q.front(); Q.pop();
    
            if(MOD(n+1,k)==MOD(head.nowN,k))
            {
                flag=1;
                printf("%d
    ",head.tot);
                cout<<head.op<<endl;
                break;
            }
            if(t[MOD(head.nowN+m,m*k)]==0)
            {
                t[MOD(head.nowN+m,m*k)]=1;
                Node s1(head.tot+1,MOD(head.nowN+m,m*k),head.op+"+");
                Q.push(s1);
            }
            if(t[MOD(head.nowN-m,m*k)]==0)
            {
                t[MOD(head.nowN-m,m*k)]=1;
                Node s2(head.tot+1,MOD(head.nowN-m,m*k),head.op+"-");
                Q.push(s2);
            }
            if(t[MOD(head.nowN*m,m*k)]==0)
            {
                t[MOD(head.nowN*m,m*k)]=1;
                Node s3(head.tot+1,MOD(head.nowN*m,m*k),head.op+"*");
                Q.push(s3);
            }
            if(t[MOD(MOD(head.nowN,m),m*k)]==0)
            {
                t[MOD(MOD(head.nowN,m),m*k)]=1;
                Node s4(head.tot+1,MOD(MOD(head.nowN,m),m*k),head.op+"%");
                Q.push(s4);
            }
        }
        if(!flag) printf("0
    ");
    }
    
    int main()
    {
        while(~scanf("%d%d%d",&n,&k,&m))
        {
            if(!n&&!m&&!k) break;
            BFS();
        }
        return 0;
    }
  • 相关阅读:
    WDA学习(14):Colors Cell and Input Enable to ALV Column
    JavaScript try-catch语句(错误处理)
    constructor
    input file 重复上传同一张图片失效的解决办法
    input下拉带输入框
    box-shaw四边阴影详解
    苹方字体合集
    两个div不同高度并排显示
    弹窗库
    webstorm破解安装版本
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5132278.html
Copyright © 2020-2023  润新知