POJ 3414--Pots(BFS+回溯路径)

Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9963   Accepted: 4179   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)


和之前做过的一道二维bfs一样。仅仅只是这个须要回溯路径。非常easy在结构体中加一个变量来记录上一个状态在队列中的下标(手敲的队列比較好。这个时候在用STL队列好像不慷慨便)最后找到满足条件的状态逆向打印路径(由于记录的都是上一个状态)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int m,n,c,s,e,p;
typedef struct node
{
	int v1,v2,cur,pre,op;
};
bool vis[999][999];
int ans[10010];
node que[10010];
void bfs()
{
	p=0;s=0;e=0;int pos;
	node t={0,0,0,0,0};
	que[e++]=t;
	vis[0][0]=1;
	while(s<e)
	{
		node f=que[s];pos=s;s++;
		if(f.v1==c||f.v2==c)
		{
			printf("%d\n",f.op);
			int tem=pos;
			for(int i=0;i<f.op;i++)
			{
               ans[p++]=que[tem].cur;
               tem=que[tem].pre;
			}
			for(int i=p-1;i>=0;i--)
			{
				switch(ans[i])
				{
					case 1:printf("FILL(1)\n");break;
					case 2:printf("FILL(2)\n");break;
					case 3:printf("DROP(1)\n");break;
					case 4:printf("DROP(2)\n");break;
					case 5:printf("POUR(2,1)\n");break;
					case 6:printf("POUR(1,2)\n");break;
				}
			}
			return ;
		}
		if(f.v1!=m)
		{
			t.v1=m;
			t.op=f.op+1;
			t.v2=f.v2;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 t.cur=1;
			 t.pre=pos;
			 que[e++]=t;
			}
		}
		if(f.v2!=n)
		{
			t.v2=n;
			t.op=f.op+1;
			t.v1=f.v1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 t.cur=2;
			t.pre=pos;
			 que[e++]=t;
			}
		}
		if(f.v1!=0)
		{
			t.v1=0;
			t.v2=f.v2;
			t.op=f.op+1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 t.cur=3;
			 t.pre=pos;
			 que[e++]=t;
			}
		}
		if(f.v2!=0)
		{
			t.v2=0;
			t.v1=f.v1;
			t.op=f.op+1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 t.cur=4;
			 t.pre=pos;
			 que[e++]=t;
			}
		}
		if(f.v2!=0&&f.v1!=m)
		{
			t.v2=f.v2-(m-f.v1);if(t.v2<0) t.v2=0;
			t.v1=f.v1+f.v2;  if(t.v1>m) t.v1=m;
			t.op=f.op+1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 t.cur=5;
			 t.pre=pos;
			 que[e++]=t;
			}
		}
		if(f.v1!=0&&f.v2!=n)
		{
			t.v1=f.v1-(n-f.v2);if(t.v1<0) t.v1=0;
			t.v2=f.v2+f.v1;  if(t.v2>n) t.v2=n;
			t.op=f.op+1;
			if(!vis[t.v1][t.v2])
			{
			 vis[t.v1][t.v2]=1;
			 t.cur=6;
			 t.pre=pos;
			 que[e++]=t;
			}
		}
	}
	puts("impossible");
}
int main()
{
	while(cin>>m>>n>>c)
	{
		memset(vis,0,sizeof(vis));
		bfs();
	}
	return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-09-29 01:50:16

POJ 3414--Pots(BFS+回溯路径)的相关文章

POJ 3414 Pots (BFS + 记录路径)

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10300 Accepted: 4362 Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill th

POJ 3414 Pots(bfs打印路径)

题意:给定两个空桶的容量分别为A.B,经过6种操作使获得C升水,求最少操作数: 思路:广搜.最少操作数很简单,练习一下打印路径:打印最短路劲就是每次记录当前状态和上一步,找到终点后查找路径. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f int A,B,C; int shortest[150][150];//更新最短步数

poj 3414 pots (bfs+路径记录)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 4955   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

POJ 3414 Pots(BFS 倒水)

题意  你有两个容积分别为a,b杯子  你每次可以将某个杯子中的水倒满或者倒掉或者倒到另一个杯子  问能否通过这两个杯子量出c容量的水 和上一个倒可乐问题类似  只是这个操作更多了点  将两个杯子中各含有的水作为状态  每出队列一个状态  将所有可能到达的状态入队  直到有一个杯子里面水的体积为c   打印路径直接递归就行了 #include <map> #include <cstdio> #include <cstring> using namespace std;

poj 3414 Pots(BFS)(简单题)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11551   Accepted: 4900   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

POJ 3414 Pots (BFS/DFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fi

广搜+输出路径 POJ 3414 Pots

POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)

POJ 3984-迷宫问题--BFS+回溯路径

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7862   Accepted: 4615 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

POJ 3414 Pots (经典bfs )

Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from