//和poj1606的题目的解法是一样的,不过有点地方需要注意,这题当没解的时候要输出impossible,还有无论是哪个杯的水的容量符合要求就可以输出了!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 110;
int a, b, c, r, l, vis[MAX][MAX], step[MAX*MAX];
string opr[7] = {" ", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(2,1)", "POUR(1,2)"};
struct Info
{
int x;
int y;
int operation;
int pre;//用链表的方式进行存储,有利于回溯找出路径
}info[MAX*MAX];
void solve(int xx, int yy, int ss)
{
if (vis[xx][yy])
return ;
vis[xx][yy] = 1;
info[r].x = xx;
info[r].y = yy;
info[r].operation = ss;
info[r].pre = l;
r++;
}
void print()
{
int i, ans;
ans = 0;
while (l != 0){
step[ans++] = info[l].operation;
l = info[l].pre;
}
cout << ans << endl;
for (i = ans - 1; i >= 0; i--){
cout << opr[step[i]] << endl;
}
}
void BFS()
{
info[0].x = 0;
info[0].y = 0;
vis[0][0] = 1;
l = 0;
r = 1;
while (l != r){
//这个地方与poj1606稍微有区别的
if (info[l].y == c || info[l].x == c){
print();
return ;
}
int tmpx, tmpy;
//第一种方法fill(1)
tmpx = a;
tmpy = info[l].y;
solve(tmpx, tmpy, 1);
//第二种方法fill(2)
tmpx = info[l].x;
tmpy = b;
solve(tmpx, tmpy, 2);
//第三种方法drop(1);
tmpx = 0;
tmpy = info[l].y;
solve(tmpx, tmpy, 3);
//第四种方法drop(2)
tmpx = info[l].x;
tmpy = 0;
solve(tmpx, tmpy, 4);
//第五种方法fill(2, 1)
tmpx = info[l].x + min(a - info[l].x, info[l].y);
tmpy = info[l].y - min(a - info[l].x, info[l].y);
solve(tmpx, tmpy, 5);
//第六种方法fill(1, 2)
tmpx = info[l].x - min(b - info[l].y, info[l].x);
tmpy = info[l].y + min(b - info[l].y, info[l].x);
solve(tmpx, tmpy, 6);
l++;
}
if (l >= r)
cout << "impossible" << endl;
}
int main()
{
cin >> a >> b >> c;
memset(vis, 0, sizeof(vis));
BFS();
system("pause");
}
poj 3414 Pots
最新推荐文章于 2022-02-25 19:38:17 发布