POJ - 3414 Pots (bfs+路径标记)

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)

NO1.结构体里面带一个string来记录路径,感觉比较方便。vis数组标记这种情况有没有出现过。:D

#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int a,b,c;
int k[6]={1,2,3,4,5,6};
bool vis[150][150];
bool flag;
string ans;

struct node
{
    int x,y;
    string step;
};

void bfs(node n)
{
    queue<node>q;
    q.push(n);
    vis[n.x][n.y]=1;
    while(q.size())
    {
        node tmp=q.front();
        q.pop();
//        cout<<"QAQ"<<'\n'<<'\n';
        if(tmp.x==c||tmp.y==c)
        {
            ans=tmp.step;
            flag=1;
            return ;
        }
        for(int i=0;i<6;i++)
        {
            node nn;
            switch(k[i])
            {
                case 1:
                    nn.x=a;
                    nn.y=tmp.y;
                    nn.step=tmp.step+"1";
                    break;
                case 2:
                    nn.y=b;
                    nn.x=tmp.x;
                    nn.step=tmp.step+"2";
                    break;
                case 3:
                    nn.x=0;
                    nn.y=tmp.y;
                    nn.step=tmp.step+"3";
                    break;
                case 4:
                    nn.y=0;
                    nn.x=tmp.x;
                    nn.step=tmp.step+"4";
                    break;
                case 5:
                    nn.y=tmp.y+tmp.x;
                    nn.x=0;
                    if(nn.y>b)
                    {
                        nn.x=nn.y-b;
                        nn.y=b;
                    }
                    nn.step=tmp.step+"5";
                    break;
                case 6:
                    nn.x=tmp.x+tmp.y;
                    nn.y=0;
                    if(nn.x>a)
                    {
                        nn.y=nn.x-a;
                        nn.x=a;
                    }
                    nn.step=tmp.step+"6";
                    break;
            }
            if(!vis[nn.x][nn.y])
            {
//                cout<<nn.x<<' '<<nn.y<<'\n';
                q.push(nn);
                vis[nn.x][nn.y]=1;
                if(nn.x==c||nn.y==c)
                {
                    ans=nn.step;
                    flag=1;
                    return ;
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        memset(vis,0,sizeof(vis));
        flag=0;
        node n;
        n.x=0;
        n.y=0;
        n.step.clear();
        bfs(n);
        if(!flag)
            cout<<"impossible"<<'\n';
        else
        {
            int len=ans.size();
            cout<<len<<'\n';
            for(int i=0;i<len;i++)
            {
                switch(ans[i])
                {
                    case '1':cout<<"FILL(1)"<<'\n';break;
                    case '2':cout<<"FILL(2)"<<'\n';break;
                    case '3':cout<<"DROP(1)"<<'\n';break;
                    case '4':cout<<"DROP(2)"<<'\n';break;
                    case '5':cout<<"POUR(1,2)"<<'\n';break;
                    case '6':cout<<"POUR(2,1)"<<'\n';break;
                }
            }
        }
    }
    return 0;
}

NO2.pair<>存储,待会回来补。  补个锤子

这题根本没有要求输出操作过程中间量,只需要记录每次操作方式就OK了,用什么pair

所以NO2.待续 待会回来补hhhhhhhhhhhhhhhhh

全部评论

相关推荐

09-17 17:09
门头沟学院 Java
雨忄:有人给出过解法,拖晚点去,然后到时候再找其他理由商量,既增加他们的筛人成本,不一定会给你收回offer ,也能占位避免工贼
秋招的嫡长offer
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务