sdutacm-Pots

来源:互联网 发布:淘宝魔镜插件下载 编辑:程序博客网 时间:2024/06/06 01:00

 

 

Pots

Time Limit: 1000MSMemory Limit: 65536KB

SubmitStatistic

ProblemDescription

You are given two pots,having the volume of A and B liters respectively. The following operations canbe 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)    pourfrom pot i to pot j; after this operation either the pot j is full (and theremay be some water left in the pot i), or the pot i is empty (and all itscontents have been moved to the pot j).

Write a program to find theshortest possible sequence of these operations that will yield exactly C litersof water in one of the pots.

Input

 On the first and onlyline are the numbers A, B, and C. These are all integers in the range from 1 to100 and C≤max(A,B).

Output

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

ExampleInput

3 5 4

ExampleOutput

6

Hint

FILL(2)

POUR(2,1)

DROP(1)

POUR(2,1)

FILL(2)

POUR(2,1)

Author

#include <iostream>#include<bits/stdc++.h>using namespace std;struct node{int x,y,ans;}t,f;int a,b,c;int tu[110][110];void bfs(){    //int i;    queue<node>g;    t.x = 0;    t.y = 0;    t.ans = 0;    g.push(t);    tu[t.x][t.y] = 1;    while(!g.empty())    {        f = g.front();        g.pop();        if(f.x==c||f.y==c)        {        printf("%d\n",f.ans);        return ;        }        for(int i=0;i<6;i++)        {            if(i==0)            {            t.x = a;            t.y = f.y;            }            else if(i==1)            {                t.x = f.x;                t.y = b;            }            else if(i==2)            {                t.x = f.x;                t.y = 0;            }            else if(i==3)            {                t.x = 0;                t.y = f.y;            }            else if(i==4)            {                t.x = f.x+f.y;                if(t.x>a)                {                 t.y = t.x -a;                 t.x = a;                }                else                t.y = 0;            }            else if(i==5)            {                t.y = f.x+f.y;                if(t.y>b)                {                    t.x = t.y-b;                    t.y = b;                }                else                   t.x = 0;            }            if(tu[t.x][t.y]==0)            {                t.ans =f.ans+1;                g.push(t);                tu[t.x][t.y] = 1;            }        }    }    printf("impossible\n");    return ;}int main(){    while(~scanf("%d%d%d",&a,&b,&c))    {        memset(tu,0,sizeof(tu));        bfs();    }    return 0;}/*题意有两个容量A,B一定的罐子,分别有装满,倒空,从一个瓶子倒入另一个瓶子这三种操作,求至少经过多少次操作后会使得其中一个瓶子中的水的体积为C思路因为求最少,可以用bfs模拟倒水应该很容易就能分析出,对应于每一种状态的下一步只有六种情况:一:用水池中的水装满 A二:用水池中的水装满 B三:把 A 中的水全部倒掉四:把 B 中的水全部倒掉五:把 A 中的水倒进 B, 但是主意不能溢出。    那么对应可能会有两种状态:用 f.x 表示 A 中的水, f.b 表示 B 中的水    如果 f.x+f.y >= B 则   f.x = f.x+f.y-B; f.y= B 【能够装满容器 B】    否则 k1 = k1+k2; k2 = 0 【不能能够装满容器 B】六:把 B 中的水倒进 A 【不能溢出】    也有两种情况,分析同上;*//***************************************************User name: jk160505徐红博Result: AcceptedTake time: 0msTake Memory: 216KBSubmit time: 2017-02-15 21:24:33****************************************************/

 

0 0