POJ 3414 - Pots - [BFS]

题目链接:http://poj.org/problem?id=3414

Time Limit: 1000MS  Memory Limit: 65536K

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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)

 

题意:

给出两个容积分别为 A,B 的壶,现有三种操作:

  1. 从水龙头把壶 i 倒满;
  2. 把壶 i 倒空;
  3. 把壶 i 里的水倒到壶 j 中,直到壶 i 空或者壶 j 满。

给出目标 C,要求最终至少某一个壶中的水量正好为 C,要求输出最少步数。

 

题解:

暴力搜索。

 

AC代码:

复制代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<queue>
using namespace std;
typedef pair<int,int> pii;

int A,B,C;
string op[7]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
inline pii change(pii x,int type)
{
    int a=x.first, b=x.second;
    switch(type)
    {
    case 1: //倒满a
        a=A;
        break;
    case 2: //倒满b
        b=B;
        break;
    case 3: //倒空a
        a=0;
        break;
    case 4: //倒空b
        b=0;
        break;
    case 5: //a倒到b
        if(a>=B-b) a-=B-b, b=B;
        else b+=a, a=0;
        break;
    case 6: //b倒到a
        if(b>=A-a) b-=A-a, a=A;
        else a+=b, b=0;
        break;
    }
    return make_pair(a,b);
}

int vis[105][105];
pii pre[105][105];
queue<pii> Q;
inline pii bfs()
{
    memset(vis,0,sizeof(vis));
    memset(pre,0,sizeof(pre));
    Q.push(make_pair(0,0));
    vis[0][0]=1;
    while(!Q.empty())
    {
        pii now=Q.front(); Q.pop();
        if(now.first==C || now.second==C) return now;
        for(int type=1;type<=6;type++)
        {
            pii nxt=change(now,type);
            if(!vis[nxt.first][nxt.second])
            {
                vis[nxt.first][nxt.second]=type;
                pre[nxt.first][nxt.second]=now;
                Q.push(nxt);
            }
        }
    }
    return make_pair(-1,-1);
}

int main()
{
    cin>>A>>B>>C;
    pii x=bfs();
    if(x.first==-1 && x.second==-1) printf("impossible\n");
    else
    {
        vector<int> ans;
        while(x.first+x.second>0)
        {
            ans.push_back(vis[x.first][x.second]);
            x=pre[x.first][x.second];
        }
        printf("%d\n",ans.size());
        for(int i=ans.size()-1;i>=0;i--) cout<<op[ans[i]]<<endl;
    }
}
复制代码

 

posted @   Dilthey  阅读(357)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示