动态规划(3)Pots (BFS)

13 篇文章 0 订阅
3 篇文章 0 订阅
Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7809 Accepted: 3277 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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. 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 operationsK. 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)

Source

Northeastern Europe 2002, Western Subregion
 
 
今天的DP已经上升到这个高度了。。。一道很有趣的倒水游戏,用BFS做就变得简单了,麻烦的是记录最后的路径,问了学长半天,才弄懂。。。
我采用的方法是用字符数组来记录每一步的操作,比如用1~6表示6种操作,那么数组“263626”就表示 FILL(2);POUR(2,1);DROP(1);POUR(2,1);FILL(2);POUR(2,1)。
就这样,。。代码如下:
#include<stdio.h>
#include<queue>
#include<stdlib.h>
#include<string.h>
#define MAX 1000
using namespace std;
int goal,m,n,flag=1;//goal  目标;m,n两个瓶子的容量;flag 标记能否达到目标。
int p = 0;  //用于记录路径的下标
struct State        //状态
{
	int x,y;
	char op[MAX];   // 记录路径
	int step_counter;  //记录步数  
}st;
bool visit[MAX][MAX];  //记录是否已访问
bool check_state(State s)
{
	if(!visit[s.x][s.y])
		return 1;
	else 
		return 0;
};
State full1 (State a)
{
	a.x=m;
	a.op[p] = '1';
	return a;
};
State full2 (State a )
{
	a.y=n;
	a.op[p] = '2';
	return a;
}
State drop1(State a)
{
	a.x=0;
	a.op[p] = '3';
	return a;
};
State drop2(State a)
{
	a.y=0;
	a.op[p] = '4';
	return a;
};
State pour1(State a)
{
	if(a.x>n-a.y)
	{
	    a.x = a.x-n+a.y;
		a.y = n;
	}
	else
	{
	 a.y += a.x;
	 a.x = 0;
    }
	a.op[p] = '5';
	return a;
};
State pour2(State a)
{
	if(a.y>m-a.x)
	{
	    a.y = a.y+a.x-m;
		a.x = m;
	}
	else
	{
	 a.x += a.y;
	 a.y = 0;
    }
	a.op[p] = '6';
	return a;
};
void bfs(State start)
{
	queue <State> q;
	State now,next;
	q.push(start);
	start.step_counter = 0;
	visit[start.x][start.y] = 1;
	while(!q.empty())
	{
		now = q.front();
		p=now.step_counter;
		if(now.x == goal || now.y == goal)//符合条件
		{
			printf("%d\n",now.step_counter);
			flag = 0;
			for(int k=0;k<now.step_counter;k++)
			{
                switch(now.op[k]-'0')// 输出路径
                {
                         case(1): printf("FILL(1)\n");break;
                		 case(2): printf("FILL(2)\n");break;
                		 case(3): printf("DROP(1)\n");break;
                		 case(4): printf("DROP(2)\n");break;
                		 case(5): printf("POUR(1,2)\n");break;
                		 case(6): printf("POUR(2,1)\n");break;
		        }
			}
			return;
		}
		for(int i=1;i<=6;i++)
		{
			switch(i)
			{
			case(1):next = full1(now);break;//六种操作
			case(2):next = full2(now);break;
			case(3):next = drop1(now);break;
			case(4):next = drop2(now);break;
			case(5):next = pour1(now);break;
			case(6):next = pour2(now);break;
			}
			if(check_state(next))
			{
			next.step_counter = now.step_counter + 1;
				q.push(next);
				visit[next.x][next.y]= 1;
			}
		}
	q.pop();
	}
}
int main()
{
	scanf("%d %d %d",&m,&n,&goal);
	st.step_counter = st.x = st.y = 0;
	for(int h=0;h<MAX;h++)
		st.op[h] = '\0';//初始化
	bfs(st);
	if(flag) printf("impossible\n");
	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值