poj3414(bfs)

http://poj.org/problem?id=3414

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


9216886

friends

POJ

3414

Accepted

32

0.9

2033

C++

2017-05-28 08:11:32

9216885

friends

POJ

3414

Memory Limit Exceeded

 

 

2036

C++

2017-05-28 08:10:48

9216883

friends

POJ

3414

Time Limit Exceeded

 

 

1936

C++

2017-05-28 08:00:48

9216881

friends

POJ

3414

Wrong Answer

 

 

2118

C++

2017-05-28 07:55:25

9216880

friends

POJ

3414

Wrong Answer

 

 

2120

C++

2017-05-28 07:54:53

9216879

friends

POJ

3414

Compile Error

 

 

2141

C++

2017-05-28 07:54:28


常见的智力题?!c-wa-t-m-a wa题日常啊

题意:给定两个水杯的容量,及目标容量,如果能够达到目标容量打印步骤,否则输出impossible。

题解:从0,0出发枚举六种倒水情况,如果水量情况未出现,就放入队列并标记

一路做下来还算顺利。但关键是将各个状态关联起来,那么可以通过保存父亲节点来实现(①保存父亲节点的地址 ②保存父亲节点的下标)

一开始没开数组,也没用头尾指针,while里面放的是队列判空,用的局部变量当的父亲节点,秀智商啊。。。

标记水量当时make_pair+set,其实一个二维的vis就够了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;

int a,b,c;

struct node{
	int watera;
	int waterb;
	int way;
	int step;
	//保存父亲节点,也可以通过保存父亲节点的数组下标 int一个pre 
	node* pre; 
};

node next[10005];
//打印链表 
void print(node* n){
	if(n->pre!=NULL){
		print(n->pre);
		if(n->way==1||n->way==2){
			printf("FILL(%d)\n",n->way);
		}
		else if(n->way==3||n->way==4){
			printf("DROP(%d)\n",n->way-2);
		}
		else if(n->way==5){
			printf("POUR(1,2)\n");
		}
		else if(n->way==6){
			printf("POUR(2,1)\n");
		}
	}
}

int bfs(){
	//根节点初始化 
	memset(next,0,sizeof(next));
	next[0].watera=0;next[0].waterb=0;
	next[0].step=0;
	next[0].pre=NULL;
	queue<node>q;
	q.push(next[0]);
	set<pair<int,int> >s;
	set<pair<int,int> >::iterator it; 
	//标记已经出现过水量的情况 
	s.insert(make_pair(0,0));
	//首尾指针 hand是父亲节点,子节点尾插在en
	int hand=0,en=1;
	while(hand<en){
		next[hand]=q.front();
		q.pop();
		if(next[hand].watera==c||next[hand].waterb==c){
			printf("%d\n",next[hand].step);
			print(&next[hand]);
			return 1;
		}
		//枚举六种情况 
		for(int i=1;i<=6;i++){
			next[en]=next[hand];
			//FILL(1)
			if(i==1){
				next[en].watera=a;
			}
			//FILL(2)
			if(i==2){
				next[en].waterb=b;
			}
			//DROP(1)
			if(i==3){
				next[en].watera=0;
			}
			//DROP(2)
			if(i==4){
				next[en].waterb=0;
			}
			//POUR(1,2)
			if(i==5){ 
				if(next[en].watera+next[en].waterb>b){
					next[en].watera-=(b-next[en].waterb);
					next[en].waterb=b;
				}
				else{
					next[en].waterb+=next[en].watera;
					next[en].watera=0;
				}
			}
			//POUR(2,1)
			if(i==6){ 
				if(next[en].watera+next[en].waterb>a){
					next[en].waterb-=(a-next[en].watera);
					next[en].watera=a;
				}
				else{
					next[en].watera+=next[en].waterb;
					next[en].waterb=0;
				}
			}
			it=s.find(make_pair(next[en].watera,next[en].waterb));
			//如果当前的水量情况没有出现过,就尾插当前结点 
			if(it==s.end()){
				next[en].step++;
				next[en].pre=&next[hand];
				next[en].way=i; 
				//标记当前结点的水量情况 
				s.insert(make_pair(next[en].watera,next[en].waterb));
				q.push(next[en++]);
			}
		}
		//父亲节点的六种情况枚举完了,头指针后移 
		hand++;
	}
	return 0;
}

int main(){
	scanf("%d%d%d",&a,&b,&c); 
	int num=bfs();
	if(num==0){
		puts("impossible");
	}
	return 0;
} 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值