• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj3414

            Pots

            Time Limit: 1000MS Memory Limit: 65536K
            Total Submissions: 6116 Accepted: 2582 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 ≤ 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

            6
            FILL(2)
            POUR(2,1)
            DROP(1)
            POUR(2,1)
            FILL(2)
            POUR(2,1)
            挺簡單的題目
            沒看完題目,不知道還有impossible的情況,wa了一次,然后第二遍忘了輸出impossible之后要return,re一次
            呃,蛋疼
              1#include<stdio.h>
              2#include<string.h>
              3#include<math.h>
              4int a,b,c;
              5struct node
              6{
              7    int a,b,d,d1;
              8}
            ;
              9struct node q[100050];
             10int wh[10005];
             11int num0[10005],num;
             12short flag[105][105];
             13int head,tail,k;
             14int bfs()
             15{
             16    int nowa,nowb,aa,bb;
             17    head=0;
             18    tail=1;
             19    q[tail].a=0;
             20    q[tail].b=0;
             21    flag[0][0]=1;
             22    wh[tail]=0;
             23    while (head<tail)
             24    {
             25        head++;
             26        nowa=q[head].a;
             27        nowb=q[head].b;
             28        if ((q[head].a==c)||(q[head].b==c))
             29        {
             30            return head;
             31        }

             32        if (nowa!=a)
             33        {
             34            if (!flag[a][nowb])
             35            {
             36                tail++;
             37                q[tail].a=a;
             38                q[tail].b=nowb;
             39                q[tail].d=1;
             40                q[tail].d1=1;
             41                wh[tail]=head;
             42                flag[a][nowb]=1;
             43            }

             44        }

             45        if (nowb!=b)
             46        {
             47            if (!flag[nowa][b])
             48            {
             49                tail++;
             50                q[tail].a=nowa;
             51                q[tail].b=b;
             52                q[tail].d=1;
             53                q[tail].d1=2;
             54                wh[tail]=head;
             55                flag[nowa][b]=1;
             56            }

             57        }

             58        if (nowa!=0)
             59        {
             60            if (!flag[0][nowb])
             61            {
             62                tail++;
             63                q[tail].a=0;
             64                q[tail].b=nowb;
             65                q[tail].d=2;
             66                q[tail].d1=1;
             67                wh[tail]=head;
             68                flag[0][nowb]=1;
             69            }

             70        }

             71        if (nowb!=0)
             72        {
             73            if (!flag[nowa][0])
             74            {
             75                tail++;
             76                q[tail].a=nowa;
             77                q[tail].b=0;
             78                q[tail].d=2;
             79                q[tail].d1=2;
             80                wh[tail]=head;
             81                flag[nowa][0]=1;
             82            }

             83        }

             84        if (nowa!=0)
             85        {
             86            if (nowa>=b-nowb)
             87            {
             88                aa=nowa+nowb-b;
             89                //printf("aa=%d\n",aa);
             90                bb=b;
             91                if (!flag[aa][bb])
             92                {
             93                    tail++;
             94                    q[tail].a=aa;
             95                    q[tail].b=bb;
             96                    q[tail].d=3;
             97                    q[tail].d1=1;
             98                    wh[tail]=head;
             99                    flag[aa][bb]=1;
            100                }

            101            }

            102            else if(nowa<b-nowb)
            103            {
            104                aa=0;
            105                bb=nowb+nowa;
            106                if (!flag[aa][bb])
            107                {
            108                    tail++;
            109                    q[tail].a=aa;
            110                    q[tail].b=bb;
            111                    q[tail].d=3;
            112                    q[tail].d1=1;
            113                    wh[tail]=head;
            114                    flag[aa][bb]=1;
            115                }

            116            }

            117        }

            118        if (nowb!=0)
            119        {
            120            if (nowb>=a-nowa)
            121            {
            122                aa=a;
            123                bb=nowb+nowa-a;
            124                if (!flag[aa][bb])
            125                {
            126                    tail++;
            127                    q[tail].a=aa;
            128                    q[tail].b=bb;
            129                    q[tail].d=3;
            130                    q[tail].d1=2;
            131                    wh[tail]=head;
            132                    flag[aa][bb]=1;
            133                }

            134            }

            135            else if (nowb<a-nowa)
            136            {
            137                bb=0;
            138                aa=nowa+nowb;
            139                if (!flag[aa][bb])
            140                {
            141                    tail++;
            142                    q[tail].a=aa;
            143                    q[tail].b=bb;
            144                    q[tail].d=3;
            145                    q[tail].d1=2;
            146                    wh[tail]=head;
            147                    flag[aa][bb]=1;
            148                }

            149            }

            150        }

            151    }

            152    return -1;
            153}

            154void print(int k1)
            155{
            156    int i;
            157    i=k1;
            158    if (k1==-1)
            159    {
            160        printf("impossible\n");
            161        return;
            162    }

            163    num=0;
            164    while(i!=1)
            165    {
            166        num++;
            167        num0[num]=i;
            168        i=wh[i];
            169    }

            170    printf("%d\n",num);
            171    for(i=num; i>=1; i--)
            172        if (q[num0[i]].d==1)
            173        {
            174            printf("FILL(%d)\n",q[num0[i]].d1);
            175        }

            176        else if(q[num0[i]].d==2)
            177        {
            178            printf("DROP(%d)\n",q[num0[i]].d1);
            179        }

            180        else if(q[num0[i]].d==3)
            181        {
            182            if (q[num0[i]].d1==1)
            183            {
            184                printf("POUR(1,2)\n");
            185            }

            186            else if(q[num0[i]].d1==2)
            187            {
            188                printf("POUR(2,1)\n");
            189            }

            190        }

            191}

            192int main()
            193{
            194    scanf("%d%d%d",&a,&b,&c);
            195    memset(flag,0,sizeof(flag));
            196    if ((c>a)&&(c>b))
            197    {
            198        k=-1;
            199        print(k);
            200    }

            201    else
            202    {
            203        k=bfs();
            204        print(k);
            205    }

            206    return 0;
            207}

            208

            posted on 2012-03-20 19:47 jh818012 閱讀(1172) 評論(2)  編輯 收藏 引用

            評論

            # re: poj3414 2012-04-02 17:48 王私江

            200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。  回復  更多評論   

            # re: poj3414[未登錄] 2012-04-02 19:59 jh818012

            @王私江
            0ms  回復  更多評論   

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內容較長,點擊標題查看
            • --王私江
            久久人妻少妇嫩草AV无码蜜桃| 国产精品九九久久免费视频| 丁香久久婷婷国产午夜视频| 精品久久香蕉国产线看观看亚洲 | 国产精品永久久久久久久久久 | 久久99精品久久久久久动态图 | 人妻少妇精品久久| 亚洲精品午夜国产va久久| 欧美一级久久久久久久大| 久久久久久久久久久| 无码人妻精品一区二区三区久久| 99国产欧美久久久精品蜜芽 | 国产福利电影一区二区三区久久久久成人精品综合 | 模特私拍国产精品久久| 久久久无码人妻精品无码| 精品久久人人做人人爽综合| 久久久久无码精品国产app| 国产精品一区二区久久精品涩爱 | 国内精品伊人久久久久av一坑 | 国产毛片久久久久久国产毛片| 伊人色综合久久天天人守人婷 | 国产精品久久网| 一级做a爰片久久毛片毛片| 国产精品久久久久jk制服| 狠狠色丁香婷婷久久综合五月| 国产成人无码精品久久久久免费| 久久天堂AV综合合色蜜桃网| 久久婷婷五月综合国产尤物app| 1000部精品久久久久久久久| 久久久久国产一区二区| 99精品久久精品一区二区| 久久最新免费视频| 66精品综合久久久久久久| 精品人妻久久久久久888| 狠狠色狠狠色综合久久| 欧美精品九九99久久在观看| 丁香久久婷婷国产午夜视频| 91精品国产综合久久四虎久久无码一级| 精产国品久久一二三产区区别| 国产精品久久久久久五月尺| 久久久久久国产精品美女|