poj 3414 搜尋

來源:互聯網
上載者:User
Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8211   Accepted: 3481   Special Judge

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 ≤ ≤ 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 AB, 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)

這是一道廣搜的題目,下面是代碼:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>using namespace std;const int maxn=101;int vis[maxn][maxn],q[maxn*maxn*maxn][5];int A,B,C,fa;int bfs(){    int front=0,rear=0,a,b,na,nb,nfront,ans;    q[rear][0]=0,q[rear][1]=0,q[rear][2]=0,q[rear++][4]=0;    vis[0][0]=1;    while(rear>front){         a=q[front][0],b=q[front][1],ans=q[front][4];         nfront=front,front++;         if(a==C||b==C){//到達目標              fa=nfront;              return 1;         }         na=A,nb=b;         if(!vis[na][nb]){//fill 1             vis[na][nb]=1;             q[rear][0]=na,q[rear][1]=nb;             q[rear][2]=1,q[rear][3]=nfront;             q[rear++][4]=ans+1;         }         na=a,nb=B;          if(!vis[na][nb]){//fill 2             vis[na][nb]=1;             q[rear][0]=na,q[rear][1]=nb;             q[rear][2]=2,q[rear][3]=nfront;             q[rear++][4]=ans+1;         }         na=0,nb=b;         if(!vis[na][nb]){//drop 1             vis[na][nb]=1;             q[rear][0]=na,q[rear][1]=nb;             q[rear][2]=3,q[rear][3]=nfront;             q[rear++][4]=ans+1;         }         na=a,nb=0;         if(!vis[na][nb]){//drop 2             vis[na][nb]=1;             q[rear][0]=na,q[rear][1]=nb;             q[rear][2]=4,q[rear][3]=nfront;             q[rear++][4]=ans+1;         }         int full;         full=(a+b)/B;         full?(na=a+b-B,nb=B):(na=0,nb=a+b);          if(!vis[na][nb]){//pour 1 to 2              vis[na][nb]=1;             q[rear][0]=na,q[rear][1]=nb;             q[rear][2]=5,q[rear][3]=nfront;             q[rear++][4]=ans+1;         }         full=(a+b)/A;         full?(na=A,nb=a+b-A):(na=a+b,nb=0);          if(!vis[na][nb]){//pour 2 to 1             vis[na][nb]=1;             q[rear][0]=na,q[rear][1]=nb;             q[rear][2]=6,q[rear][3]=nfront;             q[rear++][4]=ans+1;         }    }    return 0;}void print(int fa){//列印路徑     if(fa==0) return;     print(q[fa][3]);     if(q[fa][2]<=2)        printf("FILL(%d)\n",q[fa][2]);     else if(q[fa][2]<=4)printf("DROP(%d)\n",q[fa][2]/2);     else {        if(q[fa][2]==5)printf("POUR(1,2)\n");        else printf("POUR(2,1)\n");     }     return;}int main(){    int ok;    while(scanf("%d%d%d",&A,&B,&C)!=EOF){             memset(vis,0,sizeof(vis));             ok=bfs();             if(ok){                printf("%d\n",q[fa][4]);                print(fa);             }             else                printf("impossible\n");     }     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.