# Pots（BFS）

PotsCrawling in process...Crawling failedTime 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

Hint

FILL(2)
POUR(2,1)

那么对应可能会有两种状态：用 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】

也有两种情况,分析同上；

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int x;
int y;
int ans;
}q[5010],t,f;
int mv[110][110];

void bfs(int a,int b,int c)
{
int i;
int s = 0;
int e = 0;
t.x = 0;
t.y = 0;
t.ans = 0;
q[e++] = t;
mv[t.x][t.y] = 1;
while(s<e)
{
t = q[s++];
if(t.x == c || t.y == c)
{
printf("%d\n",t.ans);
return ;
}
for(i=0;i<6;i++)
{
if(i == 0)
{
f.x = a;
f.y = t.y;
}
if(i == 1)
{
f.x = t.x;
f.y = b;
}
if(i == 2)
{
f.x = 0;
f.y = t.y;
}
if(i == 3)
{
f.x = t.x;
f.y = 0;
}
if(i == 4)
{
f.x = t.x + t.y;
if(f.x>a)
{
f.y = f.x - a;
f.x = a;

}
else
{
f.y = 0;
}
}
if(i == 5)
{
f.y = t.x + t.y;
if(f.y>b)
{
f.x = f.y - b;
f.y = b;
}
else
{
f.x = 0;
}
}
if(mv[f.x][f.y] == 0)
{
f.ans = t.ans + 1;
q[e++] = f;
mv[f.x][f.y] = 1;
}
}
}
printf("impossible\n");
return ;

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


﻿﻿

©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客