Pots——BFS

来源:互联网 发布:手机淘宝下载安装 编辑:程序博客网 时间:2024/06/12 01:05
 Pots
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit Status

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;}


0 0
原创粉丝点击