hdu 1495 非常可樂

來源:互聯網
上載者:User

標籤:main   pop   還需   sizeof   div   pair   BMI   col   sele   

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24840    Accepted Submission(s): 9666


Problem Description大家一定覺的運動以後喝可樂是一件很愜意的事情,但是seeyou卻不這麼認為。因為每次當seeyou買了可樂以後,阿牛就要求和seeyou一起分享這一瓶可樂,而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子,它們的容量分別是N 毫升和M 毫升 可樂的體積為S (S<101)毫升 (正好裝滿一瓶) ,它們三個之間可以相互倒可樂 (都是沒有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聰明的ACMER你們說他們能平分嗎?如果能請輸出倒可樂的最少的次數,如果不能輸出"NO"。 

 

Input三個整數 : S 可樂的體積 , N 和 M是兩個杯子的容量,以"0 0 0"結束。 

 

Output如果能平分的話請輸出最少要倒的次數,否則輸出"NO"。 

 

Sample Input7 4 34 1 30 0 0 

 

Sample OutputNO3 

 

Authorseeyou 

 

Source“2006校園文化活動月”之“校慶杯”大學生程式設計競賽暨杭州電子科技大學第四屆大學生程式設計競賽  

 

RecommendLL   |   We have carefully selected several similar problems for you:  1175 1253 1072 1372 1180  一開始還想能不能通過思維找出最優答案,發現很難,於是就得遍曆各種情況,找出最小步數,類似二維圖的最短路,bfs遍曆各種情況,當發現出現了哪個杯子或瓶子中有S/2可樂就可以輸出答案了,但是要求是得到兩個S/2的答案,即有一個杯子必須是空的,所以返回的答案還要判斷是否有空的,如果不是還需要加一步來完成。這裡用一個3元素數組代替S,N,M,order是個順序數組,即order[i][0]往order[i][1]裡倒,order[i][2]保持不變。對於訪問過的情況,我們通過映射記錄,即check函數。代碼:
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;struct sta {    int ar[3];    sta(){}    sta(int a,int b,int c) {        ar[0] = a;        ar[1] = b;        ar[2] = c;    }};typedef pair<sta,int> pa;int ar[3];int order[6][3] = {0,1,2,0,2,1,1,0,2,1,2,0,2,0,1,2,1,0};bool vis[1000001];bool check(const sta &temp) {    int d = temp.ar[0] * 10000 + temp.ar[1] * 100 + temp.ar[2];    bool e = vis[d];    vis[d] = true;    return e;}sta change(const sta &temp,int a,int b,int c) {    sta temp1;    if(temp.ar[a] > ar[b] - temp.ar[b]) {        temp1.ar[a] = temp.ar[a] - ar[b] + temp.ar[b];        temp1.ar[b] = ar[b];    }    else {        temp1.ar[a] = 0;        temp1.ar[b] = temp.ar[b] + temp.ar[a];    }    temp1.ar[c] = temp.ar[c];    return temp1;}int bfs() {    if(ar[0] % 2) return 0;    queue<pa> q;    q.push(pa(sta(ar[0],0,0),0));    check(sta(ar[0],0,0));    while(!q.empty()) {        sta temp = q.front().first,temp1;        int times = q.front().second;        q.pop();        for(int i = 0;i < 6;i ++) {            temp1 = change(temp,order[i][0],order[i][1],order[i][2]);            if(temp1.ar[0] == ar[0] / 2 || temp1.ar[1] == ar[0] / 2 || temp1.ar[2] == ar[0] / 2) return times + 1 + (temp1.ar[0] && temp1.ar[1] && temp1.ar[2]);            if(!check(temp1)) q.push(pa(temp1,times + 1));        }    }    return 0;}int main() {    while(scanf("%d%d%d",&ar[0],&ar[1],&ar[2]) && ar[0] && ar[1] && ar[2]) {        memset(vis,false,sizeof(vis));        int ans = bfs();        if(ans)printf("%d\n",ans);        else printf("NO\n");    }}

 

hdu 1495 非常可樂

聯繫我們

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