POJ 3414 Pots (經典bfs )

來源:互聯網
上載者:User

標籤:poj   遞迴   bfs   string   

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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)

Source

Northeastern Europe 2002, Western Subregion
兩個壺,可以清空,可以加滿,可以相互倒水,不過每次到要麼這個壺水倒完,要麼那個壺滿了,要求一個壺有一定的水結束,輸出步驟
bfs具體在代碼中

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define N 105struct stud{int a,b;int time;string mv;};int a,b,c;int vis[N][N];int judge(int x,int y){    if(x==c||y==c)        return 1;    return 0;}stud bfs(){    struct stud next,cur;    queue<stud>q;    while(!q.empty())        q.pop();    cur.a=cur.b=0;    cur.mv="";    cur.time=0;    if(cur.a==c||cur.b==c)        return cur;    q.push(cur);    memset(vis,0,sizeof(vis));    vis[0][0]=1;    while(!q.empty())    {        cur=q.front();        q.pop();        if(cur.a<a&&!vis[a][cur.b]) //裝滿a        {            next=cur;            next.a=a;            next.time++;            next.mv+="F1";            vis[next.a][next.b];            if(judge(next.a,next.b)) return next;            q.push(next);        }        if(cur.b<b&&!vis[cur.a][b])   //裝滿b        {            next=cur;            next.b=b;            next.time++;            next.mv+="F2";            vis[next.a][next.b]=1;            if(judge(next.a,next.b)) return next;            q.push(next);        }        if(cur.a>0&&!vis[0][cur.b])   //倒出a        {            next.a=0;            next.b=cur.b;            next.mv=cur.mv;            next.mv+="D1";            next.time=cur.time+1;            vis[next.a][next.b]=1;            if(judge(next.a,next.b)) return next;            q.push(next);        }        if(cur.b>0&&!vis[cur.a][0])    //倒出b        {            next.b=0;            next.a=cur.a;            next.mv=cur.mv;            next.mv+="D2";            next.time=cur.time+1;            vis[next.a][next.b]=1;            if(judge(next.a,next.b)) return next;            q.push(next);        }        //a給b倒水        if(cur.b<b&&cur.a>0&&cur.a+cur.b<=b&&!vis[0][cur.a+cur.b])        {            next.a=0;            next.b=cur.a+cur.b;            next.time=cur.time+1;            next.mv=cur.mv+"P12";            vis[next.a][next.b]=1;             if(judge(next.a,next.b)) return next;            q.push(next);        }        else          if(cur.b<b&&cur.a>0&&cur.a+cur.b>b&&!vis[cur.a+cur.b-b][b])          {              next.a=cur.a+cur.b-b;              next.b=b;              next.time=cur.time+1;              next.mv=cur.mv+"P12";              vis[next.a][next.b]=1;              if(judge(next.a,next.b)) return next;              q.push(next);          }          //b給a倒水          if(cur.b>0&&cur.a!=a&&cur.a+cur.b<=a&&!vis[cur.a+cur.b][0])          {              next.a=cur.a+cur.b;              next.b=0;              next.time=cur.time+1;              next.mv=cur.mv+"P21";              vis[next.a][next.b]=1;               if(judge(next.a,next.b)) return next;              q.push(next);          }          else            if(cur.b>0&&cur.a!=a&&cur.a+cur.b>a&&!vis[a][cur.a+cur.b-a])          {              next.a=a;              next.b=cur.a+cur.b-a;              next.time=cur.time+1;              next.mv=cur.mv+"P21";              vis[next.a][next.b]=1;               if(judge(next.a,next.b)) return next;              q.push(next);          }    }   struct stud ans;    ans.a=-1;    return ans;}int main(){        int i;        scanf("%d%d%d",&a,&b,&c);        struct stud cur;        cur=bfs();        if(cur.a==-1)        {            printf("impossible\n");            return 0;        }        printf("%d\n",cur.time);        int len=cur.mv.size();        for(i=0;i<len;i++)        {            if(cur.mv[i]=='D')            {                printf("DROP(");                i++;                printf("%c)\n",cur.mv.at(i));            }            else            if(cur.mv[i]=='F')            {                printf("FILL(");                i++;                printf("%c)\n",cur.mv.at(i));            }            else            {                printf("POUR(");                i++;                printf("%c",cur.mv.at(i));                i++;                printf(",%c)\n",cur.mv.at(i));            }        }    return 0;}



聯繫我們

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