【简单搜索】pots

pots

问题描述

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)

思路

记录容器i,j中的水的体积为一个结点状态,对进行六种操作后的结点进行遍历。仍然是一个bfs。起始状态中,容器i和j中是没有水的。

  • 采用散列的思想,创建一个二维数组记录该结点状态在之前是否已经到达过。数组的容量是可以确定的,1<=A, B<=100,那么这个hash数组的容量就为10000,实际操作时申请得要更大一些,以防溢出等意外情况出现。
  • 如果有可执行方案,最后还要打印每一步操作。可以再申请一个数组,其下标记录当前结点的入队序列号,其内存放前一结点的入队序列号。可以在两结点进行交接的时候记录。还要再申请一个数组(也可以把上面那个数组申请为二维的),以结点的入队序列号来映射达到该结点状态所要进行的操作。

其他就没有什么了。

代码样例

//H-Pots
#include<iostream>
#include<queue>
using namespace std;
int A, B, C;
int vist[105][105];
int pre[10010], choice[10010];
char track[6][20]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct node{
	int x, y, step, id;
}; 
void print_track(int index){
	if(pre[index]==-1) return;
	print_track(pre[index]);
	int temp=choice[index];
	cout<<track[temp]<<endl;
}
int counter;
queue<node> Q;
node current, next;
void new_push(int status){
	if(!vist[next.x][next.y]){
		counter++;
		next.id=counter;
		next.step=current.step+1;
		vist[next.x][next.y]=1;
		pre[counter]=current.id, choice[counter]=status;
		Q.push(next);
	}
} 
int bfs(){
	//初始化操作,包括入队元素计数器的清零和队列的初始化,以及初始状态的计入
	//以备多组输入
	counter=0, Q=queue<node>();
	current.x=0, current.y=0, current.step=0,current.id=0;
	pre[0]=-1, vist[0][0]=1;
	Q.push(current);
	while(Q.size()){
		current=Q.front();
		Q.pop();
		if(current.x==C||current.y==C) return current.step;
		//满A
		next.x=A, next.y=current.y;
		new_push(0);
		//满B
		next.x=current.x, next.y=B;
		new_push(1);
		//空A
		next.x=0, next.y=current.y;
		new_push(2); 
		//空B
		next.x=current.x, next.y=0;
		new_push(3);
		//A->B, B满
		if(current.x&&(current.x+current.y>B)){
			next.x=current.x-(B-current.y), next.y=B;
			new_push(4);	
		}
		//A->B, B不满
		if(current.x&&(current.x+current.y<=B)){
			next.y=current.x+current.y, next.x=0; 
			new_push(4);	
		}
		//B->A, A满
		if(current.y&&(current.x+current.y>A)){
			next.y=current.y-(A-current.x), next.x=A;
			new_push(5);	
		}
		//B->A, A不满
		if(current.y&&(current.x+current.y<=A)){
			next.x=current.x+current.y, next.y=0;
			new_push(5); 
		}
	}
	return -1;
}
int main(void){
	cin>>A>>B>>C;
	int ans=bfs();
	if(ans==-1){
		cout<<"impossible"<<endl;
	}else{
		cout<<current.step<<endl;
		print_track(current.id);
	}
	return 0;
}

总结

模拟,用程序设计语言来给问题建模的能力也很重要。。。

  • 1605集训计划第八天任务
  • 延迟1天完成
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值