您的位置:首页 > 其它

POJ -3414-Pots

2015-08-19 14:34 330 查看
题意:

给你,两个容量分别为a,b的杯子,让你看需要几步可以得到c的水,不能的话,impossible,可以的话,输出步数,和路径

思路:

BFS,暴搜

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;
int a, b, c;
struct node
{
    int step;
    int k;
    int n, m;
    int qb;
} ls[1010],now, next;
int vis[101][101];
int ki[1010];
void bfs()
{
    memset(vis,0,sizeof(vis));
    queue<node> que;
    now.step = 0;
    now.k = 0;
    now.n = 0;
    now.m = 0;
    que.push(now);
    vis[0][0] = 1;
    int i;
    int ti = 0;
    while(!que.empty())
    {
        now = que.front();
        que.pop();
        if(now.n == c || now.m == c)
        {
            printf("%d\n",now.step);
            int ko = now.step;
            i = 1;
            int c = now.qb;
            ki[0] = now.k;
            while(1)
            {
                ki[i++] = ls[c].k;
                if(i > ko)
                    break;
                c = ls[c].qb;
            }
            for(i = ko-1; i >= 0; i--)
            {
                switch(ki[i])
                {
                    case 5: printf("FILL(1)\n");break;
                    case 6: printf("FILL(2)\n");break;
                    case 3: printf("POUR(1,2)\n");break;
                    case 4: printf("POUR(2,1)\n");break;
                    case 1: printf("DROP(1)\n");break;
                    case 2: printf("DROP(2)\n");break;
                }
            }
            return ;
        }
        ls[++ti] = now;
        for(i = 1; i <= 6; i++)
        {
            if(i == 1)
            {
                next.n = 0;
                next.m = now.m;
                next.step = now.step+1;
                next.k = i;
                if(!vis[next.n][next.m])
                {
                    vis[next.n][next.m] = 1;
                    next.qb = ti;
                    que.push(next);
                }
            }
            if(i == 2)
            {
                next.n = now.n;
                next.m = 0;
                next.step = now.step+1;
                next.k = i;
                if(!vis[next.n][next.m])
                {
                    vis[next.n][next.m] = 1;
                    next.qb = ti;
                    que.push(next);
                }
            }
            if(i == 3)
            {
                if(now.n > b-now.m)
                {
                    next.n = now.n-(b-now.m);
                    next.m = b;
                }
                else
                {
                    next.n = 0;
                    next.m = now.n+now.m;
                }
                next.k = i;
                next.step = now.step+1;
                if(!vis[next.n][next.m])
                {
                    vis[next.n][next.m] = 1;
                    next.qb = ti;
                    que.push(next);
                }
            }
            if(i == 4)
            {
                if(now.m > a-now.n)
                {
                    next.m = now.m-(a-now.n);
                    next.n = a;
                }
                else
                {
                    next.m = 0;
                    next.n = now.m+now.n;
                }
                next.k = i;
                next.step = now.step+1;
                if(!vis[next.n][next.m])
                {
                    vis[next.n][next.m] = 1;
                    next.qb = ti;
                    que.push(next);
                }
            }
            if(i == 5)
            {
                next.n = a;
                next.m = now.m;
                next.step = now.step+1;
                next.k = i;
                if(!vis[next.n][next.m])
                {
                    vis[next.n][next.m] = 1;
                    next.qb = ti;
                    que.push(next);
                }
            }

            if(i == 6)
            {
                next.n = now.n;
                next.m = b;
                next.step = now.step+1;
                next.k = i;
                if(!vis[next.n][next.m])
                {
                    vis[next.n][next.m] = 1;
                    next.qb = ti;
                    que.push(next);
                }

            }
        }
    }
    printf("impossible\n");
}

int main()
{
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        bfs();
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: