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

全部评论

相关推荐

老板加个卤鸡蛋:HR看了以为来卧底来了
点赞 评论 收藏
分享
零零幺零零幺:至少再做一个项目,然后猛投小厂,不然有点难
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
5015次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1103次浏览 27人参与
# 米连集团26产品管培生项目 #
4131次浏览 198人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6907次浏览 37人参与
# 简历第一个项目做什么 #
31251次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186349次浏览 1115人参与
# 巨人网络春招 #
11175次浏览 223人参与
# 面试紧张时你会有什么表现? #
30371次浏览 188人参与
# 简历中的项目经历要怎么写? #
309379次浏览 4152人参与
# 网易游戏笔试 #
6304次浏览 83人参与
# 职能管理面试记录 #
10687次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6850次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56698次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160394次浏览 1105人参与
# 小红书求职进展汇总 #
226845次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62406次浏览 728人参与
# 你怎么看待AI面试 #
179273次浏览 1164人参与
# 正在春招的你,也参与了去年秋招吗? #
362529次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92123次浏览 896人参与
# 机械求职避坑tips #
94396次浏览 567人参与
# 校招笔试 #
466318次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27111次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务