POJ 3414 H - Pots // bfs搜索最优路径,dfs回溯输出路径

POJ 3414  H - Pots

 

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)

bfs找路径,dfs回溯类似题目链接 ::迷宫输出路径

倒水问题类似题目链接:

题意:有三个杯子 A,B,C, 这三个杯子都装满了水,通过上面的FILL(倒满)、DROP(全部倒出去)、POUR(从一个杯子倒往另一个杯子),这三个类型的操作,倒出C杯中装有的水的量。

 

思路:在倒水的过程中,是A,B,两个杯子在不断的互相倒水,所以进行上面三个类型操作的时候,就会有6种情况。

FILL:倒满的情况 ;就存在把A倒满和把B倒满。

DROP:倒空的情况;就存在把A倒空和把B倒空。

POUR:互倒水的情况:就存在从A往B倒水和从B往A倒水。

这些情况都分析清楚了,那么在bfs中模仿这个倒水的过程就可以了。

用point结构体来存放倒水的杯子,x和y就模仿A和B倒水的那6种状态,如果不能倒出C杯中的水的话,就在bfs中返回-1,那那么主函数中ans接收返回的-1,就输出impossible。

node结构体来存放的是倒水的过程和倒水的次数,在倒水的过程中就把倒水的过程给记录了下来,那么在dfs回溯的时候,就可以把倒水的路径输出。

注意:在A杯中的水倒入B杯中时,这个时候需要注意的是,倒水的顺序一定要特别注意,现在详细的叙说一下:

1.A杯往B杯中倒水 

(1)B杯不满,但是A杯空了的情况:

    在这种情况下,要先执行把水倒入B杯的情况,再执行A空的情况,顺序不可颠倒

q.y=q.y+q.x;
q.x=0;

(2)B杯满了,但是A杯不空的情况:

    在这种情况下,要先执行A杯不空,A杯还剩多少水的情况,再执行把水倒入B杯的情况,顺序不可颠倒

q.x=q.x-(b-q.y);
q.y=b;   //如果颠倒了,就不能算出A杯中还剩多少水了

2.B杯往A杯中倒水

(1)A杯不满,但是B杯空了的情况:

    在这种情况下,要先执行B杯往A杯倒水的情况,这样A杯中的水就是A杯和B杯水的总和,然后再执行B杯空了的情况,顺序不可颠倒。

q.x+=q.y;
q.y=0;

(2)A杯满了,但是B杯不空的情况:

    在这种情况下,要先执行B杯在往A杯倒完水之后还剩下多少水,然后再执行A杯中的水满了,顺序不可颠倒

q.y-=(a-q.x);
q.x=a;

 

AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define max 110
int a,b,c;
char num[12][12]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct node
{
	int x;
	int y;
	int step;
	int op;
}v[max][max];
struct point
{
	int x;
	int y;
};
void dfs(int x,int y)
{
	if(x==0 && y==0)
	{
		return ;
	}
	dfs(v[x][y].x,v[x][y].y);
	printf("%s\n",num[v[x][y].op]);
}
int bfs(point t)
{
	queue<point>Q;
	v[0][0].step=1;
	Q.push(t);
	while(Q.size())
	{
		point s=Q.front();
		Q.pop();
		if(s.x==c||s.y==c)//如果当前的倒水状态已经达到了c的状态,那么就进行dfs遍历,来输出倒水路径 
		{
			printf("%d\n",v[s.x][s.y].step-1);
			dfs(s.x,s.y);
			return 0;
		}
		for(int i=0;i<6;i++)
        //否则进行6中倒水状态
	//1. 将a杯倒满? FILL(1)
	//2. 将b杯倒满 FILL(2)
	//3. 将a杯中的水倒出来 DROP(1)
	//4. 将b杯中的水倒出来 DROP(2)
     //5. 将a杯中的水倒入b杯中 POUR(1,2),这种情况下又分 
	//两种可能 1.将a中的水全部倒入b中(a空了但存在b不满的情况);2.将b杯倒满(b杯满了但存在a杯中还有水) 
	//6. 将b杯中的水倒入a杯中 POUR(2,1),这种情况下又分
    //两种可能 1.将b杯中的水全部倒入a中(b空了但存在a还不满的情况);2.将a杯倒满了(a杯满了但存在b杯中还有水) 

       {
			point q=s;
			if(i==0)
			{
				q.x=a;
			}
			else if(i==1)
			{
				q.y=b;
			}
			else if(i==2)
			{
				q.x=0;
			}
			else if(i==3)
			{
				q.y=0;
			}
			else if(i==4) 
			{
			if(q.x+q.y<=b)  
			                                 //全部倒入b中
            {
			       q.y=q.y+q.x;
          		   q.x=0;
                }else //将b倒满 
               { 
                 q.x=q.x-(b-q.y);
                 q.y=b;
                }
                }else{
                if(q.x+q.y<=a) //全部倒入a中
                 {
                     q.x+=q.y;
                      q.y=0;
                }else //将 a 倒满 
				{
				    q.y-=(a-q.x);
				    q.x=a;
				}
			 }if(v[q.x][q.y].step==0)
			 {
			     v[q.x][q.y].step=v[s.x][s.y].step+1;v[q.x][q.y].x=s.x;
				 v[q.x][q.y].y=s.y;v[q.x][q.y].op=i;
				 Q.push(q);
			 }
	  }
}
return -1; //表示倒水不能倒出c的这种状态 
}
int main()
{
		while(scanf("%d%d%d",&a,&b,&c)!=EOF)
	  {
	      memset(v,0,sizeof(v));
		  point s;
		  s.x=0;
		  s.y=0;
		  int ans=bfs(s);
		  if(ans==-1)
		  {
		    printf("impossible\n");
		  }
	  }
	  return 0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值