hdu1415和poj 1415 Jugs

来源:互联网 发布:高性能网络编程 陶辉 编辑:程序博客网 时间:2024/04/30 13:15

http://acm.hdu.edu.cn/showproblem.php?pid=1415

这个hdu的题目有问题,应该是把b的水变为t,第一次wr了,看第二个例子就可以看出是把b的水变为t。浙江大学oj是正确的。
用bfs遍历,用一个二维数组保存前面的状态和倒水的方式就行,1-6分别代表6种不同的倒水方式。就可以解决,但是还是用了1个小时而且代码比较冗长。。。。

#include<iostream>#include<queue>using namespace std;int a, b, t;struct node{    int a,b,p;}s[1002][1002];void show(int x,int y){    if (s[x][y].a >= 0  && s[x][y].b>=0)    {        show(s[x][y].a, s[x][y].b);        switch (s[x][y].p){        case 1:cout << "fill A" << endl; break;        case 2:cout << "fill B" << endl; break;        case 3:cout << "empty A" << endl; break;        case 4:cout << "empty B" << endl; break;        case 5:cout << "pour A B" << endl; break;        case 6:cout << "pour B A" << endl; break;        }    }}void bfs(){    queue<node> qu;    node now = { 0,0,0};    qu.push(now);    s[0][0].a = s[0][0].b = -1;    s[0][0].p = 0;    while (qu.size()>0)    {        node temp;        now = qu.front();        qu.pop();        if (now.b == t){            show(now.a, now.b);            return;        }        if (now.a != a){            temp.a = a;             temp.b = now.b;            if (s[temp.a][temp.b].p== -1){                s[temp.a][temp.b].p = 1;                s[temp.a][temp.b].a = now.a;                s[temp.a][temp.b].b = now.b;                qu.push(temp);            }        }        if (now.b != b){            temp.a = now.a;            temp.b = b;            if (s[temp.a][temp.b].p == -1){                s[temp.a][temp.b].p = 2;                s[temp.a][temp.b].a = now.a;                s[temp.a][temp.b].b = now.b;                qu.push(temp);            }        }        if (now.a != 0){            temp.a = 0;            temp.b = now.b;            if (s[temp.a][temp.b].p == -1){                s[temp.a][temp.b].p = 3;                s[temp.a][temp.b].a = now.a;                s[temp.a][temp.b].b = now.b;                qu.push(temp);            }        }        if (now.b != 0){            temp.a = now.a;            temp.b = 0;            if (s[temp.a][temp.b].p == -1){                s[temp.a][temp.b].p = 4;                s[temp.a][temp.b].a = now.a;                s[temp.a][temp.b].b = now.b;                qu.push(temp);            }        }        if (now.a != 0 && now.b != b){            if (now.a >= b - now.b){                temp.b = b;                temp.a = now.a - (b - now.b);            }            else            {                temp.a = 0;                temp.b = now.a + now.b;            }            if (s[temp.a][temp.b].p == -1){                s[temp.a][temp.b].p = 5;                s[temp.a][temp.b].a = now.a;                s[temp.a][temp.b].b = now.b;                qu.push(temp);            }        }        if (now.a != a && now.b != 0){            if (now.b >= a - now.a){                temp.a = a;                temp.b = now.b - (a - now.a);            }            else            {                temp.b = 0;                temp.a = now.a + now.b;            }            if (s[temp.a][temp.b].p == -1){                s[temp.a][temp.b].p = 6;                s[temp.a][temp.b].a = now.a;                s[temp.a][temp.b].b = now.b;                qu.push(temp);            }        }    }}int main(){    while (cin >> a >> b >> t)    {        for (int i = 0; i <= a; i++){            for (int j = 0; j <= b; j++){                s[i][j].p = -1;            }        }        bfs();        cout << "success" << endl;    }    return 0;}
0 0
原创粉丝点击