码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3414 Pots

时间:2014-07-26 15:04:00      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:acm   c++   bfs   poj   

Pots

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)
思路BFS,用两个罐子的剩余水量作为状态标记

代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
int A, B, C;
int vis[105][105];
struct V{
	int a;
	int b;
	int step;
	string ans;
}p,pp;
queue <V> s;
bool bfs()
{
	while (!s.empty())
		s.pop();
	vis[0][0] = 1;
	p.a = 0;
	p.b = 0;
	p.step = 0;
	s.push(p);
	while (!s.empty())
	{
		p = s.front();
		s.pop();
		if (p.a == C || p.b == C)
			return true;
		int aa, bb;
		for (int i = 1; i <= 6; i++){
			if (1 == i)
				aa = A, bb = p.b;
			else if (2 == i)
				aa = p.a, bb = B;
			else if (3 == i){
				if (p.a + p.b > A)
					bb = p.a + p.b - A, aa = A;
				else
					bb = 0, aa = p.a + p.b;
			}
			else if (4 == i){
				if (p.a + p.b > B)
					aa = p.a + p.b - B, bb = B;
				else
					aa = 0, bb = p.a + p.b;
			}
			else if (5 == i)
				aa = 0, bb = p.b;
			else
				aa = p.a, bb = 0;
			if (!vis[aa][bb]){
				vis[aa][bb] = 1;
				pp.a = aa;
				pp.b = bb;
				pp.step = p.step + 1;
				char c = i + '0';
				pp.ans = "";
				pp.ans += (p.ans + c);
				s.push(pp);
			}
		}
	}
	return false;
}
void output(char c)
{
	if ('1' == c)
		puts("FILL(1)");
	else if ('2' == c)
		puts("FILL(2)");
	else if ('3' == c)
		puts("POUR(2,1)");
	else if ('4' == c)
		puts("POUR(1,2)");
	else if ('5' == c)
		puts("DROP(1)");
	else
		puts("DROP(2)");
}
int main()
{
	while (scanf("%d%d%d", &A, &B, &C) != EOF)
	{
		memset(vis, 0, sizeof(vis));
		if (bfs()){
			printf("%d\n", p.step);
			for (int i = 0; i < p.step; i++)
				output(p.ans[i]);
		}
		else
			puts("impossible");
	}
	return 0;
}



POJ 3414 Pots,布布扣,bubuko.com

POJ 3414 Pots

标签:acm   c++   bfs   poj   

原文地址:http://blog.csdn.net/u012964281/article/details/38143561

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!