参考文章:思路公式 代码思路
原来的代码有点瑕疵,这是最新的AC代码
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int maxn = 300;
string path[maxn] = {
"FILL(1)"
,"FILL(2)"
,"DROP(1)"
,"DROP(2)"
,"POUR(1,2)"
,"POUR(2,1)" };
struct node
{
int a, b;
int pre;
int op;
int step;
};
int a, b, c;
node q[maxn];
int front = 0, rear = 0;
int vis[maxn][maxn];
void bfs(int x, int y)
{
memset(vis, 0, sizeof(vis));
node t;
t.a = x; t.b = y;
t.op = -1; t.pre = -1; t.step = 0;
q[rear] = t;
rear++;
vis[x][y] = 1;
while (front != rear)
{
node now = q[front];//front++;//不可放在这里,后面next还要用到现在的front
if (now.a == c || now.b == c)
{
q[rear] = now;//要记得加
cout << now.step << endl;
return;
}
node next = now;
next.step++;
next.pre = front;
if (now.a < a)
{
next.a = a;
next.b = now.b;
if (!vis[next.a][next.b])
{
next.op = 0;
q[rear++] = next;
vis[next.a][next.b] = 1;
}
}
if (now.b < b)
{
next.b = b;
next.a = now.a;
if (!vis[next.a][next.b])
{
next.op = 1;
q[rear++] = next;
vis[next.a][next.b] = 1;
}
}
if (now.a > 0)
{
next.a = 0;
next.b = now.b;
if (!vis[next.a][next.b])
{
next.op = 2;
q[rear++] = next;
vis[next.a][next.b] = 1;
}
}
if (now.b > 0)
{
next.b = 0;
next.a = now.a;
if (!vis[next.a][next.b])
{
next.op = 3;
q[rear++] = next;
vis[next.a][next.b] = 1;
}
}
if (now.a > 0 && now.b < b)
{
if (now.a > b - now.b)
{
next.b = b;
next.a = now.a - (b - now.b);
}
else
{
next.a = 0;
next.b = now.b + now.a;
}
if (!vis[next.a][next.b])
{
next.op = 4;
q[rear++] = next;
vis[next.a][next.b] = 1;
}
}
if (now.b > 0 && now.a < a)
{
if (now.b > a - now.a)
{
next.a = a;
next.b = now.b - (a - now.a);
}
else
{
next.b = 0;
next.a = now.a + now.b;
}
if (!vis[next.a][next.b])
{
next.op = 5;
q[rear++] = next;
vis[next.a][next.b] = 1;
}
}
front++;
}
cout << "impossible" << endl;
}
void display(int x)
{
if (q[x].pre == -1)
{
//cout << q[x].op <<" "<< path[q[x].op] << endl;没用的语句!
return;
}
display(q[x].pre);
cout << path[q[x].op] << endl;
}
int main()
{
cin >> a >> b >> c;
bfs(0, 0);
display(rear);
return 0;
}
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int maxn = 115;
struct node
{
int a, b;
int path[maxn];
int step;
};
int a, b, c;
int vis[maxn][maxn];
string path[] = {
"FILL(1)"
,"FILL(2)"
,"DROP(1)"
,"DROP(2)"
,"POUR(1,2)"
,"POUR(2,1)"
};
void output(int p[], int step)
{
cout << step << endl;
for (int i = 0; i < step; i++)
cout << path[p[i]] << endl;
}
void bfs()
{
queue<node> q;
memset(vis, 0, sizeof(vis));
node t;
t.a = 0; t.b = 0;
/*t.level = 0;*/ t.step = 0;
memset(t.path, 0, sizeof(t.path));
q.push(t);
vis[t.a][t.b] = 1;
while (!q.empty())
{
node f = q.front();
q.pop();
if (f.a == c || f.b == c)
{
output(f.path, f.step);
return;
}
node next = f;
//next.level++;
next.step++;
//直接装满a
if (a > f.a)//a的容量比现在a里的水多
{
next.a = a;
next.b = f.b;
if (!vis[next.a][next.b])
{
next.path[f.step] = 0;//next.path[next.step - 1] = 0;
q.push(next);
vis[next.a][next.b] = 1;
}
}
//直接装满b
if (b > f.b)//b的容量比现在b里的水多
{
next.b = b;
next.a = f.a;
if (!vis[next.a][next.b])
{
next.path[f.step] = 1;//next.path[next.step - 1] = 1;
q.push(next);
vis[next.a][next.b] = 1;
}
}
//倒空a
if (f.a > 0)
{
next.a = 0;
next.b = f.b;
if (!vis[next.a][next.b])
{
next.path[f.step] = 2;//next.path[next.step - 1] = 2;
q.push(next);
vis[next.a][next.b] = 1;
}
}
//倒空b
if (f.b > 0)
{
next.b = 0;
next.a = f.a;
if (!vis[next.a][next.b])
{
next.path[f.step] = 3;//next.path[next.step - 1] = 2;
q.push(next);
vis[next.a][next.b] = 1;
}
}
//把a倒入b里
if (f.a > 0 && f.b < b)
{
if (f.a > b - f.b)
{
next.b = b;
next.a = f.a - (b - f.b);
}
else
{
next.a = 0;
next.b = f.a + f.b;
}
if (!vis[next.a][next.b])
{
next.path[f.step] = 4;
q.push(next);
vis[next.a][next.b] = 1;
}
}
//把b倒入b里
if (f.b > 0 && f.a < a)
{
if (f.b > a - f.a)
{
next.a = a;
next.b = f.b - (a - f.a);
}
else
{
next.b = 0;
next.a = f.a + f.b;
}
if (!vis[next.a][next.b])
{
next.path[f.step] = 5;
q.push(next);
vis[next.a][next.b] = 1;
}
}
}
//都不行的话
cout << "impossible" << endl;
}
int main()
{
cin >> a >> b >> c;
bfs();
return 0;
}