Pots——BFS

 Pots
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

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

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.  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
 
   
题意:有1,2两个杯子,容量分别为A,B,给你一定量C的水,通过将1杯子倒满,将2杯子倒满,将1杯子倒空,将2杯子倒空,1杯子里的水倒进2杯子里,2杯子里的水倒进1杯子里六个操作,让1,2杯子的任意一个杯子里的水为C,求最少用多少步。
首先要注意,1杯子里的水往2杯子里倒入时,如果2杯子满了,那么剩下的水要留在1杯子里,另外一种情况也一样。
其次,要用数组进行标记,避免重复的做某一个状态。比如说,之前1杯子里有2升水,2杯子里有1升水,这种状态已经加入队列,后来经过多种变化,又出现这种状态,就不能再加入队列了
 
   
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

struct node
{
    int xiana;
    int xianb;
    int step;
} bian[30000];

int bj[1001][1001];

int a,b,c;

void bfs()
{
    int l,z,fo;
    struct node head,next;
    head.xiana = 0;
    head.xianb = 0;
    head.step = 0;
    bj[head.xiana][head.xianb] = 1;
    l = z = 0;
    bian[z++] = head;
    while(l < z)
    {
        head = bian[l++];
        bj[head.xiana][head.xianb] = 1;
        if(head.xiana == c || head.xianb == c)
        {
            printf("%d\n",head.step);
            return ;
        }
        for(fo = 0; fo < 6; fo++)
        {
            if(fo == 0)
            {
                next.xiana = a;
                next.xianb = head.xianb;
            }
            else if(fo == 1)
            {
                next.xianb = b;
                next.xiana = head.xiana;
            }
            else if(fo == 2)
            {
                next.xiana = 0;
                next.xianb = head.xianb;
            }
            else if(fo == 3)
            {
                next.xianb = 0;
                next.xiana = head.xiana;
            }
            else if(fo == 4)
            {
                if(head.xiana + head.xianb > a)
                {
                    next.xiana = a;
                    next.xianb = head.xiana + head.xianb - a;
                }
                else
                {
                    next.xianb = 0;
                    next.xiana = head.xiana + head.xianb;
                }
            }
            else if(fo == 5)
            {
                if(head.xiana + head.xianb > b)
                {
                    next.xiana = head.xiana + head.xianb - b;
                    next.xianb = b;
                }
                else
                {
                    next.xiana = 0;
                    next.xianb = head.xiana + head.xianb;
                }
            }
            if(!bj[next.xiana][next.xianb])
            {
                next.step = head.step + 1;
                bian[z++] = next;
            }
        }
    }
    puts("impossible");
}


int main()
{

    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        memset(bj,0,sizeof(bj));
        bfs();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值