H

题目大意:
有一个瓶子A和一个瓶子B,可以有三种操作倒满,倒空,或者把瓶子A倒向瓶子B(或者把瓶子B倒向瓶子A),可以扩展出6种操作,没什么简单的写法,只能一种一种的写.....
当然是使用广搜.......................直到一个瓶子里面有C升水,或者倒不出来这种结果,还需要输出到得步骤, 可以递归输出路径......
///////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;

#define maxn 110

int A, B, C;
char a[10][10] ={ "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};

struct node
{
    int fromX, fromY, step, op;
}v[maxn][maxn];
struct point
{
    int x, y;
};//当前瓶子A,B的水量

//输出路径
void DFS(int x, int y)
{
    if(x == 0 && y == 0)
        return ;

    DFS(v[x][y].fromX, v[x][y].fromY);

    printf("%s ", a[ v[x][y].op ]);

}
void BFS()
{
    queue<point> Q;
    point s, q;
    s.x = s.y = 0;
    v[0][0].step = 1;

    Q.push(s);

    while(Q.size())
    {
        s = Q.front();Q.pop();

        if(s.x == C || s.y == C)
        {
            printf("%d ", v[s.x][s.y].step-1);
            DFS(s.x, s.y);

            return ;
        }

        for(int i=0; i<6; i++)
        {
            q = s;

            if(i == 0)
                q.x = A;
            else if(i == 1)
                q.y = B;
            else if(i == 2)
                q.x = 0;
            else if(i == 3)
                q.y = 0;
            else if(i == 4)
            {
                if(q.x+q.y <= B)
                    q.y += q.x, q.x = 0;//把A里面的水全倒B里面
                else
                    q.x -= (B-q.y), q.y = B;//把B倒满
            }
            else
            {
                if(q.x+q.y <= A)
                    q.x += q.y, q.y = 0;
                else
                    q.y -= (A-q.x), q.x = A;
            }

            if(v[q.x][q.y].step == 0)
            {
                v[q.x][q.y].step = v[s.x][s.y].step+1;
                v[q.x][q.y].fromX = s.x;
                v[q.x][q.y].fromY = s.y;
                v[q.x][q.y].op = i;

                Q.push(q);
            }
        }
    }

    printf("impossible ");
}

int main()
{
    while(scanf("%d%d%d", &A, &B, &C) != EOF)
    {
        memset(v, 0sizeof(v));

        BFS();
    }

    return 0;

} 

原文地址:https://www.cnblogs.com/liuxin13/p/4649133.html