POJ 3414 Pots (BFS)__搜尋

來源:互聯網
上載者:User
Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i) empty the pot i to the drain; POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.


Input

On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).


Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.


Sample Input

3 5 4


Sample Output

6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)


題意

有兩個杯子,分別給出容量,然後我們對它做的有三種操作: 充滿1或2杯子 倒掉1或2杯子 從1倒進2或從2倒進1

問,怎麼樣在最少的操作次數下讓某一個杯子剛好達到指定量。


思路

我們可以認為初始狀態兩個杯子都是空的,然後對於每一種操作都算是一種狀態,把它排入佇列,然後對隊列中的所有狀態再進行下一次操作,也就是bfs啦。

不過需要注意的是,我們不能讓同一個狀態被排入佇列兩次(防止爆棧),因此要過濾一些,已知a,b小於100,因此可以用一個二維數組來儲存某種狀態是否被訪問過。

最終輸出最短的操作次數與步驟。

也不知道我是為了圖想起來簡單還是什麼,所有的操作都是一個單獨的判斷,看起來代碼略長。。。


AC 代碼

#include <iostream>#include<stdio.h>#include<queue>#include<string.h>#include<math.h>using namespace std;typedef __int64 LL;bool visited[105][105];struct node{    int left;    int right;    int time;    string path;    void init(int left,int right,string path,int time)    {        this->left=left;        this->right=right;        this->path=path;        this->time=time;    }};void bfs(int left,int right,int c){    queue<node>sk;    bool flag=false;    node s;    s.init(0,0,"",0);    visited[0][0]=true;    sk.push(s);    while(!sk.empty())    {        node p=sk.front();        sk.pop();        if(p.left==c||p.right==c)        {            cout<<p.time<<endl<<p.path;            flag=true;            return;        }        node tmp;        if(!visited[left][p.right])     //充滿1        {            tmp.init(left,p.right,p.path+"FILL(1)\n",p.time+1);            visited[left][p.right]=true;            sk.push(tmp);        }        if(!visited[p.left][right])     //充滿2        {            tmp.init(p.left,right,p.path+"FILL(2)\n",p.time+1);            visited[p.left][right]=true;            sk.push(tmp);        }        if(!visited[0][p.right])        //釋放1        {            tmp.init(0,p.right,p.path+"DROP(1)\n",p.time+1);            visited[0][p.right]=true;            sk.push(tmp);        }        if(!visited[p.left][0])         //釋放2        {            tmp.init(p.left,0,p.path+"DROP(2)\n",p.time+1);            visited[p.left][0]=true;            sk.push(tmp);        }        if(left-p.left<p.right&&!visited[left][p.right-(left-p.left)])  //從2到1        {            tmp.init(left,p.right-(left-p.left),p.path+"POUR(2,1)\n",p.time+1);            visited[left][p.right-(left-p.left)]=true;            sk.push(tmp);        }        else if(left-p.left>=p.right&&!visited[p.left+p.right][0])        {            tmp.init(p.left+p.right,0,p.path+"POUR(2,1)\n",p.time+1);            visited[p.left+p.right][0]=true;            sk.push(tmp);        }        if(p.left<=right-p.right&&!visited[0][p.left+p.right])          //從1到2        {            tmp.init(0,p.left+p.right,p.path+"POUR(1,2)\n",p.time+1);            visited[0][p.left+p.right]=true;            sk.push(tmp);        }        else if(p.left>right-p.right&&!visited[p.left-(right-p.right)][right])        {            tmp.init(p.left-(right-p.right),right,p.path+"POUR(1,2)\n",p.time+1);            visited[p.left-(right-p.right)][right]=true;            sk.push(tmp);        }    }    if(!flag)        printf("impossible\n");}int main(){    int a,b,c;    while(~scanf("%d%d%d",&a,&b,&c))    {        memset(visited,false,sizeof(visited));        bfs(a,b,c);    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.