POJ3414 - 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 ≤ ≤ 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 jis 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)
 
 
 
 
递归打印路径,代码能力还得有待提升,因为一个小bug卡了仨小时。今天是2017年最后一天,幸好在半夜十二点之前把这题写出来了,要不然就得留遗憾跨年了,一发过还算是今年一个好的结束吧,希望下一年再接再厉,早日成为大牛!
#include <queue>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int A, B, C;
int T;
int v[105][105];
int a[10005];
string step[7] = {"0","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct node{
	int a , b;
	int step;
}pre, nex;
struct step{
	int a;
	int b;
	int s;
}sp[105][105];
void print(int x, int y) {
	if(sp[x][y].s) {
		a[T++] = sp[x][y].s;
		print(sp[x][y].a, sp[x][y].b);
	}
	else {
		for(int i = T-1; i >= 0; i--) {
			cout<<step[a[i]]<<endl;
		}
	}
}
void bfs() {
	queue<node> q;
	pre.a = 0;
	pre.b = 0;
	pre.step = 0;
	sp[0][0].s = 0;
	v[0][0] = 1;
	q.push(pre);
	while(!q.empty()) {
		nex = q.front();
		q.pop();
		if(nex.a == C || nex.b == C) {
			printf("%d\n",nex.step);
			print(nex.a , nex.b);
			return ;
		}
		if(nex.a != A && v[A][nex.b] == 0) { //把A倒满 1 FILL(1)
			pre.a = A;
			pre.b = nex.b;
			pre.step = nex.step+1;
			v[pre.a][pre.b] = 1;
			sp[pre.a][pre.b].a = nex.a;
			sp[pre.a][pre.b].b = nex.b;
			sp[pre.a][pre.b].s = 1;
			q.push(pre);
		}
		if(nex.b != B && v[nex.a][B] == 0) { //把B倒满 2 FILL(2)
			pre.a = nex.a;
			pre.b = B;
			pre.step = nex.step+1;
			v[pre.a][pre.b] = 1;
			sp[pre.a][pre.b].a = nex.a;
			sp[pre.a][pre.b].b = nex.b;
			sp[pre.a][pre.b].s = 2;
			q.push(pre);
		}
		if(nex.a != 0 && v[0][nex.b] == 0) { //把A倒掉 3 DROP(1)
			pre.a = 0;
			pre.b = nex.b;
			pre.step = nex.step + 1;
			v[pre.a][pre.b] = 1;
			sp[pre.a][pre.b].a = nex.a;
			sp[pre.a][pre.b].b = nex.b;
			sp[pre.a][pre.b].s = 3;
			q.push(pre);
		}
		if(nex.b != 0 && v[nex.a][0] == 0) { //把B倒掉 4 DROP(2)
			pre.a = nex.a;
			pre.b = 0;
			pre.step = nex.step + 1;
			v[pre.a][pre.b] = 1;
			sp[pre.a][pre.b].a = nex.a;
			sp[pre.a][pre.b].b = nex.b;
			sp[pre.a][pre.b].s = 4;
			q.push(pre);
		}
		if(nex.a != 0 && nex.b != B) { //把A倒给B 5 POUR(1,2)
			if(nex.a >= (B - nex.b) && v[nex.a-(B-nex.b)][B] == 0) {
				pre.a = nex.a - (B-nex.b);
				pre.b = B;
				pre.step = nex.step + 1;
				v[pre.a][pre.b] = 1;
				sp[pre.a][pre.b].a = nex.a;
				sp[pre.a][pre.b].b = nex.b;
				sp[pre.a][pre.b].s = 5;
				q.push(pre);
			}
			else if(nex.a < (B - nex.b) && v[0][nex.a + nex.b] == 0){
				pre.a = 0;
				pre.b = nex.b + nex.a;
				pre.step = nex.step + 1;
				v[pre.a][pre.b] = 1;
				sp[pre.a][pre.b].a = nex.a;
				sp[pre.a][pre.b].b = nex.b;
				sp[pre.a][pre.b].s = 5;
				q.push(pre);				
			}
		}
		if(nex.a != A && nex.b != 0) { //把B倒给A 6 POUR(2,1)
			if(nex.b >= (A - nex.a) && v[A][nex.b-(A-nex.a)] == 0) {
				pre.a = A;
				pre.b = nex.b - (A-nex.a);
				pre.step = nex.step + 1;
				v[pre.a][pre.b] = 1;
				sp[pre.a][pre.b].a = nex.a;
				sp[pre.a][pre.b].b = nex.b;
				sp[pre.a][pre.b].s = 6;
				q.push(pre);
			}
			else if(nex.b < (A - nex.a) && v[nex.a+nex.b][0] == 0){
				pre.a = nex.b + nex.a;
				pre.b = 0;
				pre.step = nex.step + 1;
				v[pre.a][pre.b] = 1;
				sp[pre.a][pre.b].a = nex.a;
				sp[pre.a][pre.b].b = nex.b;
				sp[pre.a][pre.b].s = 6;
				q.push(pre);				
			}
		}
	}
	puts("impossible");
	return; 
}
int main() {
	while(~scanf("%d%d%d", &A, &B, &C)) {
		T= 0;
		memset(v, 0 , sizeof(v));
		bfs();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值