其他分享
首页 > 其他分享> > POJ-3414 Pots(倒水问题-bfs手写队列)

POJ-3414 Pots(倒水问题-bfs手写队列)

作者:互联网

Pots

Time Limit: 1000MS Memory Limit: 65536K

题目链接http://poj.org/problem?id=3414

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)


题目大意是这样的,给你三个杯子,第三个是你要实现的目标,你可以对前两个杯子进行注满(FILL)、倒空(DROP)、将一个杯子里的水倒到另一个杯子(POUR)。问你至少需要多少步数,如果无法实现,输出impossible,否则输出倒水的过程。。。
这题由于要输出路径,所以用STL模板中的队列的话不太好写,所以这里就直接用数组模拟队列了。
这里对能否实现目标的操作可能有点迷,但开始时可以先筛去一部分:

 if (a<c && b<c)
    { printf ("impossible\n");
      return 0;
    }

接下来就是队列的操作,我们可以标记一下状态,及当前a和b的状态,如果a、b再次出现,则不再进行搜索,也说明此路不通。接下来就是对倒水的枚举,总共有六种可能,空A、空B、满A、满B、A->B、B->A。
之后比较难的就是对路径的输出了,这就需要进行递归输出。我们用per和qs分别代表标记和路径状态。当然学dfs时我们就知道了per的用法了,相当于一个里面元素带了箭头的表。(先要对per进行初始化)

void print(int x)
{
    if (per[x]) print(per[x]);       
    if (qs[x]==1) printf ("FILL(1)\n");
    else if(qs[x]==2) printf ("FILL(2)\n");
    else if (qs[x]==3) printf ("DROP(1)\n");
    else if (qs[x]==4) printf ("DROP(2)\n");
    else if (qs[x]==5) printf ("POUR(1,2)\n");
    else if (qs[x]==6) printf ("POUR(2,1)\n");
 }
#include<cstdio>
#include<cstring>
using namespace std;
void print(int x);
int qa[10000],qb[10000],qs[10000],per[10000],ans[10000];
int vis[200][200];
int main()
{
    int a,b,c;
    scanf ("%d%d%d",&a,&b,&c);
    if (a<c && b<c)
    { printf ("impossible\n");
      return 0;
    }
    int head=0,tail=1;
    qa[tail]=qb[tail]=per[tail]=qs[tail]=ans[tail]=0;
    while (head<=tail)
    { head++;
      if (vis[qa[head]][qb[head]]) continue;
      vis[qa[head]][qb[head]]=1;
      if (qa[head]==c || qb[head]==c)
         { printf ("%d\n",ans[head]);
           print(head);
           return 0;
         }    
	  for (int i=1; i<=6; i++)
      {  
         if (i==1)     //倒满A
         { if (qa[head]==a) continue;
           tail++;qa[tail]=a;qb[tail]=qb[head];
           qs[tail]=i;per[tail]=head;ans[tail]=ans[head]+1;
         }
         else if (i==2)     //倒满B
         { if (qb[head]==b) continue;
           tail++;qa[tail]=qa[head];qb[tail]=b;
           qs[tail]=i;per[tail]=head;ans[tail]=ans[head]+1;
         }
         else if (i==3)       //倒空A
         { if (!qa[head]) continue;
           tail++;qa[tail]=0;qb[tail]=qb[head];
           qs[tail]=i;per[tail]=head;ans[tail]=ans[head]+1;
         }
         else if (i==4)      //倒空B
         { if (!qb[head]) continue;
           tail++;qb[tail]=0;qa[tail]=qa[head];
           qs[tail]=i;per[tail]=head;ans[tail]=ans[head]+1;
         }
         else if (i==5)    //A->B
         { if (!qa[head] || qb[head]==b) continue;
           tail++;
           if (b-qb[head]>=qa[head]) qa[tail]=0,qb[tail]=qb[head]+qa[head];
           else qa[tail]=qa[head]-(b-qb[head]),qb[tail]=b;
           qs[tail]=i;per[tail]=head;ans[tail]=ans[head]+1;
         }
         else       //B->A
         { if (!qb[head] || qa[head]==a) continue;
           tail++;
           if (a-qa[head]>=qb[head]) qb[tail]=0,qa[tail]=qa[head]+qb[head];
           else qb[tail]=qb[head]-(a-qa[head]),qa[tail]=a;
           qs[tail]=i;per[tail]=head;ans[tail]=ans[head]+1;
         }
      }
    }
    printf ("impossible\n");
   return 0;
}
void print(int x)
{
    if (per[x]) print(per[x]);
    if (qs[x]==1) printf ("FILL(1)\n");
    else if(qs[x]==2) printf ("FILL(2)\n");
    else if (qs[x]==3) printf ("DROP(1)\n");
    else if (qs[x]==4) printf ("DROP(2)\n");
    else if (qs[x]==5) printf ("POUR(1,2)\n");
    else if (qs[x]==6) printf ("POUR(2,1)\n");
 }

emmm,手写队列一波,有助于更好地理解队列。。
在这里插入图片描述

标签:qa,qs,Pots,tail,bfs,3414,qb,printf,head
来源: https://blog.51cto.com/u_15249461/2870437