poj 3414 Pots

//和poj1606的题目的解法是一样的,不过有点地方需要注意,这题当没解的时候要输出impossible,还有无论是哪个杯的水的容量符合要求就可以输出了! 
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 110;
int a, b, c, r, l, vis[MAX][MAX], step[MAX*MAX];
string opr[7] = {" ", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(2,1)", "POUR(1,2)"};

struct Info
{
       int x;
       int y;
       int operation;
       int pre;//用链表的方式进行存储,有利于回溯找出路径 
}info[MAX*MAX];

void solve(int xx, int yy, int ss)
{
     if (vis[xx][yy])
         return ;
     vis[xx][yy] = 1;
     info[r].x = xx;
     info[r].y = yy;
     info[r].operation = ss;
     info[r].pre = l;
     r++;
}

void print()
{
     int i, ans;
     ans = 0;
     while (l != 0){
           step[ans++] = info[l].operation;
           l = info[l].pre;
     }
     
     cout << ans << endl;
     for (i = ans - 1; i >= 0; i--){
         cout << opr[step[i]] << endl;
     }
}


void BFS()
{
     info[0].x = 0;
     info[0].y = 0;
     vis[0][0] = 1;
     l = 0;
     r = 1;
     while (l != r){
           //这个地方与poj1606稍微有区别的 
           if (info[l].y == c || info[l].x == c){
               print();
               return ;
           }
           
           int tmpx, tmpy;
           //第一种方法fill(1) 
           tmpx = a;
           tmpy = info[l].y;
           solve(tmpx, tmpy, 1);
           
           //第二种方法fill(2) 
           tmpx = info[l].x;
           tmpy = b;
           solve(tmpx, tmpy, 2);
           
           //第三种方法drop(1); 
           tmpx = 0;
           tmpy = info[l].y;
           solve(tmpx, tmpy, 3);
            
           //第四种方法drop(2) 
           tmpx = info[l].x;
           tmpy = 0;
           solve(tmpx, tmpy, 4);
           
           //第五种方法fill(2, 1) 
           tmpx = info[l].x + min(a - info[l].x, info[l].y);
           tmpy = info[l].y - min(a - info[l].x, info[l].y);
           solve(tmpx, tmpy, 5);
           
           //第六种方法fill(1, 2) 
           tmpx = info[l].x - min(b - info[l].y, info[l].x);
           tmpy = info[l].y + min(b - info[l].y, info[l].x);
           solve(tmpx, tmpy, 6);
           
           l++;
     }
     if (l >= r)
         cout << "impossible" << endl;
}


int main()
{
    cin >> a >> b >> c;
    memset(vis, 0, sizeof(vis));
    BFS();
    
    system("pause");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值