[kuangbin带你飞]专题1 简单搜索 H - Pots POJ - 3414

题目:

Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19598   Accepted: 8304  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 ≤ 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)


题意:有两个容器,一个容量为A升,一个容量为B升。有以下三种操作:

1。FILL(i)直接装满第i个容器

2,DROP(i)把第i个容器的水全倒掉。

3,POUR(i,j)把第i个容器的水全部倒入第j个容器中。(若第j个容器已经装满,则第i个容器里会剩下一部分液体)

问当仅有以上三种操作时,最少需要几步可以使任意一个瓶子里的液体含量==C升。并输出操作过程。若永远无法得到结果,输出“impossible”。


思路:这三种操作可以细分为6种:FILL(1) , FILL(2) , POUR(1,2) , POUR(2,1) , DROP(1) , DROP(2)。

BFS搜索当前状态通过以上6种操作可以到达的可行解即可。


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<set>

typedef long long ll;

using namespace std;

struct node{
	int i,j;
	string path;
};

bool vis[110][110];   //vis数组记录两个水桶的容水量分别为i,j的情况是否之前已经遍历过   

int main(){
	ios::sync_with_stdio(false);
	int A,B,C;
	cin>>A>>B>>C;
	
	queue<node> q;
	
	memset(vis,0,sizeof(vis));
	vis[0][0] = 1;  //初始状态 
	
	node a;
	a.i = A , a.j = 0 , a.path = "1";
	vis[A][0] = 1;
	q.push(a);
	
	a.i = 0 , a.j = B , a.path = "2";
	vis[0][B] = 1;
	q.push(a);      //开始的时候只能选择把A加满或者把B加满,否则将陷入毫无意义的死循环 
	
	while(!q.empty()){
		node t = q.front();
		q.pop();
		
		if(t.i==C || t.j==C){    //找到某个桶水量==c,输出路径后结束 
			int len = t.path.size();
			printf("%d\n",len);
			for(int i = 0 ; i<len ; i++){
				if(t.path[i] == '1'){
					printf("FILL(1)\n");
				}
				else if(t.path[i] == '2'){
					printf("FILL(2)\n");
				}
				else if(t.path[i] == '3'){
					printf("POUR(1,2)\n");
				}
				else if(t.path[i] == '4'){
					printf("POUR(2,1)\n");
				}
				else if(t.path[i] == '5'){
					printf("DROP(1)\n");
				}
				else if(t.path[i] == '6'){
					printf("DROP(2)\n");
				}
			}
			return 0;
		}
		else{
			node nxt1;
			nxt1.i = A , nxt1.j = t.j;   //fill 1
			if(!vis[nxt1.i][nxt1.j]){
				nxt1.path  = t.path + '1';
				vis[nxt1.i][nxt1.j] = 1;
				q.push(nxt1);
			}
			
			node nxt2; 
			nxt2.i = t.i , nxt2.j = B;    //fill 2
			if(!vis[nxt2.i][nxt2.j]){
				nxt2.path  = t.path + '2';
				vis[nxt2.i][nxt2.j] = 1;
				q.push(nxt2);
			}
			
			node nxt3;
			if(B-t.j < t.i){            //pour(1,2)
				nxt3.i = t.i - (B-t.j);
				nxt3.j = B;
			}
			else{
				nxt3.i = 0;
				nxt3.j  = t.i + t.j;
			}
			if(!vis[nxt3.i][nxt3.j]){
				nxt3.path = t.path + '3';
				vis[nxt3.i][nxt3.j] = 1;
				q.push(nxt3);
			}
			
			node nxt4;
			if(A-t.i < t.j){            //pour(2,1)
				nxt4.i = A;
				nxt4.j = t.j - (A-t.i);
			}
			else{
				nxt4.i = t.i + t.j;
				nxt4.j = 0;
			}
			if(!vis[nxt4.i][nxt4.j]){
				nxt4.path = t.path + '4';
				vis[nxt4.i][nxt4.j] = 1;
				q.push(nxt4);
			}
			
			node nxt5;
			nxt5.i = 0 , nxt5.j = t.j;    //drop(1)
			if(!vis[nxt5.i][nxt5.j]){
				nxt5.path = t.path + '5';
				vis[nxt5.i][nxt5.j] = 1;
				q.push(nxt5);
			}
			
			node nxt6;
			nxt6.j = 0 , nxt6.i = t.i;     //drop(2)
			if(!vis[nxt6.i][nxt6.j]){
				nxt6.path = t.path + '6';
				vis[nxt6.i][nxt6.j] = 1;
				q.push(nxt6);
			}
		}
	}
	printf("impossible\n");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值