3414 Pots 经典倒水问题,要求最短,宽搜,ax + by = c 有
解条件
c = 0 (mod gcd(a,b))
#include
<
iostream
>
#define
MAX_LEN 10001
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
using
namespace
std;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
a,b,c;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
bool
black[
101
][
101
]
=
...
{false}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
struct
Node
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int a,b;
int level;
int prev;
char s;
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Node que[MAX_LEN];
int
top
=
0
,end
=
0
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
inline
void
pour(
int
&
c,
int
&
d,
int
vol)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if (c+d > vol)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
c -= vol-d;
d = vol;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
d += c;
c =0;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
bool
expand()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
//fill
Node tmp = que[top];
if (tmp.a !=a && black[a][tmp.b] ==false)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tmp.a = a;
++tmp.level;
tmp.s = 'f';
tmp.prev = top;
black[tmp.a][tmp.b] = true;
que[end++] = tmp;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
tmp = que[top];
if (tmp.b != b && black[tmp.a][b] ==false)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tmp.b = b;
++tmp.level;
tmp.s = 'f';
tmp.prev = top;
black[tmp.a][tmp.b] = true;
que[end++] = tmp;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//drop
tmp = que[top];
if (tmp.a != 0 && black[0][tmp.b] ==false)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tmp.a = 0;
++tmp.level;
tmp.s = 'd';
tmp.prev = top;
black[0][tmp.b] = true;
que[end++] = tmp;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
tmp = que[top];
if (tmp.b != 0 && black[tmp.a][0]==false)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tmp.b = 0;
++tmp.level;
tmp.s = 'd';
tmp.prev = top;
black[tmp.a][0] = true;
que[end++] = tmp;
}
// pour 1 -> 2
tmp = que[top];
if (tmp.a != 0 && tmp.b!=b)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
pour(tmp.a, tmp.b,b);
if (!black[tmp.a][tmp.b])
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
++tmp.level;
tmp.s = 'p';
tmp.prev = top;
black[tmp.a][tmp.b] = true;
que[end++] = tmp;
if (tmp.a == c || tmp.b == c)
return false;
}
}
// pour 2 -> 1
tmp = que[top];
if (tmp.b != 0 && tmp.a!=a)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
pour(tmp.b, tmp.a,a);
if (!black[tmp.a][tmp.b])
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
++tmp.level;
tmp.prev = top;
tmp.s = 'p';
black[tmp.a][tmp.b] = true;
que[end++] = tmp;
if (tmp.a == c || tmp.b == c)
return false;
}
}
++top;
return true;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
print(
int
idx)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if (idx == 0) return;
int ti = que[idx].prev;
print(ti);
if (que[idx].s == 'f')
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (que[ti].a !=a && que[idx].a ==a)
cout<<"FILL(1)"<<endl;
else
cout<<"FILL(2)"<<endl;
}
else if (que[idx].s == 'd')
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (que[ti].a !=0 && que[idx].a ==0)
cout<<"DROP(1)"<<endl;
else
cout<<"DROP(2)"<<endl;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (que[ti].a > que[idx].a)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cout<<"POUR(1,2)"<<endl;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cout<<"POUR(2,1)"<<endl;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
bool
search()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
top = 0;
que[0].a = que[0].b = 0;
que[0].level = 0;
black[0][0]= true;
end = 1;
while (top!= end && expand());
return top!=end;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
main()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
cin>>a>>b>>c;
if (a == c)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cout<<"1 FILL(1)"<<endl;
return 0;
}
if (b == c)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cout<<"1 FILL(2)"<<endl;
return 0;
}
if (search())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cout<<que[end-1].level<<endl;
print(end-1);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cout<<"impossible"<<endl;
}
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)