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

POJ3414——Pots

2019年02月19日 ⁄ 综合 ⁄ 共 3455字 ⁄ 字号 评论关闭
Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10021   Accepted: 4209   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 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)

Source

Northeastern Europe 2002, Western Subregion

啊哈,和杭电的一题差不多,每个状态拿过来最多可以新产生6个状态,做好剪枝和标记,就可以AC了

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

bool vis[105][105];
 
struct node
{
    int A, B;
    int num;
    int code;
    int from, to;
    int cnt;
};

node pre[1001000];

void change(node &temp2, node &temp1, int t)
{
	temp2.A = temp1.A;
	temp2.B = temp1.B;
	temp2.cnt = temp1.cnt + 1;
	temp2.num = temp1.num + t;
	pre[temp2.num] = temp1;
}

node bfs(int A, int B, int C)
{
	queue< node >qu;
	memset( vis, 0, sizeof(vis) );
	while( !qu.empty() )
		qu.pop();
    int cnt = 1;
	node temp1, temp2, ans;
	bool flag = false;
	temp1.A = 0;
	temp1.B = 0;
	temp1.num = 0;//????
	temp1.cnt = 0;
	temp1.code = -1;//????,
	//pre[temp1.num] = temp1;
	qu.push(temp1);
	while( !qu.empty() )
	{
		temp1 = qu.front();
		qu.pop();
		if( temp1.A == C || temp1.B == C)
		{
			flag = true;
			ans = temp1;
			break;
		}
		if(temp1.A != 0)//??A???
		{
		    change(temp2, temp1, cnt ++);
			temp2.A = 0;
			temp2.code = 2;
			temp2.from = 1;
			if( !vis[temp2.A][temp2.B] )
			{
				vis[temp2.A][temp2.B] = 1;
				qu.push(temp2);
			}
		}
		if(temp1.B != 0)
		{
		    change(temp2, temp1, cnt ++);
			temp2.B = 0;
			temp2.code = 2;
			temp2.from = 2;
			if( !vis[temp2.A][temp2.B] )
			{
				vis[temp2.A][temp2.B] = 1;
				qu.push(temp2);
			}
		}
        if(temp1.A != A)//???,????
		{
		    change(temp2, temp1, cnt ++);
			temp2.A = A;
			temp2.code = 1;
			temp2.from = 1;
			if( !vis[temp2.A][temp2.B] )
			{
				vis[temp2.A][temp2.B] = 1;
				qu.push(temp2);
			}
		}
		if(temp1.B != B)//???,????
		{
		    change(temp2, temp1, cnt ++);
			temp2.B = B;
			temp2.code = 1;
			temp2.from = 2;
			if( !vis[temp2.A][temp2.B] )
			{
				vis[temp2.A][temp2.B] = 1;
				qu.push(temp2);
			}
		}
		if( temp1.A != 0 && temp1.B != B)//A??,B??,?A?B
		{
		    change(temp2, temp1, cnt ++);
			temp2.code = 3;
			temp2.from = 1;
			temp2.to = 2;
			if(temp2.A + temp2.B <= B)
			{
				temp2.B += temp2.A;
				temp2.A = 0;
				if( !vis[temp2.A][temp2.B] )
				{
					vis[temp2.A][temp2.B] = 1;
					qu.push(temp2);
				}
			}
			else
			{
				temp2.A -= (B - temp2.B);
				temp2.B = B;
				if( !vis[temp2.A][temp2.B] )
				{
					vis[temp2.A][temp2.B] = 1;
					qu.push(temp2);
				}
			}
		}
		if( temp1.B != 0 && temp1.A != A)//B??,A??,?B?A
		{
		    change(temp2, temp1, cnt ++);
			temp2.code = 3;
			temp2.from = 2;
			temp2.to = 1;
			if(temp2.A + temp2.B <= A)
			{
				temp2.A += temp2.B;
				temp2.B = 0;
				if( !vis[temp2.A][temp2.B] )
				{
					vis[temp2.A][temp2.B] = 1;
					qu.push(temp2);
				}
			}
			else
			{
				temp2.B -= (A - temp2.A);
				temp2.A = A;
				if( !vis[temp2.A][temp2.B] )
				{
					vis[temp2.A][temp2.B] = 1;
					qu.push(temp2);
				}
			}
		}
	}
	if(flag)
		return ans;
	ans.num = -1;
	return ans;
}

void print(node temp)
{
	if(temp.num != 0)
		print( pre[temp.num] );
	if(temp.code == 1)
	{
		printf("FILL(%d)\n", temp.from);
	}
	else if(temp.code == 2)
	{
		printf("DROP(%d)\n", temp.from);
	}
	else if(temp.code == 3)
	{
		printf("POUR(%d,%d)\n", temp.from, temp.to);
	}
}

int main()
{
	int A, B, C;
	while(~scanf("%d%d%d", &A, &B, &C))
	{
		node ans = bfs(A, B, C);
		if(ans.num == -1)
		{
			printf("impossible\n");
			continue;
		}
		printf("%d\n", ans.cnt);
		print(ans);
	}
	return 0;
}

抱歉!评论已关闭.