倒水問題C++(BFS)

來源:互聯網
上載者:User

標籤:border   const   ble   dep   一個   bfs   min   ica   sizeof   

有裝滿水的6升的杯子、空的3升杯子和1升杯子, 3個杯子中都沒有刻度. 在不使用其他道具的情況下, 是否可以量出4升的水呢? 

你的任務是解決一般性的問題:設大、中、小3個杯子的容量分別為a,b,c,最初只有大杯子裝滿水,其他兩個杯子為空白。最少需要多少步才能讓某一個杯子中的水有x升呢?你需要列印出每步操作後各個杯子中的水量(0<c<b<a<1000)。

範例輸入

範例輸出

2

6 3 1 4

9 6 3 1

最少需要3步:(6,3,1)-->(3,3,0)-->(3,2,1)-->(4,2,0)

No

 

話不多說,先上碼:

 

#include <iostream>

#include <queue>

using namespace std;

 

const int MAXN=1000;

 

class node

{

public:

    int cup[3]={0,0,0},last=0,depth=0;

};

 

int vis[MAXN][MAXN],i_cup[3],target;

node v[MAXN];

queue<int> number;

 

void progress(int x)

{

    if (!x)

    {

        printf("(%d,%d,%d)-->",i_cup[0],i_cup[1],i_cup[2]);

        return;

    }

    progress(v[x].last);

    printf("(%d,%d,%d)-->",v[x].cup[0],v[x].cup[1],v[x].cup[2]);

}

 

void bfs()

{

    int k=0;

    while (!number.empty())

        number.pop();

    v[k].cup[0]=i_cup[0];

    vis[v[k].cup[1]][v[k].cup[2]]=1;

    number.push(k);

    while (!number.empty())

    {

        if ((v[number.front()].cup[0]==target)||(v[number.front()].cup[1]==target)||(v[number.front()].cup[2]==target))

        {

            cout<<"最少需要"<<v[number.front()].depth<<"步:";

            progress(v[number.front()].last);

            printf("(%d,%d,%d)",v[number.front()].cup[0],v[number.front()].cup[1],v[number.front()].cup[2]);

            cout<<endl;

            return;

        }

        for (int out=0; out<3; out++)

        {

            for (int in=0; in<3; in++)

            {

                if (in==out)

                    continue;

                int num=min(v[number.front()].cup[out],i_cup[in]-v[number.front()].cup[in]);

                if (!num)

                    continue;

                node tem=v[number.front()];

                tem.cup[in]=v[number.front()].cup[in]+num;

                tem.cup[out]=v[number.front()].cup[out]-num;

                if (!vis[tem.cup[1]][tem.cup[2]])

                {

                    vis[tem.cup[1]][tem.cup[2]]=1;

                    v[++k]=tem;

                    v[k].last=number.front();

                    v[k].depth=v[number.front()].depth+1;

                    number.push(k);

                }

            }

        }

        number.pop();

    }

    cout<<"No"<<endl;

}

 

int main()

{

    int T;

    cin>>T;

    while (T--) {

        memset(vis, 0, sizeof(vis));

        cin>>i_cup[0]>>i_cup[1]>>i_cup[2]>>target;

        bfs();

    }

}

倒水問題C++(BFS)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.