POJ3414 Pots(BFS)

倒水问题。

题意:给两个杯子,容积分别为A和B。通过从水龙头接水(每次都接满),把杯里水全倒掉,把一个杯子里的水倒到另一个杯子里面三种操作使其中一个杯子里的水为C。

解题思路:6入口的BFS。难点在如何保存路径。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

bool vis[110][110];
struct Node
{
    int x,y;
    int step;
    int fa;
    int my;
    int flag;
} node[10010];
queue<Node>q;

void path(int u)
{
    if(node[u].fa==-1)
        return ;
    path(node[u].fa);
    if(node[u].flag==11)
        printf("FILL(1)\n");
    else if(node[u].flag==12)
        printf("FILL(2)\n");
    else if(node[u].flag==21)
        printf("DROP(1)\n");
    else if(node[u].flag==22)
        printf("DROP(2)\n");
    else if(node[u].flag==31)
        printf("POUR(1,2)\n");
    else if(node[u].flag==32)
        printf("POUR(2,1)\n");
}

int main()
{
    int a,b,c,cnt;
    //freopen("d:\\test.txt","r",stdin);
    scanf("%d%d%d",&a,&b,&c);
    memset(vis,false,sizeof(vis));
    vis[0][0]=true;
    cnt=0;
    node[cnt].x=0,node[cnt].y=0,node[cnt].step=0,node[cnt].fa=-1,node[cnt].my=0;
    cnt++;
    q.push(node[0]);
    Node t;
    while(!q.empty())
    {
        t=q.front();
        if(t.x==c ||t.y==c)
            break;
        q.pop();
        if(t.x<a && !vis[a][t.y])
        {
            vis[a][t.y]=true;
            node[cnt].x=a,node[cnt].y=t.y,node[cnt].step=t.step+1,node[cnt].my=cnt,node[cnt].fa=t.my,node[cnt].flag=11;
            q.push(node[cnt]);
            cnt++;
        }
        if(t.y<b && !vis[t.x][b])
        {
            vis[t.x][b]=true;
            node[cnt].x=t.x,node[cnt].y=b,node[cnt].step=t.step+1,node[cnt].my=cnt,node[cnt].fa=t.my,node[cnt].flag=12;
            q.push(node[cnt]);
            cnt++;
        }
        if(t.x>0 && !vis[0][t.y])
        {
            vis[0][t.y]=true;
            node[cnt].x=0,node[cnt].y=t.y,node[cnt].step=t.step+1,node[cnt].my=cnt,node[cnt].fa=t.my,node[cnt].flag=21;
            q.push(node[cnt]);
            cnt++;
        }
        if(t.y>0 &&!vis[t.x][0])
        {
            vis[t.x][0]=true;
            node[cnt].x=t.x,node[cnt].y=0,node[cnt].step=t.step+1,node[cnt].my=cnt,node[cnt].fa=t.my,node[cnt].flag=22;
            q.push(node[cnt]);
            cnt++;
        }
        if(t.x>0 && t.y<b)
        {
            if(t.x>=b-t.y && !vis[t.x-b+t.y][b])
            {
                vis[t.x-b+t.y][b]=true;
                node[cnt].x=t.x-b+t.y,node[cnt].y=b,node[cnt].step=t.step+1,node[cnt].my=cnt,node[cnt].fa=t.my,node[cnt].flag=31;
                q.push(node[cnt]);
                cnt++;
            }
            else if(t.x<b-t.y && !vis[0][t.y+t.x])
            {
                vis[0][t.y+t.x]=true;
                node[cnt].x=0,node[cnt].y=t.y+t.x,node[cnt].step=t.step+1,node[cnt].my=cnt,node[cnt].fa=t.my,node[cnt].flag=31;
                q.push(node[cnt]);
                cnt++;
            }
        }
        if(t.x<a && t.y>0)
        {
            if(t.y>=a-t.x && !vis[a][t.y-a+t.x])
            {
                vis[a][t.y-a+t.x]=true;
                node[cnt].x=a,node[cnt].y=t.y-a+t.x,node[cnt].step=t.step+1,node[cnt].my=cnt,node[cnt].fa=t.my,node[cnt].flag=32;
                q.push(node[cnt]);
                cnt++;
            }
            else if(t.y<a-t.x && !vis[t.x+t.y][0])
            {
                vis[t.x+t.y][0]=true;
                node[cnt].x=t.x+t.y,node[cnt].y=0,node[cnt].step=t.step+1,node[cnt].my=cnt,node[cnt].fa=t.my,node[cnt].flag=32;
                q.push(node[cnt]);
                cnt++;
            }
        }
    }
    if(q.empty())
        printf("impossible\n");
    else
    {
        printf("%d\n",t.step);
        path(t.my);
    }

    return 0;
}
时间: 2024-10-13 22:38:49

POJ3414 Pots(BFS)的相关文章

poj3414 Pots(BFS+路径输出)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj1606一样 :http://blog.csdn.net/u012860063/article/details/37772275 Description You are given two pots, having the volume of A and B liters respectively.

POJ 3414 Pots (BFS/DFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fi

poj3414 Pots (BFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

poj3414——Pots(BFS)

Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i) empty the pot i to the drain; POUR(i,j) pour from pot i to pot j

POJ-3414 Pots (BFS)

Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from

poj3414 Pots(BFS)

题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其中的一个杯子里的水恰为C升,输出最少步数和操作:如果不能倒到C升,输出“impossible”. 思路 这题与poj1606基本相同,在poj1606的基础上添加了输出最少步数,修改了操作的表示,以及不可能达到目标时输出impossible.将poj1606的代码略作修改即可. 代码 1 #incl

Pots BFS(著名倒水问题升级版)

Pots You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from pot i 

poj3414 Pots

Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from

Pots bfs

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from pot i to po