zoukankan      html  css  js  c++  java
  • hdu 1104

    
    % mod 
    比较基本的广搜 有些数论知识 第一次在节点力用了队列 挺好
    #include<iostream>
    #include<queue>
    using namespace std;
    struct Node
    {
        int x;
        int step;
        queue<char> q;
    };
    int v[1000010];
    int n,m,k;
    bool bfs()
    {
        int s=((n+1)%k+k)%k;
        queue<Node>q;
        Node now,next;
        now.x=n;now.step=0;
        q.push(now);
        memset(v,0,sizeof(v));
        while(!q.empty())
        {
            now=q.front();
            if((now.x%k+k)%k==s)
            {
                printf("%d
    ",now.step);
                while(now.step--)
                {
                    printf("%c",now.q.front());
                    now.q.pop();
                }
                printf("
    ");
                return true;
            }
            q.pop();
            int xx;
            xx=(now.x+m)%(k*m);
            if(!v[(xx%k+k)%k])
            {
                v[(xx%k+k)%k]=1;
                next=now;next.x=xx;next.q.push('+');next.step++;
                q.push(next);
            }
            xx=(now.x-m)%(k*m);
            if(!v[(xx%k+k)%k])
            {
                 v[(xx%k+k)%k]=1;
                next=now;next.x=xx;next.q.push('-');next.step++;
                q.push(next);
            }
            xx=(now.x*m)%(k*m);
            if(!v[(xx%k+k)%k])
            {
                v[(xx%k+k)%k]=1;
                next=now;next.x=xx;next.q.push('*');next.step++;
                q.push(next);
            }
            xx=((now.x%m+m)%m)%(m*k);
            if(!v[(xx%k+k)%k])
            {v[(xx%k+k)%k]=1;
            next=now;next.x=xx;next.q.push('%');next.step++;
            q.push(next);
            }
        }
        return 0;
    }
    int main()
    {
    
        while(scanf("%d%d%d",&n,&k,&m)!=EOF)
        {
            if(n==0&&k==0&&m==0)
                break;
            if(!bfs())
                printf("0
    ");
        }
        return 0;
    }


  • 相关阅读:
    supervised learning 监督式学习
    4.4 day14 内置函数
    4.3 day13 迭代器 生成器
    4.2 homework
    4.2 day12 装饰器-带参数
    3.29 homework
    SQL Server 索引和视图
    SQL Server 事务、异常和游标
    SQL Server 触发器
    SQL Server 存储过程
  • 原文地址:https://www.cnblogs.com/zhangdashuai/p/3702151.html
Copyright © 2011-2022 走看看