POJ3414(Pots)

Pots

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 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)

思路

倒水問題,比杭電那個非常可樂要簡單一點。這是一個special judge,操作比較簡單

  1. FILL(i) 將第i個容器從水龍頭裏裝滿(1 ≤ i ≤ 2);
  2. DROP(i) 將第i個容器抽乾
  3. POUR(i,j) 將第i個容器裏的水倒入第j個容器(這次操作結束後產生兩種結果,一是第j個容器倒滿並且第i個容器依舊有剩餘,二是第i個容器裏的水全部倒入j中,第i個容器爲空)
    寫好狀態轉移,數值都比較小判重也簡單。
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
	int x,y,t;
	int f[200];
};
queue<node>q;
bool visited[10000];
void bfs(int a,int b,int c)
{
	while(!q.empty()){
		node ptr = q.front(),p;
		q.pop();
		if(ptr.x == c || ptr.y == c){
			printf("%d\n",ptr.t);
			for(int i = 1;i <= ptr.t;i++){
				if(ptr.f[i] == 1){
					printf("FILL(1)\n");
				}
				if(ptr.f[i] == 2){
					printf("FILL(2)\n");
				}
				if(ptr.f[i] == 3){
					printf("DROP(1)\n");
				}
				if(ptr.f[i] == 4){
					printf("DROP(2)\n");
				}
				if(ptr.f[i] == 5){
					printf("POUR(1,2)\n");
				}
				if(ptr.f[i] == 6){
					printf("POUR(2,1)\n");
				}
			}
			return ;
		}
		p = ptr;
		for(int i = 0;i < 6;i++){
			if(i == 0){				//給a加滿 
				p.x = a;
				p.y = ptr.y;
				int t = p.x*100+p.y;
				if(visited[t] == false){
					p.t = ptr.t + 1;
					p.f[p.t] = 1;
					visited[t] = true;
					q.push(p);
				}
			}
			if(i == 1){				//給b加滿 
				p.x = ptr.x;
				p.y = b;
				int t = p.x*100+p.y;
				if(visited[t] == false){
					p.t = ptr.t + 1;
					p.f[p.t] = 2;
					visited[t] = true;
					q.push(p);
				}
			}
			if(i == 2){				//把a倒掉 
				p.x = 0;
				p.y = ptr.y;
				int t = p.x*100+p.y;
				if(visited[t] == false){
					p.t = ptr.t + 1;
					p.f[p.t] = 3;
					visited[t] = true;
					q.push(p);
				}
			}
			if(i == 3){				//把b倒掉 
				p.x = ptr.x;
				p.y = 0;
				int t = p.x*100+p.y;
				if(visited[t] == false){
					p.t = ptr.t + 1;
					p.f[p.t] = 4;
					visited[t] = true;
					q.push(p);
				}
			}
			if(i == 4){				//a給b倒 
				if(ptr.y < b){
					if(ptr.x <= b - ptr.y){
						p.x = 0;
						p.y = ptr.x + ptr.y;
					}
					else{
						p.x = ptr.x - (b - ptr.y);
						p.y = b;
					}
					int t = p.x*100+p.y;
					if(visited[t] == false){
						p.t = ptr.t + 1;
						p.f[p.t] = 5;
						visited[t] = true;
						q.push(p);
					}
				}
			}
			if(i == 5){				//b給a倒 
				if(ptr.x < a){
					if(ptr.y <= a - ptr.x){
						p.x = ptr.x + ptr.y;
						p.y = 0;
					}
					else{
						p.x = a;
						p.y = ptr.y - (a - ptr.x);
					}
					int t = p.x*100+p.y;
					if(visited[t] == false){
						p.t = ptr.t + 1;
						p.f[p.t] = 6;
						visited[t] = true;
						q.push(p);
					}
				}
			}
		}
	}
	printf("impossible\n");
}
int main()
{
	int a,b,c;
	while(~scanf("%d%d%d",&a,&b,&c)){
		while(!q.empty()){
			q.pop();
		}
		node p;
		p.x = 0;p.y = 0;p.t = 0;
		memset(p.f,0,sizeof(p.f));
		memset(visited,0,sizeof(visited));
		q.push(p);
		bfs(a,b,c);
	}
	return 0;
}

願你走出半生,歸來仍是少年~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章