ZOJ 1005 Jugs

來源:互聯網
上載者:User

題意:兩個罐子倒水

題解:

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int a, b, n;bool vis[1010][1010], flag;int res[10100], step;void print(){    for(int i = 0; i < step; i++)    {        switch(res[i])        {        case 1: printf("fill A\n"); break;        case 2: printf("fill B\n"); break;        case 3: printf("empty A\n"); break;        case 4: printf("empty B\n"); break;        case 5: printf("pour A B\n"); break;        case 6: printf("pour B A\n"); break;        case 7: printf("success\n"); break;        }    }}void dfs(int x, int y){    if(flag) return;    if(x == n || y == n)    {        flag = true;        res[step++] = 7;        print();        return;    }    if(a > x && !vis[a][y] && !flag)    {        res[step++] = 1;        vis[a][y] = true;        dfs(a, y);        step--;    }    if(b > y && !vis[x][b] && !flag)    {        res[step++] = 2;        vis[x][b] = true;        dfs(x, b);        step--;    }    if(x != 0 && !vis[0][y] && !flag)    {        res[step++] = 3;        vis[0][y] = true;        dfs(0, y);        step--;    }    if(y != 0 && !vis[x][0] && !flag)    {        res[step++] = 4;        vis[x][0] = true;        dfs(x, 0);        step--;    }    int tmp = min(x, b - y);    if(!vis[x-tmp][y+tmp] && !flag)    {        res[step++] = 5;        vis[x-tmp][y+tmp] = true;        dfs(x - tmp, y + tmp);        step--;    }    tmp = min(a - x, y);    if(!vis[x+tmp][y-tmp] && !flag)    {        res[step++] = 6;        vis[x+tmp][y-tmp] = true;        dfs(x + tmp, y - tmp);        step--;    }}int main(){    while(scanf("%d%d%d",&a,&b,&n) != EOF)    {        flag = false;        memset(vis, 0, sizeof(vis));        step = 0;        vis[0][0] = true;        dfs(0, 0);    }    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.