• HDOJ-三部曲一(搜索、数学)-1010-Pots


    Pots

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 21   Accepted Submission(s) : 9
    Special Judge
    Problem Description

    You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

    1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
    2. DROP(i)      empty the pot i to the drain;
    3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

    Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

     
    Input

    On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

     
    Output

    The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

     
    Sample Input
    3 5 4
     
    Sample Output
    6
    FILL(2)
    POUR(2,1)
    DROP(1)
    POUR(2,1)
    FILL(2)
    POUR(2,1)
     
    Source
    PKU
     
     
     
    BFS,用A中的水量乘1000加上B中的水量标记出现过的状态。用指针记录上一步操作的地址。
     
     
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    struct status                          //用于存储每一步操作后的状态
    {
    	int x,y;
    	int op;                            //用于存储这一步的操作,用数字表示
    	status *last;                      //用于存储上一步操作的位置
    };
    
    int a,b,c;
    int operate[1000];                     //用于最后统计各步操作的     
    status que[1000];
    bool f[100111];                        //用来标记各步操作的结果是否出现过
    
    
    status BFS()
    {
    	int front=0,rear=1;
    	que[0].x=0;      
    	que[0].y=0;
    	que[0].op=0;
    	que[0].last=NULL;                 //用来标记初始状态
    	f[que[0].x*1000+que[0].y]=true;   
    	while(front<rear)
    	{
    		if(que[front].x!=a)
    		{
    			que[rear]=que[front];
    			que[rear].x=a;
    			if(!f[que[rear].x*1000+que[rear].y])   //如果没出现过这种状态 
    			{
    				f[que[rear].x*1000+que[rear].y]=true;
    				que[rear].last=&que[front];
    				que[rear].op=1;
    				if(que[rear].x==c||que[rear].y==c)
    					return que[rear];
    				rear++;
    			}
    		}
    		if(que[front].y!=b)
    		{
    			que[rear]=que[front];
    			que[rear].y=b;
    			if(!f[que[rear].x*1000+que[rear].y])
    			{
    				f[que[rear].x*1000+que[rear].y]=true;
    				que[rear].last=&que[front];
    				que[rear].op=2;
    				if(que[rear].x==c||que[rear].y==c)
    					return que[rear];
    				rear++;
    			}
    		}
    		if(que[front].x!=0)
    		{
    			que[rear]=que[front];
    			que[rear].x=0;
    			if(!f[que[rear].x*1000+que[rear].y])
    			{
    				f[que[rear].x*1000+que[rear].y]=true;
    				que[rear].last=&que[front];
    				que[rear].op=3;
    				if(que[rear].x==c||que[rear].y==c)
    					return que[rear];
    				rear++;
    			}
    		}
    		if(que[front].y!=0)
    		{
    			que[rear]=que[front];
    			que[rear].y=0;
    			if(!f[que[rear].x*1000+que[rear].y])
    			{
    				f[que[rear].x*1000+que[rear].y]=true;
    				que[rear].last=&que[front];
    				que[rear].op=4;
    				if(que[rear].x==c||que[rear].y==c)
    					return que[rear];
    				rear++;
    			}
    		}
    		if(que[front].x!=0&&que[front].y!=b)
    		{
    			if(que[front].x+que[front].y<=b)
    			{
    				que[rear].x=0;
    				que[rear].y=que[front].x+que[front].y;
    			}
    			else 
    			{
    				que[rear].x=que[front].x-(b-que[front].y);
    				que[rear].y=b;
    			}
    			if(!f[que[rear].x*1000+que[rear].y])
    			{
    				f[que[rear].x*1000+que[rear].y]=true;
    				que[rear].last=&que[front];
    				que[rear].op=5;
    				if(que[rear].x==c||que[rear].y==c)
    					return que[rear];
    				rear++;
    			}
    		}
    		if(que[front].x!=a&&que[front].y!=0)
    		{
    			if(que[front].x+que[front].y<=a)
    			{
    				que[rear].x=que[front].x+que[front].y;
    				que[rear].y=0;
    			}
    			else
    			{
    				que[rear].x=a;
    				que[rear].y=que[front].y-(a-que[front].x);
    			}
    			if(!f[que[rear].x*1000+que[rear].y])
    			{
    				f[que[rear].x*1000+que[rear].y]=true;
    				que[rear].last=&que[front];
    				que[rear].op=6;
    				if(que[rear].x==c||que[rear].y==c)
    					return que[rear];
    				rear++;
    			}
    		}
    		front++;
    	}
    	status no_ans;                                   //如果while循环中没出现过答案
    	no_ans.op=-1;                                    //标记没有结果 
    	return no_ans;
    }
    
    
    int main()
    {
    	while(cin>>a>>b>>c)
    	{
    		memset(f,false,sizeof(f));
    		memset(operate,0,sizeof(operate));
    		status ans=BFS();
    		int i=0;
    		if(ans.op==-1)
    			cout<<"impossible"<<endl;
    		else
    		{
    			status *p=&ans;
    			while(p->op!=0)
    			{
    				operate[i++]=p->op;               //倒序记录各步操作
    				p=p->last;
    			}
    			cout<<i<<endl;
    			for(i--;i>=0;i--)                     //正序输出
    			{
    				switch(operate[i])
    				{
    				case 1:cout<<"FILL(1)"<<endl;break;
    				case 2:cout<<"FILL(2)"<<endl;break;
    				case 3:cout<<"DROP(1)"<<endl;break;
    				case 4:cout<<"DROP(2)"<<endl;break;
    				case 5:cout<<"POUR(1,2)"<<endl;break;
    				case 6:cout<<"POUR(2,1)"<<endl;break;
    				}
    			}
    		}
    	}
    }
    
     
  • 相关阅读:
    服务器性能测试实时监控Linux命令
    软件性能测试中的关键指标
    递归静态路由和直连静态路由
    FIB表与RIB表的区别与联系
    FIB表中 Next Hop 的几种状态码(drop/receive/attached/no route)的含义
    Leetcode 与树(TreeNode )相关的题解测试工具函数总结
    centos 7.6 docker 安装 nextcloud -使用sqlite数据库
    看守所收押流程
    qt编译oracle驱动,qt 5.12 连接 oracle 数据库示例代码
    centos 7.6 安装配置nginx (显示中文目录,带密码验证)
  • 原文地址:https://www.cnblogs.com/aljxy/p/3345366.html
Copyright © 2020-2023  润新知