poj 3414 (POTS) (BFS)

简介:
Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12295   Accepted: 5190   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 ≤ ≤ 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)

Source

Northeastern Europe 2002, Western Subregion

题目大意:
就是给你两个瓶子a b,看是否能够通过以下的步骤达到k
1)FILL(i):将 i 瓶子倒满水;
2)POUR(i,j):将i瓶子中的水倒入j
3)DROP(I):将i瓶子中的水倒出来:

/**解题思路:
就是一个简单的BFS,仔细搞搞就行了

**/


/**
2015 - 09 - 16 下午
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 105;
const double eps = 1e-7;
bool vis[maxn][maxn];
const int dir[4][2]= {1, 0, 0, 1, -1, 0, 0, -1};
char map[maxn][maxn];

int a, b, k;
struct node
{
    int vola, volb, step;
    char str[maxn][maxn];
};

bool bfs()
{
    memset(vis, false, sizeof(vis));
    queue<node> que;
    node p, q;
    p.vola = 0, p.volb = 0, p.step = 0;
    que.push(p);
    vis[0][0] = 1;
    ///vis[p.vola][p.volb] = true;
    while(!que.empty())
    {
        p = que.front();
        que.pop();
        if(p.vola==k || p.volb == k)
        {
            cout<<p.step<<endl;
            for(int i=1; i<=p.step; i++)
                ///cout<<p.str[i]<<endl;
                printf("%s\n",p.str[i]);
            return true;
        }
        ///倒满 a
        if(p.vola == 0)
        {
            q = p;
            q.vola = a;
            q.step++;
            strcpy(q.str[q.step], "FILL(1)");
            if(!vis[q.vola][q.volb])
            {
                vis[q.vola][q.volb] = true;
                que.push(q);
            }
        }
        ///把 a 中的水倒出来
        else if(p.vola <= a)
        {
            q = p;
            q.vola = 0;
            q.step++;
            strcpy(q.str[q.step], "DROP(1)");
            if(!vis[q.vola][q.volb])
            {
                vis[q.vola][q.volb] = true;
                que.push(q);
            }
            ///a -> b
            if(p.volb < b)
            {
                q = p;
                if(q.volb + q.vola <= b)
                {
                    q.volb += q.vola;
                    q.vola = 0;
                }
                else
                {
                    q.vola = (q.vola+q.volb)-b;
                    q.volb = b;
                }
                q.step++;
                strcpy(q.str[q.step], "POUR(1,2)");
                if(!vis[q.vola][q.volb])
                {
                    vis[q.vola][q.volb] = true;
                    que.push(q);
                }
            }
        }
        ///把 b 倒满
        if(p.volb == 0)
        {
            q = p;
            q.volb = b;
            q.step++;
            strcpy(q.str[q.step], "FILL(2)");
            if(!vis[q.vola][q.volb])
            {
                vis[q.vola][q.volb] = true;
                que.push(q);
            }
        }
        ///把 b 中的水倒出来
        else if(p.volb <= b)
        {
            q = p;
            q.volb = 0;
            q.step++;
            strcpy(q.str[q.step],"DROP(2)");
            if(!vis[q.vola][q.volb])
            {
                vis[q.vola][q.volb] = true;
                que.push(q);
            }
            if(p.vola < a)
            {
                q = p;
                if(q.vola + q.volb <= a)
                {
                    q.vola += q.volb;
                    q.volb = 0;
                }
                else
                {
                    q.volb = (q.vola+q.volb)-a;
                    q.vola = a;
                }
                q.step++;
                strcpy(q.str[q.step], "POUR(2,1)");
                if(!vis[q.vola][q.volb])
                {
                    vis[q.vola][q.volb] = true;
                    que.push(q);
                }
            }
        }
    }
    return false;
}
int main()
{
    while(cin>>a>>b>>k)
    {
        bool ok = bfs();
        if(!ok)
            puts("impossible");
    }
    return 0;
}
/**
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
**/


目录
相关文章
|
6月前
Poj 3255(dijkstra求次短路)
Poj 3255(dijkstra求次短路)
40 0
|
机器学习/深度学习
洛谷P3395-路障(BFS)
洛谷P3395-路障(BFS)
洛谷P3395-路障(BFS)
POJ-2253,Frogger(最短路问题)
POJ-2253,Frogger(最短路问题)
|
人工智能 并行计算 网络架构
【HDU 4771 Stealing Harry Potter&#39;s Precious】BFS+状压
2013杭州区域赛现场赛二水。。。 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间。 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点,搜索下一个最近的宝藏,直至找到全部k个宝藏。
1040 0
poj 3984 bfs
http://poj.org/problem?id=3984 #include&lt;iostream&gt; #include&lt;stdio.h&gt; #include&lt;cstring&gt; #define Max 0x7f7f7f7f using namespace std; int map[6][6]; int visited[6][6]; int di
1257 0
hdu1175 bfs
// hdu 1175 #include &lt;iostream&gt; #include &lt;cstring&gt; #include &lt;cstdio&gt; #include &lt;queue&gt; using namespace std; struct node { int x , y ; int step ; }s,e; nod
921 0