zoukankan      html  css  js  c++  java
  • hdu1104 Remainder bfs找算式是否有解……

    须要注意的是,进行模运算剪枝……

    #include<iostream>
    #include<queue>
    #include<cstdlib>
    #include<cstring>
    using namespace std;
    int N,M,K,T,X;
    bool used[1010];
    struct node
    {
    	int value,step;
    	char prc[10000];
    }st;
    int remind(int a,int b)
    {
    	return (a%b+b)%b;
    }
    void init()
    {
    	T=remind(N+1,K);
    	st.value=N;
    	X=M*K;
    	st.step=0;
    	memset(used,0,sizeof(used));
    	used[st.value]=1;
    }
    void bfs()
    {
    	int i,tmp,tmp1;
    	queue<node>qq;
    	node t1,t2;
    	qq.push(st);
    	while(qq.size())
    	{
    		t1=qq.front();
    		qq.pop();
    		if(remind(t1.value,K)==T)
    		{
    			t1.prc[t1.step]='';
    			printf("%d
    %s
    ",t1.step,t1.prc);
    			return;
    		}
    		for(i=0;i<4;i++)
    		{
    			t2=t1;
    			t2.step++;
    			if(i==0)
    			{
    				tmp=(t2.value+M)%X;
    				t2.prc[t1.step]='+';
    			}
    			else if(i==1)
    			{
    				tmp=(t2.value-M)%X;
    				t2.prc[t1.step]='-';
    			}
    			else if(i==2)
    			{
    				tmp=(t2.value*M)%X;
    				t2.prc[t1.step]='*';
    			}
    			else
    			{
    				tmp=remind(t2.value,M);
    				t2.prc[t1.step]='%';
    			}
    			tmp1=remind(tmp,K);
    			if(!used[tmp1])
    			{
    				used[tmp1]=1;
    				t2.value=tmp;
    				qq.push(t2);
    			}
    		}
    	}
    	printf("0
    ");
    }
    int main()
    {
    	while(scanf("%d%d%d",&N,&K,&M)&&(N!=0||M!=0||K!=0))
    	{
    		init();
    		bfs();
    	}
    }

  • 相关阅读:
    windows常规
    oracle常规操作
    idea使用
    java-maven
    java-rabbimq
    centos7 rabbitMq 安装教程
    Leetcode 332.重新安排行程
    Leetcode 334.递增的三元子序列
    Leetcode 331.验证二叉树的前序序列化
    Leetcode 330.按要求补齐数组
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4180370.html
Copyright © 2011-2022 走看看