• 罐罐罐罐罐罐


    SZU Problem(A16):Three Jugs

    Judge Info

    • Memory Limit: 32768KB
    • Case Time Limit: 10000MS
    • Time Limit: 10000MS
    • Judger: Number Only Judger

    Description

    Frogs start exploring the kingdom when the first frog land. Recently, explore teams find a locked door. There is no key to open the door, but there is a small altar in front of the door, and the inscription on a nearby wall tells them that they need to fill up a water jug with exactly K gallons of water, and put it on the altar to open the door. The inscription also informs them that they have only one chance at trying to open the door.

    Explore teams have three water jugs of sizes A,B and C only, and limited water source. How can they open the door? The team leader first lists out the possible actions:

    1)Fill up one of the jugs with water;

    2)Empty one of the jugs ,pour from it back into the water source;

    3)Pour from one jug into other till either the former is empty or the latter jug is full.

    Task

    You are required to write a program that finds out the minimum amount of water required to open the door.

    Input

    The first line of input contains T(1 leq T leq 100), the number of test cases. There is one line only for each test case, it contains four positive integers, the first three integers specifying the sizes of the jugs that can be used, and the fourth integer represent the target that needs to be achieved. All integers are in the range [1, 100].

    Output

    For each test case, print a line contains the minimum amount of water required. If it is not achievable, output -1.

    Sample Input

    2
    4 9 13 10
    4 9 13 5

    Sample Output

    10
    9


    刚刚开始,没有思路,只有暴力解决,不用想,绝对超时啦~
    超级长的代码,还是要秀秀哈哈哈,虽然超时,但数据测试过,都是对的
    !!!
      1 #include <iostream>
      2 #include <queue>
      3 #include <cstring> // memset()
      4 #include <stdio.h>
      5 using namespace std;
      6 const int N = 105;
      7 // 所用到的数组
      8 int flag[N][N][N];
      9 priority_queue<int> result; // 存储结果的优先队列
     10 int queuea[N*N*N];
     11 int queueb[N*N*N];
     12 int queuec[N*N*N];
     13 int queued[N*N*N];
     14 int main()
     15 {
     16     int t, a, b, c, goal, head, p, tempa, tempb, tempc, tempd, i, big;
     17     cin >> t;
     18     while( t-- )
     19     {
     20         // 初始化
     21         head = p = 0;
     22         while( !result.empty() )
     23             result.pop();
     24         cin >> a >> b >> c >> goal;
     25         big = max(a, max( b, c ) );
     26         for( i=goal; i<=a+b+c; i++ )
     27         {
     28             memset( flag, 0, sizeof(flag) );
     29             queuea[p] = 0;
     30             queueb[p] = 0;
     31             queuec[p] = 0;
     32             queued[p++] = i;
     33 
     34             while( head < p )
     35             {
     36                 tempa = queuea[head];
     37                 tempb = queueb[head];
     38                 tempc = queuec[head];
     39                 tempd = queued[head];
     40 
     41                 if( tempa != 0 )
     42                 {
     43                     if( tempa >= b-tempb ) // a罐罐中的水将b罐罐倒满后还有剩余
     44                     {
     45                         tempa -= (b-tempb);
     46                         tempb = b;
     47                     }
     48                     else // a罐罐中的水全部都没有了
     49                     {
     50                         tempb += tempa;
     51                         tempa = 0;
     52                     }
     53                     if( tempa+tempb+tempc == goal )
     54                     {
     55                         result.push( tempa+tempb+tempc+tempd );
     56                         goto END;
     57                     }
     58                     else if( flag[tempa][tempb][tempc] == 0 )
     59                     {
     60                         flag[tempa][tempb][tempc] = 1;
     61                         queuea[p] = tempa;
     62                         queueb[p] = tempb;
     63                         queuec[p] = tempc;
     64                         queued[p++] = tempd;
     65                     }
     66                 }
     67 
     68                 tempa = queuea[head];
     69                 tempb = queueb[head];
     70                 tempc = queuec[head];
     71                 tempd = queued[head];
     72 
     73                 if( tempa != 0 )
     74                 {
     75                     if( tempa >= c-tempc ) // a罐罐中的水将c罐罐倒满后还有剩余
     76                     {
     77                         tempa -= (c-tempc);
     78                         tempc = c;
     79                     }
     80                     else // a罐罐中的水全部都没有了
     81                     {
     82                         tempc += tempa;
     83                         tempa = 0;
     84                     }
     85                     if( tempa+tempb+tempc == goal )
     86                     {
     87                         result.push( tempa+tempb+tempc+tempd );
     88                         goto END;
     89                     }
     90                     else if( flag[tempa][tempb][tempc] == 0 )
     91                     {
     92                         flag[tempa][tempb][tempc] = 1;
     93                         queuea[p] = tempa;
     94                         queueb[p] = tempb;
     95                         queuec[p] = tempc;
     96                         queued[p++] = tempd;
     97                     }
     98                 }
     99 
    100                 tempa = queuea[head];
    101                 tempb = queueb[head];
    102                 tempc = queuec[head];
    103                 tempd = queued[head];
    104                 if( tempa != 0 )
    105                 {
    106                     tempd += tempa;
    107                     tempa = 0;
    108 
    109                     if( tempa+tempb+tempc == goal )
    110                     {
    111                         result.push( tempa+tempb+tempc+tempd );
    112                         goto END;
    113                     }
    114                     else if( flag[tempa][tempb][tempc] == 0 )
    115                     {
    116                         flag[tempa][tempb][tempc] = 1;
    117                         queuea[p] = tempa;
    118                         queueb[p] = tempb;
    119                         queuec[p] = tempc;
    120                         queued[p++] = tempd;
    121                     }
    122                 }
    123 
    124                 tempa = queuea[head];
    125                 tempb = queueb[head];
    126                 tempc = queuec[head];
    127                 tempd = queued[head];
    128 
    129                 if( tempb != 0 )
    130                 {
    131                     if( tempb >= a-tempa ) // b罐罐中的水将a罐罐倒满后还有剩余
    132                     {
    133                         tempb -= (a-tempa);
    134                         tempa = a;
    135                     }
    136                     else // b罐罐中的水全部都没有了
    137                     {
    138                         tempa += tempb;
    139                         tempb = 0;
    140                     }
    141                     if( tempa+tempb+tempc == goal )
    142                     {
    143                         result.push( tempa+tempb+tempc+tempd );
    144                         goto END;
    145                     }
    146                     else if( flag[tempa][tempb][tempc] == 0 )
    147                     {
    148                         flag[tempa][tempb][tempc] = 1;
    149                         queuea[p] = tempa;
    150                         queueb[p] = tempb;
    151                         queuec[p] = tempc;
    152                         queued[p++] = tempd;
    153                     }
    154                 }
    155 
    156                 tempa = queuea[head];
    157                 tempb = queueb[head];
    158                 tempc = queuec[head];
    159                 tempd = queued[head];
    160 
    161                 if( tempb != 0 )
    162                 {
    163                     if( tempb >= c-tempc ) // b罐罐中的水将c罐罐倒满后还有剩余
    164                     {
    165                         tempb -= (c-tempc);
    166                         tempc = c;
    167                     }
    168                     else // b罐罐中的水全部都没有了
    169                     {
    170                         tempc += tempb;
    171                         tempb = 0;
    172                     }
    173                     if( tempa+tempb+tempc == goal )
    174                     {
    175                         result.push( tempa+tempb+tempc+tempd );
    176                         goto END;
    177                     }
    178                     else if( flag[tempa][tempb][tempc] == 0 )
    179                     {
    180                         flag[tempa][tempb][tempc] = 1;
    181                         queuea[p] = tempa;
    182                         queueb[p] = tempb;
    183                         queuec[p] = tempc;
    184                         queued[p++] = tempd;
    185                     }
    186                 }
    187 
    188                 tempa = queuea[head];
    189                 tempb = queueb[head];
    190                 tempc = queuec[head];
    191                 tempd = queued[head];
    192                 if( tempb != 0 )
    193                 {
    194                     tempd += tempb;
    195                     tempb = 0;
    196 
    197                     if( tempa+tempb+tempc == goal )
    198                     {
    199                         result.push( tempa+tempb+tempc+tempd );
    200                         goto END;
    201                     }
    202                     else if( flag[tempa][tempb][tempc] == 0 )
    203                     {
    204                         flag[tempa][tempb][tempc] = 1;
    205                         queuea[p] = tempa;
    206                         queueb[p] = tempb;
    207                         queuec[p] = tempc;
    208                         queued[p++] = tempd;
    209                     }
    210                 }
    211 
    212                 tempa = queuea[head];
    213                 tempb = queueb[head];
    214                 tempc = queuec[head];
    215                 tempd = queued[head];
    216 
    217                 if( tempc != 0 )
    218                 {
    219                     if( tempc >= a-tempa ) // c罐罐中的水将a罐罐倒满后还有剩余
    220                     {
    221                         tempc -= (a-tempa);
    222                         tempa = a;
    223                     }
    224                     else // c罐罐中的水全部都没有了
    225                     {
    226                         tempa += tempc;
    227                         tempc = 0;
    228                     }
    229                     if( tempa+tempb+tempc == goal )
    230                     {
    231                         result.push( tempa+tempb+tempc+tempd );
    232                         goto END;
    233                     }
    234                     else if( flag[tempa][tempb][tempc] == 0 )
    235                     {
    236                         flag[tempa][tempb][tempc] = 1;
    237                         queuea[p] = tempa;
    238                         queueb[p] = tempb;
    239                         queuec[p] = tempc;
    240                         queued[p++] = tempd;
    241                     }
    242                 }
    243 
    244                 tempa = queuea[head];
    245                 tempb = queueb[head];
    246                 tempc = queuec[head];
    247                 tempd = queued[head];
    248 
    249                 if( tempc != 0 )
    250                 {
    251                     if( tempc >= b-tempb ) // c罐罐中的水将b罐罐倒满后还有剩余
    252                     {
    253                         tempc -= (b-tempb);
    254                         tempb = b;
    255                     }
    256                     else // c罐罐中的水全部都没有了
    257                     {
    258                         tempb += tempc;
    259                         tempc = 0;
    260                     }
    261                     if( tempa+tempb+tempc == goal )
    262                     {
    263                         result.push( tempa+tempb+tempc+tempd );
    264                         goto END;
    265                     }
    266                     else if( flag[tempa][tempb][tempc] == 0 )
    267                     {
    268                         flag[tempa][tempb][tempc] = 1;
    269                         queuea[p] = tempa;
    270                         queueb[p] = tempb;
    271                         queuec[p] = tempc;
    272                         queued[p++] = tempd;
    273                     }
    274                 }
    275 
    276                 tempa = queuea[head];
    277                 tempb = queueb[head];
    278                 tempc = queuec[head];
    279                 tempd = queued[head];
    280                 if( tempc != 0 )
    281                 {
    282                     tempd += tempc;
    283                     tempc = 0;
    284 
    285                     if( tempa+tempb+tempc == goal )
    286                     {
    287                         result.push( tempa+tempb+tempc+tempd );
    288                         goto END;
    289                     }
    290                     else if( flag[tempa][tempb][tempc] == 0 )
    291                     {
    292                         flag[tempa][tempb][tempc] = 1;
    293                         queuea[p] = tempa;
    294                         queueb[p] = tempb;
    295                         queuec[p] = tempc;
    296                         queued[p++] = tempd;
    297                     }
    298                 }
    299 
    300                 tempa = queuea[head];
    301                 tempb = queueb[head];
    302                 tempc = queuec[head];
    303                 tempd = queued[head];
    304 
    305                 if( tempd != 0 && tempd >= a-tempa )
    306                 {
    307                     tempd -= (a-tempa);
    308                     tempa = a;
    309 
    310                     if( tempa+tempb+tempc == goal )
    311                     {
    312                         result.push( tempa+tempb+tempc+tempd );
    313                         goto END;
    314                     }
    315                     else if( flag[tempa][tempb][tempc] == 0 )
    316                     {
    317                         flag[tempa][tempb][tempc] = 1;
    318                         queuea[p] = tempa;
    319                         queueb[p] = tempb;
    320                         queuec[p] = tempc;
    321                         queued[p++] = tempd;
    322                     }
    323                 }
    324 
    325                 tempa = queuea[head];
    326                 tempb = queueb[head];
    327                 tempc = queuec[head];
    328                 tempd = queued[head];
    329 
    330                 if( tempd != 0 && tempd >= b-tempb )
    331                 {
    332                     tempd -= (b-tempb);
    333                     tempb = b;
    334                     if( tempa+tempb+tempc == goal )
    335                     {
    336                         result.push( tempa+tempb+tempc+tempd );
    337                         goto END;
    338                     }
    339                     else if( flag[tempa][tempb][tempc] == 0 )
    340                     {
    341                         flag[tempa][tempb][tempc] = 1;
    342                         queuea[p] = tempa;
    343                         queueb[p] = tempb;
    344                         queuec[p] = tempc;
    345                         queued[p++] = tempd;
    346                     }
    347                 }
    348 
    349                 tempa = queuea[head];
    350                 tempb = queueb[head];
    351                 tempc = queuec[head];
    352                 tempd = queued[head];
    353 
    354                 if( tempd != 0 && tempd >= c-tempc )
    355                 {
    356                     tempd -= (c-tempc);
    357                     tempc = c;
    358 
    359                     if( tempa+tempb+tempc == goal )
    360                     {
    361                         result.push( tempa+tempb+tempc+tempd );
    362                         goto END;
    363                     }
    364                     else if( flag[tempa][tempb][tempc] == 0 )
    365                     {
    366                         flag[tempa][tempb][tempc] = 1;
    367                         queuea[p] = tempa;
    368                         queueb[p] = tempb;
    369                         queuec[p] = tempc;
    370                         queued[p++] = tempd;
    371                     }
    372                 }
    373                 head++;
    374             }
    375         }
    376 END:    if( result.empty() == true )
    377             cout << -1 << endl;
    378         else
    379             cout << result.top() << endl;
    380     }
    381     return 0;
    382 }

    经过后来的苦苦苦苦思索,啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊阿啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊,我成功啦哈哈哈啊哈哈哈好开心~~~!!!!!

      1 #include <iostream>
      2 #include <queue>
      3 #include <cstring> // memset()
      4 #include <stdio.h>
      5 using namespace std;
      6 const int N = 105;
      7 // 所用到的数组
      8 struct node
      9 {
     10     bool operator < ( const node& n1 ) const
     11     {
     12         return priority > n1.priority; // 最小值优先
     13     }
     14     int a;
     15     int b;
     16     int c;
     17     int d;
     18     int priority;
     19 };
     20 int goal;
     21 int flag[N][N][N];
     22 priority_queue< int, vector<int>, greater<int> > result; // 存储结果的优先队列
     23 priority_queue< node > my_queue;
     24 void judge( int, int, int, int );
     25 int main()
     26 {
     27     int t, a, b, c, tempa, tempb, tempc, tempd;
     28     scanf( "%d", &t );
     29     while( t-- )
     30     {
     31         // 初始化
     32         while( !result.empty() )
     33             result.pop();
     34         while( !my_queue.empty() )
     35             my_queue.pop();
     36         scanf( "%d%d%d%d", &a, &b, &c, &goal );
     37 
     38         memset( flag, 0, sizeof(flag) );
     39 
     40             node begin;
     41             begin.a = 0;
     42             begin.b = 0;
     43             begin.c = 0;
     44             begin.d = goal;
     45             flag[0][0][0] = 1;
     46             begin.priority = goal;
     47             my_queue.push( begin );
     48             while( my_queue.empty() != true )
     49             {
     50                 node temp = my_queue.top();
     51                 tempa = temp.a;
     52                 tempb = temp.b;
     53                 tempc = temp.c;
     54                 tempd = temp.d;
     55 
     56 
     57                 if( tempa != 0 )
     58                 {
     59                     if( tempa >= b-tempb ) // a罐罐中的水将b罐罐倒满后还有剩余
     60                     {
     61                         tempa -= (b-tempb);
     62                         tempb = b;
     63                     }
     64                     else // a罐罐中的水全部都没有了
     65                     {
     66                         tempb += tempa;
     67                         tempa = 0;
     68                     }
     69                     judge( tempa, tempb, tempc, tempd );
     70                 }
     71 
     72                 tempa = temp.a;
     73                 tempb = temp.b;
     74                 tempc = temp.c;
     75                 tempd = temp.d;
     76 
     77                 if( tempa != 0 )
     78                 {
     79                     if( tempa >= c-tempc ) // a罐罐中的水将c罐罐倒满后还有剩余
     80                     {
     81                         tempa -= (c-tempc);
     82                         tempc = c;
     83                     }
     84                     else // a罐罐中的水全部都没有了
     85                     {
     86                         tempc += tempa;
     87                         tempa = 0;
     88                     }
     89                     judge( tempa, tempb, tempc, tempd );
     90                 }
     91 
     92                 tempa = temp.a;
     93                 tempb = temp.b;
     94                 tempc = temp.c;
     95                 tempd = temp.d;
     96                 if( tempa != 0 )
     97                 {
     98                     tempd += tempa;
     99                     tempa = 0;
    100 
    101                     judge( tempa, tempb, tempc, tempd );
    102                 }
    103 
    104                 tempa = temp.a;
    105                 tempb = temp.b;
    106                 tempc = temp.c;
    107                 tempd = temp.d;
    108 
    109                 if( tempb != 0 )
    110                 {
    111                     if( tempb >= a-tempa ) // b罐罐中的水将a罐罐倒满后还有剩余
    112                     {
    113                         tempb -= (a-tempa);
    114                         tempa = a;
    115                     }
    116                     else // b罐罐中的水全部都没有了
    117                     {
    118                         tempa += tempb;
    119                         tempb = 0;
    120                     }
    121                     judge( tempa, tempb, tempc, tempd );
    122                 }
    123 
    124                 tempa = temp.a;
    125                 tempb = temp.b;
    126                 tempc = temp.c;
    127                 tempd = temp.d;
    128 
    129                 if( tempb != 0 )
    130                 {
    131                     if( tempb >= c-tempc ) // b罐罐中的水将c罐罐倒满后还有剩余
    132                     {
    133                         tempb -= (c-tempc);
    134                         tempc = c;
    135                     }
    136                     else // b罐罐中的水全部都没有了
    137                     {
    138                         tempc += tempb;
    139                         tempb = 0;
    140                     }
    141                     judge( tempa, tempb, tempc, tempd );
    142                 }
    143 
    144                 tempa = temp.a;
    145                 tempb = temp.b;
    146                 tempc = temp.c;
    147                 tempd = temp.d;
    148                 if( tempb != 0 )
    149                 {
    150                     tempd += tempb;
    151                     tempb = 0;
    152 
    153                     judge( tempa, tempb, tempc, tempd );
    154                 }
    155 
    156                 tempa = temp.a;
    157                 tempb = temp.b;
    158                 tempc = temp.c;
    159                 tempd = temp.d;
    160 
    161                 if( tempc != 0 )
    162                 {
    163                     if( tempc >= a-tempa ) // c罐罐中的水将a罐罐倒满后还有剩余
    164                     {
    165                         tempc -= (a-tempa);
    166                         tempa = a;
    167                     }
    168                     else // c罐罐中的水全部都没有了
    169                     {
    170                         tempa += tempc;
    171                         tempc = 0;
    172                     }
    173                     judge( tempa, tempb, tempc, tempd );
    174                 }
    175 
    176                 tempa = temp.a;
    177                 tempb = temp.b;
    178                 tempc = temp.c;
    179                 tempd = temp.d;
    180 
    181                 if( tempc != 0 )
    182                 {
    183                     if( tempc >= b-tempb ) // c罐罐中的水将b罐罐倒满后还有剩余
    184                     {
    185                         tempc -= (b-tempb);
    186                         tempb = b;
    187                     }
    188                     else // c罐罐中的水全部都没有了
    189                     {
    190                         tempb += tempc;
    191                         tempc = 0;
    192                     }
    193                     judge( tempa, tempb, tempc, tempd );
    194                 }
    195 
    196                 tempa = temp.a;
    197                 tempb = temp.b;
    198                 tempc = temp.c;
    199                 tempd = temp.d;
    200                 if( tempc != 0 )
    201                 {
    202                     tempd += tempc;
    203                     tempc = 0;
    204 
    205                     judge( tempa, tempb, tempc, tempd );
    206                 }
    207 
    208                 tempa = temp.a;
    209                 tempb = temp.b;
    210                 tempc = temp.c;
    211                 tempd = temp.d;
    212                 
    213                     if( tempd >= a-tempa )
    214                     {
    215                         tempd -= (a-tempa);
    216                         tempa = a;
    217                     }
    218                     else if( a+tempb+tempc <= a+b+c )
    219                     {
    220                         tempd = 0;
    221                         tempa = a;
    222                     }
    223 
    224                     judge( tempa, tempb, tempc, tempd );
    225                
    226                 tempa = temp.a;
    227                 tempb = temp.b;
    228                 tempc = temp.c;
    229                 tempd = temp.d;
    230 
    231                     if( tempd >= b-tempb )
    232                     {
    233                         tempd -= (b-tempb);
    234                         tempb = b;
    235                     }
    236                     else if( tempa+b+tempc <= a+b+c )
    237                     {
    238                         tempd = 0;
    239                         tempb = b;
    240                     }
    241                     judge( tempa, tempb, tempc, tempd );
    242 
    243                 tempa = temp.a;
    244                 tempb = temp.b;
    245                 tempc = temp.c;
    246                 tempd = temp.d;
    247 
    248         
    249                     if( tempd >= c-tempc )
    250                     {
    251                         tempd -= (c-tempc);
    252                         tempc = c;
    253                     }
    254                     else if( tempa+tempb+c <= a+b+c )
    255                     {
    256                         tempd = 0;
    257                         tempc = c;
    258                     }
    259                     judge( tempa, tempb, tempc, tempd );
    260 
    261 
    262                 if( result.empty() == false )
    263                 {
    264                     temp = my_queue.top();
    265                     int a = result.top();
    266                     if( a <= temp.priority )
    267                         break;
    268                     else
    269                         my_queue.pop();
    270                 }
    271                 else
    272                     my_queue.pop();
    273             }
    274             if( result.empty() == true )
    275                 cout << -1 << endl;
    276             else
    277                 cout << result.top() << endl;
    278     }
    279     return 0;
    280 }
    281 void judge( int tempa, int tempb, int tempc, int tempd )
    282 {
    283     node temp;
    284     if( tempa+tempb+tempc == goal )
    285         result.push( tempa+tempb+tempc+tempd );
    286     else if( flag[tempa][tempb][tempc] == 0 )
    287     {
    288         flag[tempa][tempb][tempc] = 1;
    289         temp.a = tempa;
    290         temp.b = tempb;
    291         temp.c = tempc;
    292         temp.d = tempd;
    293         temp.priority = tempa+tempb+tempc+tempd;
    294         my_queue.push( temp );
    295     }
    296 }
    最后,睡前一AC,心情倍儿棒,大家晚安吧~
  • 相关阅读:
    HTML实现“摇一摇”效果,比较好的两篇文章;
    mongodb查询关于大于小于的用法;
    thenjs的应用
    js原生forEach、map与jquery的each、$.each的区别
    nodejs的url模块中的resolve()的用法总结
    2021.1.22 刷题(环形链表)
    2021.1.21 刷题(定义链表)
    2021.1.21 刷题(移除链表元素)
    2021.1.20 刷题(螺旋矩阵)
    滑动窗口-长度最小的子数组
  • 原文地址:https://www.cnblogs.com/zhongshuxin/p/3284005.html
Copyright © 2020-2023  润新知