zoukankan      html  css  js  c++  java
  • poj3414 Pots

    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 ≤ ≤ 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 AB, 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)
    这道题可以用宽搜做,思路挺简单的,就是写起来比较复杂,一个小错误让我改了4个小时的代码。。。
    #include<stdio.h>
    #include<string.h>
    int a,b,k,num,e[111111];
    int vis[105][105];
    struct node
    {
    	int x,y,from,num,caozuo;   //from记录这个点的父亲,num表示执行了多少次,caozuo为1~6,每一个操作对应一种输出 
    }q[111111];
    int bfs()
    {
    	int front=1,rear=1,x,xx,y,yy,i,flag;
    	q[1].x=0;q[1].y=0;q[1].num=0;q[1].from=-1;q[1].caozuo=-1;vis[0][0]=1;
    	while(front<=rear)
    	{
    		x=q[front].x;y=q[front].y;
    		if(x==k || y==k){
    			num=front;
    			return q[front].num;
    		}
    		front++;
    		if(x!=a){
    			xx=a;yy=y;
    			if(vis[xx][yy]==0)
    			{vis[xx][yy]=1;rear++;q[rear].x=xx;q[rear].y=yy;q[rear].from=front-1;q[rear].num=q[front-1].num+1;q[rear].caozuo=1;}
    		}
    		if(y!=b){
    			xx=x;yy=b;
    			if(vis[xx][yy]==0)
    			{vis[xx][yy]=1;rear++;q[rear].x=xx;q[rear].y=yy;q[rear].from=front-1;q[rear].num=q[front-1].num+1;q[rear].caozuo=2;}
    		}
    		if(x!=0){
    			xx=0;yy=y;
                if(vis[xx][yy]==0)
    			{vis[xx][yy]=1;rear++;q[rear].x=xx;q[rear].y=yy;q[rear].from=front-1;q[rear].num=q[front-1].num+1;q[rear].caozuo=3;}
    		}
    		if(y!=0){
    	        xx=x;yy=0;
                if(vis[xx][yy]==0)
    			{vis[xx][yy]=1;rear++;q[rear].x=xx;q[rear].y=yy;q[rear].from=front-1;q[rear].num=q[front-1].num+1;q[rear].caozuo=4;}
    		}
    		if(x!=0){
    			if(y<b)
    			{
    			 if(x+y<=b){
    			    xx=0;yy=x+y;
                    if(vis[xx][yy]==0)
    			  {vis[xx][yy]=1;rear++;q[rear].x=xx;q[rear].y=yy;q[rear].from=front-1;q[rear].num=q[front-1].num+1;q[rear].caozuo=5;}
    			 }
    			 else if(x+y>b){
    			    xx=x+y-b;yy=b;
                    if(vis[xx][yy]==0)
    			   {vis[xx][yy]=1;rear++;q[rear].x=xx;q[rear].y=yy;q[rear].from=front-1;q[rear].num=q[front-1].num+1;q[rear].caozuo=5;}
    			 }
    			}
    		}
    		if(y!=0){
    			if(x<a)
    			{
    			 if(y+x<=a){
    			 xx=x+y;yy=0;
                 if(vis[xx][yy]==0)
    			  {vis[xx][yy]=1;rear++;q[rear].x=xx;q[rear].y=yy;q[rear].from=front-1;q[rear].num=q[front-1].num+1;q[rear].caozuo=6;}
    			 }
    			 else if(x+y>a){
    			 xx=a;yy=x+y-a;
                 if(vis[xx][yy]==0)
    			  {vis[xx][yy]=1;rear++;q[rear].x=xx;q[rear].y=yy;q[rear].from=front-1;q[rear].num=q[front-1].num+1;q[rear].caozuo=6;}
    			 }
    			}
    		}
    	}
    	return -1;
    }
    
    
    int main()
    {
    	int i,j,t,tt;
    	while(scanf("%d%d%d",&a,&b,&k)!=EOF)
    	{
    		memset(vis,0,sizeof(vis));
    		tt=bfs();
    		if(tt==-1){
    			printf("impossible
    ");continue;
    		}
    		printf("%d
    ",tt);
    		t=0;
    		while(num!=1)
    		{
    			e[++t]=q[num].caozuo;
    			num=q[num].from;
    		}
    		for(i=t;i>=1;i--){
    			if(e[i]==1)printf("FILL(1)
    ");
    			else if(e[i]==2)printf("FILL(2)
    ");
    			else if(e[i]==3)printf("DROP(1)
    ");
    			else if(e[i]==4)printf("DROP(2)
    ");
    			else if(e[i]==5)printf("POUR(1,2)
    ");
    			else if(e[i]==6)printf("POUR(2,1)
    ");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    11.2hadoop监控:日志配置、堆栈跟踪、度量和JMX
    11.1.3hadoop工具dfsadmin、fsck、数据库扫描器、均衡器
    11.1.2hadoop 安全模式
    11.1.1namenode和datanode的数据结构和格式以及镜像fsimage和编辑日志edit
    10.5 hadoop集群基准评测程序测试
    10.4 hadoop安全性kerberos安全验证和委托令牌
    零基础学习python_生成器(49课)
    安全测试5_服务端的安全漏洞(SQL注入、命令注入、文件操作类)
    安全测试4_客户端的安全漏洞(XSS、CSRF、点击劫持、URL跳转)
    零基础学习python_魔法方法(41-48课)(迭代器)
  • 原文地址:https://www.cnblogs.com/herumw/p/9464815.html
Copyright © 2011-2022 走看看