下面的两段代码我觉得是一模一样的意思啊!!!!为什么确有不一样的结果呢?
代码的意思是:
给你两个容器分别容量为:Ca,Cb。让你通过6种方法配置出N升溶液出来。
一般来说输入的数据满足0 < Ca <= Cb 和 N <= Cb <=1000
这6种方法分别为:
1.倒满容器a
2.倒满容器b
3.倒空容器a
4.倒空容器b
5.容器a倒入容器b
6.容器b倒入容器a
两段代码如下:
[code=c]
#include <iostream>
#include <queue>
#include <string>
using namespace std;

struct Jugs
{
    int a, b;
    string ops;
};

string BFS(int ca, int cb, int n)
{
    bool arrived[1001][1001];
    for(int i = 0; i < 1001; ++i)
        for(int j = 0; j < 1001; ++j)
            arrived[i][j] = false;
    queue<Jugs> Q;
    Jugs s, u, v;
    s.a = s.b = 0;
    s.ops = "";
    Q.push(s);
    arrived[s.a][s.b] = true;
    while( !Q.empty() )
    {
        u = Q.front();
        Q.pop();
        if( u.b == n )
        {
            u.ops.push_back('6');
            break;
        }
        else
        {
            //fill A
            v = u;
            v.a = ca;
            if(!arrived[v.a][v.b])
            {
                arrived[v.a][v.b] = true;
                v.ops.push_back('0');
                Q.push(v);
            }
            //fill B
            v = u;
            v.b = cb;
            if(!arrived[v.a][v.b])
            {
                arrived[v.a][v.b] = true;
                v.ops.push_back('1');
                Q.push(v);
            }
            //empty A
            v = u;
            v.a = 0;
            if(!arrived[v.a][v.b])
            {
                arrived[v.a][v.b] = true;
                v.ops.push_back('2');
                Q.push(v);
            }
            //empty B
            v = u;
            v.b = 0;
            if(!arrived[v.a][v.b])
            {
                arrived[v.a][v.b] = true;
                v.ops.push_back('3');
                Q.push(v);
            }
            //pour A B
            v = u;
            v.b += v.a;
            if( v.b > cb )
            {
                v.a = v.b - cb;
                v.b = cb;
            }
            else
            {
                v.a = 0;
            }
            if(!arrived[v.a][v.b])
            {
                arrived[v.a][v.b] = true;
                v.ops.push_back('4');
                Q.push(v);
            }
            //pour B A
            v = u;
            v.a += v.b;
            if( v.a > ca )
            {
                v.a = ca;
                v.b = v.a - ca;
            }
            else
            {
                v.b = 0;
            }
            if(!arrived[v.a][v.b])
            {
                arrived[v.a][v.b] = true;
                v.ops.push_back('5');
                Q.push(v);
            }
        }
    }

    return u.ops;
}

int main()
{
    const string operations[] = { "fill A", "fill B", "empty A", "empty B", "pour A B", "pour B A", "success" };
    int ca, cb, n;
    while( cin >> ca >> cb >> n )
    {
        string ops = BFS(ca, cb, n);
        for( unsigned int i = 0; i < ops.size(); i++ )
            cout << operations[ ops[i] - '0' ] << endl;
    }
    return 0;
}

[/code]
输入:3 5 4
输出:
fill A
pour A B
fill A
pour A B
empty B
pour A B
fill A
pour A B
success
-------------------------------------------------------------------------------------


[code=c]
#include <iostream>
#include <string>
#include <queue>
using namespace std;

const string operations[] = { "fill A", "fill B", "empty A", "empty B", "pour A B", "pour B A", "success" };

struct State
{
    int A, B;
    string op;
}ST,tmp, now;

int Ca, Cb, N;
bool arrd[1001][1001];
queue<State> Q;

void BFS()
{
    
    for(int i = 0; i < 1001; ++i)
        for(int j = 0; j < 1001; ++j)
            arrd[i][j] = false;
    while ( !Q.empty() )
        Q.pop();
    ST.A = ST.B = 0;
    ST.op = "";
    arrd[ST.A][ST.B] = true;
    Q.push( ST );
    while ( !Q.empty() )
    {
        tmp = Q.front();
        Q.pop();
        if ( tmp.B == N )
        {
            tmp.op.push_back('6');
            break;
        }
        //fill A
        now = tmp;
        now.A = Ca;
        if ( !arrd[now.A][now.B] )
        {
            now.op.push_back('0');
            Q.push(now);
            arrd[now.A][now.B] = true;
        }
        //fill B
        now = tmp;
        now.B = Cb;
        if ( !arrd[now.A][now.B] )
        {
            now.op.push_back('1');
            Q.push(now);
            arrd[now.A][now.B] = true;
        }
        //empty A
        now = tmp;
        now.A = 0;
        if ( !arrd[now.A][now.B] )
        {
            now.op.push_back('2');
            Q.push(now);
            arrd[now.A][now.B] = true;
        }
        //empty B
        now = tmp;
        now.B = 0;
        if ( !arrd[now.A][now.B] )
        {
            now.op.push_back('3');
            Q.push(now);
            arrd[now.A][now.B] = true;
        }   
        //pour A B
        now = tmp;
        now.B += now.A;
        if ( now.B > Cb )
        {
            now.A = now.B - Cb;
            now.B = Cb;
        }
        else
        {
            now.A = 0;
        }
        if ( !arrd[now.A][now.B] )
        {
            now.op.push_back('4');
            Q.push(now);
            arrd[now.A][now.B] = true;
        }
        //pour B A
        now = tmp;
        now.A += now.B;
        if ( now.A > Ca )
        {
            now.B = now.A - Ca;
            now.A = Ca;
        }
        else
        {
            now.B = 0;
        }
        if ( !arrd[now.A][now.B] )
        {
            now.op.push_back('5');
            Q.push(now);
            arrd[now.A][now.B] = true;
        }             
    }
}

int main()
{
    while ( cin >> Ca >> Cb >> N )
    {
        BFS();
        for ( unsigned i = 0; i < tmp.op.size(); ++ i )
        {
            cout << operations[ tmp.op[i] - '0' ] << endl;
        }
    }
    return 0;
}

[/code]
输入:3 5 4
输出:
fill B
pour B A
empty A
pour B A
fill B
pour B A
success