poj_3414 Pots(bfs)

来源:互联网 发布:淘宝上买卫生巾靠谱吗 编辑:程序博客网 时间:2024/05/22 12:25
Pots
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14916 Accepted: 6269 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

6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
bfs模拟,用一个结构体保存当前状态的两个水桶的体积,状态序列,与序列长度。
最后输出序列长度和状态序列。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;const int FILL1 = 0, FILL2 = 1, POUR12 = 2, POUR21 = 3, DROP1 = 4, DROP2 = 5;const char s[][10] = {"FILL(1)", "FILL(2)", "POUR(1,2)", "POUR(2,1)", "DROP(1)", "DROP(2)"};struct State{    int va, vb;    int cot;    vector<int> step;};bool vis[110][110];int maxa, maxb, goal;State ans;State solve(State t, int type){    if(type == FILL1) t.va = maxa;    else if(type == FILL2) t.vb = maxb;    else if(type == DROP1) t.va = 0;    else if(type == DROP2) t.vb = 0;    else if(type == POUR12)    {        int v = maxb - t.vb;        if(v > 0)        {            if(t.va >= v) t.va -= v, t.vb = maxb;            else t.vb += t.va, t.va = 0;        }        else return t;    }    else if(type == POUR21)    {        int v = maxa - t.va;        if(v > 0)        {            if(t.vb >= v) t.vb -= v, t.va = maxa;            else t.va += t.vb, t.vb = 0;        }        else return t;    }    t.step.push_back(type);    t.cot++;    return t;}bool bfs(){    memset(vis, 0, sizeof(vis));    queue<State> Q;    State st;    st.va = 0, st.vb = 0, st.cot = 0;    Q.push(st);    while(!Q.empty())    {        State t = Q.front();        Q.pop();        if(t.va == goal || t.vb == goal) {ans = t; return true;}        for(int i = 0; i < 6; i++)        {            State temp = solve(t, i);            if(temp.cot == t.cot || (temp.va == t.va && temp.vb == t.vb)) continue;            if(!vis[temp.va][temp.vb])                Q.push(temp), vis[temp.va][temp.vb] = 1;        }    }    return false;}int main(){    while(~scanf("%d%d%d", &maxa, &maxb, &goal))    {        if(bfs())        {            printf("%d\n", ans.cot);            for(int i = 0; i < ans.step.size(); i++)            {                printf("%s\n", s[ans.step[i]]);            }        }        else        {            printf("impossible\n");        }    }    return 0;}


0 0
原创粉丝点击