日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 3414 Pots【广搜】

發布時間:2025/5/22 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3414 Pots【广搜】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 ito 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

    6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1) code: View Code #include<stdio.h>
    #include<string.h>
    #define clr(x)memset(x,0,sizeof(x))
    int v[1001][1001];
    int va;
    int vb;
    struct node
    {
    int x,y,xu,step;
    }q[100000];
    int pre[100000];
    //1 full 1 2 full 2 3 drop 1 4 drop 2 5 from a to b 6 from b to a
    void print(int x)
    {
    if(pre[x]!=0)
    print(pre[x]);
    //printf("%d\n",q[x].xu);
    if(q[x].xu==1)
    printf("FILL(1)\n");
    else if(q[x].xu==2)
    printf("FILL(2)\n");
    else if(q[x].xu==3)
    printf("DROP(1)\n");
    else if(q[x].xu==4)
    printf("DROP(2)\n");
    else if(q[x].xu==5)
    printf("POUR(1,2)\n");
    else printf("POUR(2,1)\n");
    }
    int main()
    {
    int a,b,c,i,j,front,rear,tmp,v1,v2,res,ans;
    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
    va=vb=0;
    front=rear=0;
    q[rear].xu=0; q[rear].step=0; q[rear].x=0; q[rear++].y=0;
    res=-1;
    v[0][0]=1;

    memset(pre,0,sizeof(pre));
    while(front<=rear)
    {
    va=q[front].x; vb=q[front].y;
    //printf("%d %d\n",va,vb);
    if(va==c||vb==c) { res=q[front].step;ans=front; break;}
    if(!v[a][vb]) { v[a][vb]=1; q[rear].x=a; q[rear].y=vb; q[rear].xu=1; pre[rear]=front; q[rear++].step=q[front].step+1; }
    if(!v[va][b]) { v[va][b]=1; q[rear].x=va; q[rear].y=b; q[rear].xu=2; pre[rear]=front; q[rear++].step=q[front].step+1; }
    if(!v[0][vb]) { v[0][vb]=1; q[rear].x=0; q[rear].y=vb; q[rear].xu=3; pre[rear]=front; q[rear++].step=q[front].step+1; }
    if(!v[va][0]) { v[va][0]=1; q[rear].x=va; q[rear].y=0; q[rear].xu=4; pre[rear]=front; q[rear++].step=q[front].step+1; }
    if((va+vb)/b<1){
    if(!v[0][va+vb]){ v[0][va+vb]=1; q[rear].x=0; q[rear].y=va+vb; q[rear].xu=5; pre[rear]=front; q[rear++].step=q[front].step+1; }
    }
    else if(!v[va-(b-vb)][b]) { v[va-(b-vb)][b]=1; q[rear].x=va-(b-vb); q[rear].y=b; q[rear].xu=5; pre[rear]=front; q[rear++].step=q[front].step+1; }
    if((va+vb)/a<1){
    if(!v[va+vb][0]){ v[va+vb][0]=1; q[rear].x=va+vb; q[rear].y=0; q[rear].xu= 6; pre[rear]=front; q[rear++].step=q[front].step+1; }
    }
    else if(!v[a][vb-(a-va)]) { v[a][vb-(a-va)]=1; q[rear].x=a; q[rear].y=vb-(a-va); q[rear].xu=6; pre[rear]=front; q[rear++].step=q[front].step+1; }
    front++;
    }
    if(res==-1)printf("impossible\n");
    else {
    printf("%d\n",res);
    print(ans);
    }
    }
    return 0;
    }

    轉載于:https://www.cnblogs.com/dream-wind/archive/2012/03/22/2412181.html

    總結

    以上是生活随笔為你收集整理的POJ 3414 Pots【广搜】的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。