H - Pots POJ - 3414 (bfs操作转换-路径记录)

H - Pots

 POJ - 3414 

#include<iostream>
#include<map>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=102;
bool vis[maxn][maxn];
struct node
{
    int x, y;
} temp1,temp2;
int  a,b,c,cnt,aa[maxn*maxn];
map<int,pair<int,pair<int,int > > >mmp[maxn];
int bfs()
{
    queue<node>q;
    temp1.x=temp1.y=0;
    q.push(temp1);
    vis[temp1.x][temp1.y]=1;
    while(!q.empty())
    {
        temp1.x=q.front().x;
        temp1.y=q.front().y;
        if(temp1.x==c)
        {
            cnt =temp1.y;
            return 1;
        }
        if(temp1.y==c)
        {
            cnt =temp1.x;
            return 2;
        }
        q.pop();
        if(temp1.x<a&&!vis[a][temp1.y])
        {
            temp2.x=a;
            temp2.y=temp1.y;
            vis[temp2.x][temp2.y]=1;
            q.push(temp2);
            mmp[temp2.x][temp2.y]=make_pair(1,make_pair(temp1.x,temp1.y));
        }
        if(temp1.y<b&&!vis[temp1.x][b])
        {
            temp2.x=temp1.x;
            temp2.y=b;
            q.push(temp2);
            vis[temp2.x][temp2.y]=1;
            mmp[temp2.x][temp2.y]=make_pair(2,make_pair(temp1.x,temp1.y));
        }
        if(temp1.x>0&&!vis[0][temp1.y])
        {
            temp2.x=0;
            temp2.y=temp1.y;
            q.push(temp2);
            vis[temp2.x][temp2.y]=1;
            mmp[temp2.x][temp2.y]=make_pair(3,make_pair(temp1.x,temp1.y));
        }
        if(temp1.y>0&&!vis[temp1.x][0])
        {
            temp2.x=temp1.x;
            temp2.y=0;
            q.push(temp2);
            vis[temp2.x][temp2.y]=1;
            mmp[temp2.x][temp2.y]=make_pair(4,make_pair(temp1.x,temp1.y));
        }

        if(temp1.x<a&&!vis[temp1.x+min(a-temp1.x,temp1.y)][temp1.y-min(a-temp1.x,temp1.y)])
        {
            temp2.x=temp1.x+min(a-temp1.x,temp1.y);
            temp2.y=temp1.y-min(a-temp1.x,temp1.y);
            q.push(temp2);
            vis[temp2.x][temp2.y]=1;
            mmp[temp2.x][temp2.y]=make_pair(5,make_pair(temp1.x,temp1.y));
        }
        if(temp1.y<b&&!vis[temp1.x-min(b-temp1.y,temp1.x)][temp1.y+min(b-temp1.y,temp1.x)])
        {
            temp2.x=temp1.x-min(b-temp1.y,temp1.x);
            temp2.y=temp1.y+min(b-temp1.y,temp1.x);
            q.push(temp2);
            vis[temp2.x][temp2.y]=1;
            mmp[temp2.x][temp2.y]=make_pair(6,make_pair(temp1.x,temp1.y));
        }
    }
    return -1;
}
int main()
{
    cin>>a>>b>>c;
    memset(vis,0,sizeof(vis));
    int ans=bfs(),sum=0;
    if(ans==-1)
        cout<<"impossible"<<endl;
    else
    {
        int tx,ty;
        if(ans==1)
        {
            tx=c;
            ty=cnt;
        }
        if(ans==2)
        {
            ty=c;
            tx=cnt;
        }
        while(1)
        {
            if(tx==0&&ty==0)
                break;
            aa[sum++]=mmp[tx][ty].first;
            int tempx=mmp[tx][ty].second.first;
            int tempy=mmp[tx][ty].second.second;
            tx=tempx;
            ty=tempy;
        }
        cout<<sum<<endl;
        for(int i=sum-1; i>=0; i--)
        {
            if(aa[i]==2)
                cout<<"FILL(2)"<<endl;
            if(aa[i]==1)
                cout<<"FILL(1)"<<endl;
            if(aa[i]==3)
                cout<<"DROP(1)"<<endl;
            if(aa[i]==4)
                cout<<"DROP(2)"<<endl;
            if(aa[i]==5)
                cout<<"POUR(2,1)"<<endl;
            if(aa[i]==6)
                cout<<"POUR(1,2)"<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值