ZOJ Problem Set - 1005 Jugs

本文提供了一个使用广度优先搜索(BFS)算法解决水壶问题的C++实现案例。通过定义节点结构体并利用队列进行状态转移,该程序演示了如何从初始状态出发寻找达到目标状态的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单BFS

#include <iostream>
#include <queue>
using namespace std;

struct Node
{
    int a,b;
    string str;
};
queue <Node> qt;

bool cot[1001][1001];

int main()
{
    int aV,bV,aimV,i,j;
    Node tmp1,tmp,sta;
    while(cin>>aV>>bV>>aimV)
    {
        for(i = 0;i <= bV;i ++)
        for(j = 0;j <= bV;j ++)
        cot[i][j] = 0;
        cot[0][0] = 1;
        sta.a = sta.b = 0;
        sta.str = "";
        qt.push(sta);
        while(!qt.empty())
        {
            tmp = qt.front();qt.pop();tmp1 = tmp;
            //cout<<tmp.a<<" "<<tmp.b<<" "<<tmp.str<<endl;
            tmp1.a = aV;

            if(tmp1.b == aimV)
            {
                for(i = 0;i < tmp1.str.size();i ++)
                {
                    switch ( tmp1.str[i]){
                    case '1': cout<<"fill A"<<endl;break;
                    case '2': cout<<"fill B"<<endl;break;
                    case '3': cout<<"empty A"<<endl;break;
                    case '4': cout<<"empty B"<<endl;break;
                    case '5': cout<<"pour A B"<<endl;break;
                    case '6': cout<<"pour B A"<<endl;break;
                    }
                }
                cout<<"success"<<endl;
                break;
            }
            if(cot[tmp1.a][tmp1.b] == 0)
            {
                tmp1.str += '1';
                qt.push(tmp1);
                cot[tmp1.a][tmp1.b] = 1;
            }
            tmp1 = tmp;
            tmp1.b = bV;
            if(cot[tmp1.a][tmp1.b] == 0)
            {
                tmp1.str += '2';
                qt.push(tmp1);
                cot[tmp1.a][tmp1.b] = 1;
            }
            tmp1 = tmp;
            tmp1.a = 0;
            if(cot[tmp1.a][tmp1.b] == 0)
            {
                tmp1.str += '3';
                qt.push(tmp1);
                cot[tmp1.a][tmp1.b] = 1;
            }
            tmp1 = tmp;
            tmp1.b = 0;
            if(cot[tmp1.a][tmp1.b] == 0)
            {
                tmp1.str += '4';
                qt.push(tmp1);
                cot[tmp1.a][tmp1.b] = 1;
            }
            tmp1 = tmp;
            if(tmp1.a + tmp1.b <= bV) {
                tmp1.b += tmp1.a;tmp1.a = 0;
                if(cot[tmp1.a][tmp1.b] == 0)
            {
                tmp1.str += '5';
                qt.push(tmp1);
                cot[tmp1.a][tmp1.b] = 1;
            }

                }
            else
            {
                tmp1.a -= (bV - tmp1.b);
                tmp1.b = bV;
                if(cot[tmp1.a][tmp1.b] == 0)
            {
                tmp1.str += '5';
                qt.push(tmp1);
                cot[tmp1.a][tmp1.b] = 1;
            }
            }
            tmp1 = tmp;
            if(tmp1.a + tmp1.b <= aV) {
                tmp1.a += tmp1.b;tmp1.b = 0;
                if(cot[tmp1.a][tmp1.b] == 0)
            {
                tmp1.str += '6';
                qt.push(tmp1);
                cot[tmp1.a][tmp1.b] = 1;
            }

                }
            else
            {
                tmp1.b -= (aV - tmp1.a);tmp1.a = aV;
                if(cot[tmp1.a][tmp1.b] == 0)
            {
                tmp1.str += '6';
                qt.push(tmp1);
                cot[tmp1.a][tmp1.b] = 1;
            }
            }
        }
        while(!qt.empty()) qt.pop();

    }
    return 0;
}

传说中一种特判: 不能理解。

#include <stdio.h> 

int main(){ 
  int ca,cb,n; 
  int a,b,bnow; 
  int i; 
  while (scanf("%d%d%d",&ca,&cb,&n) != EOF) 
  { 
     a = b = 0; 
     while (b != n) 
     { 
        for ( i=0; i<=(cb-b)/ca; i++) 
        { 
           printf ("fill A\n"); 
           printf ("pour A B\n"); 
           bnow = b; 
           bnow = b+ca; 
           if (bnow == n)break; 
        } 
        if (bnow == n) break; 
        printf ("empty B\n"); 
        printf ("pour A B\n"); 
        a = ca-(cb-b)%ca; 
        b=a; 
        if(b==n)break; 
     } 
     printf ("success\n"); 
  } 
  return 0; 
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值