给你两个容量不同的瓶子,要你通过3种操作,量出一定体积的水。就和以前做的益智题一样的。
BFS做的,注意要记录路径。。
又是深夜做题,用vector记录路径的。不知道怎么样复制一个vector到另一个vector,所以很暴力的for了一遍。。。
最后居然1Y了。。。数据太弱了。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
struct T
{
int a;
int b;
vector<int> op;
T()
{
a=0;
b=0;
op.clear();
}
~T()
{
}
};
queue<T> q;
bool v[200][200];
int main()
{
int a,b,c,i;
T s,ts,tts;
scanf("%d%d%d",&a,&b,&c);
s.a=0;
s.b=0;
if (s.a == c || s.b == c)
{
printf("0\n");
return 0;
}
while (!q.empty())
q.pop();
q.push(s);
memset(v,false,sizeof(v));
while (!q.empty())
{
ts=q.front();
q.pop();
if (ts.a == c || ts.b == c)
{
break;
}
tts.a=a;
tts.b=ts.b;
tts.op.clear();
for (i=0; i<ts.op.size(); i++)
tts.op.push_back(ts.op[i]);
tts.op.push_back(1);
if (v[tts.a][tts.b] == false)
{
v[tts.a][tts.b]=true;
q.push(tts);
}
tts.a=ts.a;
tts.b=b;
tts.op.clear();
for (i=0; i<ts.op.size(); i++)
tts.op.push_back(ts.op[i]);
tts.op.push_back(2);
if (v[tts.a][tts.b] == false)
{
v[tts.a][tts.b]=true;
q.push(tts);
}
tts.a=0;
tts.b=ts.b;
tts.op.clear();
for (i=0; i<ts.op.size(); i++)
tts.op.push_back(ts.op[i]);
tts.op.push_back(3);
if (v[tts.a][tts.b] == false)
{
v[tts.a][tts.b]=true;
q.push(tts);
}
tts.a=ts.a;
tts.b=0;
tts.op.clear();
for (i=0; i<ts.op.size(); i++)
tts.op.push_back(ts.op[i]);
tts.op.push_back(4);
if (v[tts.a][tts.b] == false)
{
v[tts.a][tts.b]=true;
q.push(tts);
}
if (b >= ts.b+ts.a)
{
tts.a=0;
tts.b=ts.b+ts.a;
}
else
{
tts.a=ts.b+ts.a-b;
tts.b=b;
}
tts.op.clear();
for (i=0; i<ts.op.size(); i++)
tts.op.push_back(ts.op[i]);
tts.op.push_back(5);
if (v[tts.a][tts.b] == false)
{
v[tts.a][tts.b]=true;
q.push(tts);
}
if (a >= ts.b+ts.a)
{
tts.a=ts.b+ts.a;
tts.b=0;
}
else
{
tts.a=a;
tts.b=ts.b+ts.a-a;
}
tts.op.clear();
for (i=0; i<ts.op.size(); i++)
tts.op.push_back(ts.op[i]);
tts.op.push_back(6);
if (v[tts.a][tts.b] == false)
{
v[tts.a][tts.b]=true;
q.push(tts);
}
}
if (q.empty())
{
printf("impossible\n");
}
else
{
printf("%d\n",ts.op.size());
for (i=0; i<ts.op.size(); i++)
{
switch (ts.op[i])
{
case 1:
printf("FILL(1)\n");
break;
case 2:
printf("FILL(2)\n");
break;
case 3:
printf("DROP(1)\n");
break;
case 4:
printf("DROP(2)\n");
break;
case 5:
printf("POUR(1,2)\n");
break;
case 6:
printf("POUR(2,1)\n");
break;
}
}
}
}