POJ-3414,AC码,简单易懂

G++AC
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int A,B,C,vis[150][150],flag=0;//flag用于判断是否有结果
char *w=NULL;
struct point
 {
     int a,b;//a中的水和b中的水
     char c[10000];//存入路径
 }; 
 void pour(int *a,int *b,int AN,int BN) //把*b中的水倒入*a中
 {
    int AS=AN-(*a);
    if((*b)>AS)
    {
        int c=(*b)-AS;
        (*a)=AN;
        (*b)=c;
    }
    else
    {
        (*a)+=(*b);
        (*b)=0;
    }
    return ;
 }
void bfs(int x,int y)
{
    point first;
    first.a=0,first.b=0;
    queue<point>q;
    q.push(first);
    vis[first.a][first.b]=0;
    while(!q.empty())
    {
        point deal;
        deal=q.front(),q.pop();
        // cout<<deal.a<<" "<<deal.b<<endl;
        w=deal.c;
        if(deal.a==C || deal.b==C)
           {flag=1; return ;}
        for (int i = 0; i < 6; ++i)
        {
            point now;
            now=deal;
            // cout<<"now : "<<now.a<<" "<<now.b<<endl;
            
            if(i==0)
            {  
                now.a=A;
                if(vis[now.a][now.b])
                {
                    vis[now.a][now.b]=0;
                    strcat(now.c,"FILL(1)");
                    q.push(now);
                }
            }
            now=deal;
            if(i==1)
            {             
                now.b=B;
                if(vis[now.a][now.b])
                {
                    vis[now.a][now.b]=0;
                    strcat(now.c,"FILL(2)");
                    q.push(now);
                }
            }
            now=deal;
            if(i==2)
            {
                pour(&now.a,&now.b,A,B);
                if(vis[now.a][now.b])
                {
                    vis[now.a][now.b]=0;
                    strcat(now.c,"POUR(2,1)");
                    q.push(now);
                }
            }
            now=deal;
            if(i==3)
            {
                pour(&now.b,&now.a,B,A);
                if(vis[now.a][now.b])
                {
                    vis[now.a][now.b]=0;
                    strcat(now.c,"POUR(1,2)");
                    q.push(now);
                }
            }
            now=deal;
            if(i==4)
            {
                now.a=0;
                if(vis[now.a][now.b])
                {
                    vis[now.a][now.b]=0;
                    strcat(now.c,"DROP(1)");
                    q.push(now);
                }
            }
            now=deal;
            if(i==5)
            {
                now.b=0;
                if(vis[now.a][now.b])
                {
                    vis[now.a][now.b]=0;
                    strcat(now.c,"DROP(2)");
                    q.push(now);
                }
            }
        }
    }
}
int main(void) 
{
         
    while (cin >> A>>B>>C)
    {
        flag=0;
        memset(vis,1,sizeof(vis));
        bfs(A,B);
        if(flag==1)
        {
                int m=strlen(w),l=1;
                for (int i = 0; i < m-1; ++i)
                    if(w[i]==')')l++;
                cout<<l<<endl;
                for (int i = 0; i < m; ++i)
                {
                    cout<<w[i];
                    if(w[i]==')')cout<<endl;
                }
        }
        else cout<<"impossible"<<endl;
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值