zoukankan      html  css  js  c++  java
  • 【BFS】Pots

    [poj3414]Pots
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 16925   Accepted: 7168   Special Judge

    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)

    Source

    题目大意:两只杯子,分别N容量M容量,问要怎样凑出K容量

                    DROP(x):倒空x杯

                    FILL(x):倒满x被

                    POUR(x,y):将x杯中的水倒入y

    试题分析:思路很明确的一道题,直接写就行,就算锻炼代码能力了  难得1AC

    代码

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<algorithm>
    //#include<cmath>
    
    using namespace std;
    const int INF = 9999999;
    #define LL long long
    
    inline int read(){
    	int x=0,f=1;char c=getchar();
    	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    	return x*f;
    }
    int N,M,K;
    struct data{
    	int a,b,fr,st;
        int fg;
    }Que[200001];
    int l=1,r=1;
    /*
    1:Fill(1)
    2:Fill(2)
    3:POUR(1,2)
    4:ROUP(2,1)
    5:DROP(1)
    6:DROP(2)
    */
    bool vis[201][201];
    bool alflag=false;
    
    void print(data k){
    	if(k.fr!=-1){
    		print(Que[k.fr]);
    		if(k.fg==1) puts("FILL(1)");
    		if(k.fg==2) puts("FILL(2)");
    		if(k.fg==3) puts("POUR(1,2)");
    		if(k.fg==4) puts("POUR(2,1)");
    		if(k.fg==5) puts("DROP(1)");
    		if(k.fg==6) puts("DROP(2)");
    	}
    }
    
    void BFS(){
    	Que[l].a=0,Que[l].b=0,Que[l].fr=-1,Que[l].st=0;
    	vis[0][0]=true;
    	int x,y,step;
    	while(l<=r){
    		x=Que[l].a,y=Que[l].b,step=Que[l].st;
    		if(x!=N&&!vis[N][y]){
    			vis[N][y]=true;
    			Que[++r].fg=1;
    			Que[r].a=N;
    			Que[r].b=y;
    			Que[r].st=step+1;
    			Que[r].fr=l;
    			if(Que[r].a==K||Que[r].b==K){
    				printf("%d
    ",step+1);
    				print(Que[r]);
    				alflag=true;
    				return ;
    			}
    		}
    		if(y!=M&&!vis[x][M]){
    			vis[x][M]=true;
    			Que[++r].fg=2;
    			Que[r].a=x;
    			Que[r].b=M;
    			Que[r].st=step+1;
    			Que[r].fr=l;
    			if(Que[r].a==K||Que[r].b==K){
    				printf("%d
    ",step+1);
    				print(Que[r]);
    				alflag=true;
    				return ;
    			}
    		}
    		if(x&&y!=M){
    			int xx,yy;
    			if(x-(M-y)<=0) yy=y+x,xx=0;
    			else yy=M,xx=x-(M-y);
    			if(!vis[xx][yy]){
    			    vis[xx][yy]=true;
    			    Que[++r].fg=3;
    		    	Que[r].a=xx;
    		    	Que[r].b=yy;
    		    	Que[r].st=step+1;
    	    		Que[r].fr=l;
    	    		if(Que[r].a==K||Que[r].b==K){
    					printf("%d
    ",step+1);
    					print(Que[r]);
    					alflag=true;
    					return ;
    				}
    	    	}
    		}
    		if(y&&x!=N){
    			int xx,yy;
    			if(y-(N-x)<=0) xx=y+x,yy=0;
    			else xx=N,yy=y-(N-x);
    			if(!vis[xx][yy]){
    			    vis[xx][yy]=true;
    			    Que[++r].fg=4;
    		    	Que[r].a=xx;
    		    	Que[r].b=yy;
    		    	Que[r].st=step+1;
    	    		Que[r].fr=l;
    	    		if(Que[r].a==K||Que[r].b==K){
    				    printf("%d
    ",step+1);
    			    	print(Que[r]);
    			    	alflag=true;
    			    	return ;
    			    }
    	    	}
    		}
    		if(x&&!vis[0][y]){
    			vis[0][y]=true;
    			Que[++r].fg=5;
    			Que[r].a=0;
    			Que[r].b=y;
    			Que[r].st=step+1;
    			Que[r].fr=l;
    			if(Que[r].a==K||Que[r].b==K){
    				printf("%d
    ",step+1);
    				print(Que[r]);
    				alflag=true;
    				return ;
    			}
    		}
    		if(y&&!vis[x][0]){
    			vis[x][0]=true;
    			Que[++r].fg=6;
    			Que[r].a=x;
    			Que[r].b=0;
    			Que[r].st=step+1;
    			Que[r].fr=l;
    			if(Que[r].a==K||Que[r].b==K){
    				printf("%d
    ",step+1);
    				print(Que[r]);
    				alflag=true;
    				return ;
    			}
    		}
    		l++;
    	}
    }
    
    int main(){
    	//freopen(".in","r",stdin);
    	//freopen(".out","w",stdout);
    	N=read(),M=read(),K=read();
    	BFS();
    	if(!alflag){
    		printf("impossible");
    		return 0;
    	}
    	return 0;
    }
  • 相关阅读:
    Python
    12C配置EM Express的https端口
    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
    Java – How to add days to current date
    Java – Display all ZoneId and its UTC offset
    Java 8 – Period and Duration examples
    Java 8 – Convert Instant to ZonedDateTime
    Java 8 – Convert Instant to LocalDateTime
    Java 8 – How to format LocalDateTime
    how-to-convert-string-to-localdate
  • 原文地址:https://www.cnblogs.com/wxjor/p/6985293.html
Copyright © 2011-2022 走看看