SDUTOJ 2780 ——Pots BFS

Pots

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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.

输入

 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).

输出

 The first line of the output must contain the length of the sequence of operations K.  If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

示例输入

3 5 4

示例输出

6

提示

FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

来源

bfs,和图不同的是它有六个状态,分别是
1 把第一个杯子的水盛满,第二个杯子为0
2 把第二个杯子的水盛满,第一个杯子为0
3 把第一个杯子的水盛满,第二个杯子不变(承接上一个第二个杯子的状态)
4 把第二个杯子的水盛满,第一个杯子不变(承接上一个第二个杯子的状态)
5 把第一个杯子里的水倒入第二个杯子(注意判是否溢出)
把第二个杯子里的水倒入第一个杯子(注意判是否溢出)

这六个状态搜索,用标记数组不让重复搜索...



#include <stdio.h>
#include <string.h>
#include <queue>

using namespace std;

int a,b,c;
struct node
{
    int x;
    int y;
    int ans;
};

queue <node> q;
bool vis[210][210];

void bfs()
{
    while(!q.empty())
        q.pop();
    node t,f;
    t.x = 0;
    t.y = 0;
    t.ans = 0;
    q.push(t);
    while(!q.empty())
    {
        t = q.front();
        q.pop();
        if(t.x == c || t.y == c)
        {
            printf("%d\n",t.ans);
            return ;
        }
        f.x = 0;
        f.y = t.y;
        if(!vis[f.x][f.y])
        {
            f.ans = t.ans + 1;
            q.push(f);
            vis[f.x][f.y] = true;
        }
        f.x = t.x;
        f.y = 0;
        if(!vis[f.x][f.y])
        {
            f.ans = t.ans + 1;
            q.push(f);
            vis[f.x][f.y] = true;
        }
        f.x = t.x;
        f.y = b;
        if(!vis[f.x][f.y])
        {
            f.ans = t.ans + 1;
            q.push(f);
            vis[f.x][f.y] = true;
        }
        f.x = a;
        f.y = t.y;
        if(!vis[f.x][f.y])
        {
            f.ans = t.ans + 1;
            q.push(f);
            vis[f.x][f.y] = true;
        }
        if(t.x + t.y > a)
        {
            f.x = a;
            f.y = t.x + t.y - a;
        }
        else
        {
            f.x = t.x + t.y;
            f.y = 0;
        }
        if(!vis[f.x][f.y])
        {
            f.ans = t.ans + 1;
            q.push(f);
            vis[f.x][f.y] = true;
        }
        if(t.x + t.y > b)
        {
            f.x = t.x + t.y - b;
            f.y = b;
        }
        else
        {
            f.x = 0;
            f.y = t.x + t.y;
        }
        if(!vis[f.x][f.y])
        {
            f.ans = t.ans + 1;
            q.push(f);
            vis[f.x][f.y] = true;
        }
    }
    printf("impossible\n");
}

int main()
{
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        memset(vis,false,sizeof(vis));
        bfs();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值