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

全部评论

相关推荐

07-09 19:25
门头沟学院 Java
这是要把每一个投校招的都开盒吗?
26届之耻将大局逆转:裁人的时候一次性追回餐费
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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