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

POJ 3414-Pots 简单搜索 BFS

2013年08月03日 ⁄ 综合 ⁄ 共 2854字 ⁄ 字号 评论关闭

题目来源:http://acm.pku.edu.cn/JudgeOnline/problem?id=3414

 

解题报告:

 

还是BFS

 

设(i,j)为瓶1与瓶2在某一时刻的容量,那么从这点出发,可以到达的点有:

 

(A, j) : FILL(1)

(i, B) : FILL(2)

(0, j): DROP(1)

(i, 0): DROP(2)

(i+j, 0) or (A, j-A+i) : POUR(2,1)

(0, i+j) or (i-B+j, B) : POUR(1,2)

 

判断这些点的访问状态,然后广搜下去,另外,要求出从源顶点到这些点的最短路径

 

当i=C 或 j=C时,就找到了

 

同时,为了直到到达(i, j)这一点时,所做的操作,我还设了一个operation变量,来记录达到一点所做的操作,供最后输出

 

在题中,我用的是一维数组,因此(i, j)变为(i*(B+1))+j

 

print_path函数,实质上输出了从源顶点到目标点的最短路径上的每个点的操作。

 

 

 

 

 附录:

 

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

 

抱歉!评论已关闭.