现在的位置: 首页 > 综合 > 正文

POJ 3414 pots

2013年10月14日 ⁄ 综合 ⁄ 共 2606字 ⁄ 字号 评论关闭

Description Special Judge

You are given two pots, having the volume of AandB 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 pot i is empty (and     all its contents have been moved to the potj).

Write a program to find the shortest possible sequenceof these operations that will yield exactlyC liters of water in one ofthe 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 andC≤max(A,B).

Output

The first line of the output must contain the lengthof the sequence of operationsK. The following
K lines must eachdescribe 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 andonly 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、2号杯子,通过装满1或2,将1中水倒入2中,2中水倒入1中,将1中水倒掉,将2中水倒掉,六个操作,来得到C升水。

方法:广度优先搜索。觉得自己的方法有些复杂,又用了数组又用了队列,感觉两者功能上有些重复了,可以进一步简化。反正大概就是这样嘛。

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

struct operate
{
	int num1;
	int num2;
	int op;
	int front;
}pot[100001];

int flag[110][110], j = 1;

queue<int> Q;

void solve(int x, int y, int z, int m)
{
	if(flag[x][y])
		return;
	flag[x][y] = 1;
	pot[j].num1 = x;
	pot[j].num2 = y;
	pot[j].op = z;
	pot[j].front = m;
	Q.push(j);
	j++;
};

void Printf(int x)
{
	int count = 0;
	int op[100001];
	while(x!=0)
	{
		op[count++] = pot[x].op;
		x = pot[x].front;
	}
	printf("%d\n",count);
	for(int i = count - 1;i>=0;i--)
	{
		if(op[i]==1)
		{
			printf("FILL(1)\n");
		}
		else if(op[i]==2)
		{
			printf("FILL(2)\n");
		}
		else if(op[i]==3)
		{
			printf("DROP(1)\n");
		}
		else if(op[i]==4)
		{
			printf("DROP(2)\n");
		}
		else if(op[i]==5)
		{
			printf("POUR(2,1)\n");
		}
		else if(op[i]==6)
		{
			printf("POUR(1,2)\n");
		}
	}
};

int BFS(int a,int b, int c)
{
	
	pot[0].num1 = 0;
	pot[0].num2 = 0;
	pot[0].op = 0;
	pot[0].front = -1;
	flag[0][0] = 1;
	Q.push(0);
	while(!Q.empty())
	{
		int temp  = Q.front();
		Q.pop();
		if(pot[temp].num1==c||pot[temp].num2==c)
		{
			Printf(temp);
			return 1;
		}

		int x, y;
		for(int k = 1;k<=6;k++)
		{
			switch(k)
			{
			case 1:
				x = a;
				y = pot[temp].num2;
				solve(x,y,k,temp);	
				break;
			case 2:
				x = pot[temp].num1;
				y = b;
				solve(x,y,k,temp);
				break;
			case 3:
				x = 0;
				y = pot[temp].num2;
				solve(x,y,k,temp);
				break;
			case 4:
				x = pot[temp].num1;
				y = 0;
				solve(x,y,k,temp);
				break;
			case 5:
				if(pot[temp].num1+pot[temp].num2>a)
				{
					x = a;
					y = pot[temp].num2 + pot[temp].num1 - a;
				}
				else
				{
					x = pot[temp].num1+pot[temp].num2;
					y = 0;
				}
				solve(x,y,k,temp);
				break;
			case 6:
				if(pot[temp].num1+pot[temp].num2>b)
				{
					x = pot[temp].num1 - b + pot[temp].num2;
					y = b;
				}
				else
				{
					x = 0;
					y = pot[temp].num1+pot[temp].num2;
				}
				solve(x,y,k,temp);
				break;
			}
		}
	}
	return -1;
};

int main()
{
	int A, B, C;
	int ans;
	scanf("%d%d%d",&A, &B, &C);
	ans = BFS(A, B, C);
	if(ans==-1)
	{
		printf("impossible\n");
	}
	return 0;
}

 

抱歉!评论已关闭.