POJ 3414 Pots(bfs打印路径)

题意:给定两个空桶的容量分别为A、B,经过6种操作使获得C升水,求最少操作数;

思路:广搜。最少操作数很简单,练习一下打印路径;打印最短路劲就是每次记录当前状态和上一步,找到终点后查找路径。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int A,B,C;
int shortest[150][150];//更新最短步数
int quan[510][510];//记录当前操作
int vis[150][150];//标记
int route[500010],num;//最短路径上的操作,及最少操作数
struct node
{
    int x,y;
    node(){}
    node(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
}q[500010];
node pre[500][500];//上一个操作
int bfs()
{
    int b=0,e=0;
    q[e++]=node(0,0);
    vis[0][0]=1;
    shortest[0][0]=1;
    while(b<e)
    {
        node now=q[b++];
        for(int i=0;i<6;i++)
        {
            int xx,yy;
            if(i==0)//装满1
            {
                xx=A;yy=now.y;
                if(vis[xx][yy]==1) continue;
                vis[xx][yy]=1;
                shortest[xx][yy]=shortest[now.x][now.y]+1;
                q[e++]=node(xx,yy);
                quan[xx][yy]=i;//记录当前操作
                pre[xx][yy]=now;//记录上一步
            }
            if(i==1)//装满2
            {
                yy=B;xx=now.x;
                if(vis[xx][yy]==1) continue;
                vis[xx][yy]=1;
                shortest[xx][yy]=shortest[now.x][now.y]+1;
                q[e++]=node(xx,yy);
                quan[xx][yy]=i;
                pre[xx][yy]=now;
            }
            if(i==2)//清空1
            {
                xx=0;yy=now.y;
                if(vis[xx][yy]==1) continue;
                vis[xx][yy]=1;
                shortest[xx][yy]=shortest[now.x][now.y]+1;
                q[e++]=node(xx,yy);
                quan[xx][yy]=i;
                pre[xx][yy]=now;
            }
            if(i==3)//清空2
            {
                yy=0;xx=now.x;
                if(vis[xx][yy]==1) continue;
                vis[xx][yy]=1;
                shortest[xx][yy]=shortest[now.x][now.y]+1;
                q[e++]=node(xx,yy);
                quan[xx][yy]=i;
                pre[xx][yy]=now;
            }
            if(i==4)//1倒入2
            {
                if(now.x>=B-now.y)
                {
                    yy=B;xx=now.x-(B-now.y);
                }
                else
                {
                    yy=now.y+now.x;xx=0;
                }
                if(vis[xx][yy]==1) continue;
                vis[xx][yy]=1;
                shortest[xx][yy]=shortest[now.x][now.y]+1;
                q[e++]=node(xx,yy);
                quan[xx][yy]=i;
                pre[xx][yy]=now;
            }
            if(i==5)//2倒入1
            {
                if(now.y>=A-now.x)
                {
                    xx=A;yy=now.y-(A-now.x);
                }
                else
                {
                    xx=now.x+now.y;yy=0;
                }
                if(vis[xx][yy]==1) continue;
                vis[xx][yy]=1;
                shortest[xx][yy]=shortest[now.x][now.y]+1;
                q[e++]=node(xx,yy);
                quan[xx][yy]=i;
                pre[xx][yy]=now;
            }
            if(xx==C||yy==C)
            {
                int a,b;
                while(xx!=0||yy!=0)//查找路径
                {
                    route[num++]=quan[xx][yy];
                    a=pre[xx][yy].x;
                    b=pre[xx][yy].y;
                    xx=a;yy=b;
                }
                return shortest[now.x][now.y];
            }
        }
    }
    return -1;
}
int main()
{
    int i,j,k,cnt;
    while(scanf("%d%d%d",&A,&B,&C)!=EOF)
    {
        memset(pre,0,sizeof(pre));
        memset(quan,0,sizeof(quan));
        memset(vis,0,sizeof(vis));
        memset(route,0,sizeof(route));
        memset(shortest,0,sizeof(shortest));
        num=0;temp=INF;
        cnt=bfs();
        if(cnt==-1) printf("impossible\n");
        else
        {
           printf("%d\n",cnt);
           for(i=num-1;i>=0;i--)
           {
               if(route[i]==0)
               {
                   printf("FILL(1)\n");
               }
               else if(route[i]==1)
               {
                   printf("FILL(2)\n");
               }
               else if(route[i]==2)
               {
                   printf("DROP(1)\n");
               }
               else if(route[i]==3)
               {
                   printf("DROP(2)\n");
               }
               else if(route[i]==4)
               {
                   printf("POUR(1,2)\n");
               }
               else if(route[i]==5)
               {
                   printf("POUR(2,1)\n");
               }
           }
        }
    }
    return 0;
}
时间: 2024-10-12 21:40:30

POJ 3414 Pots(bfs打印路径)的相关文章

POJ 3414 Pots (BFS + 记录路径)

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10300 Accepted: 4362 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)        fill th

POJ 3414 Pots(BFS 倒水)

题意  你有两个容积分别为a,b杯子  你每次可以将某个杯子中的水倒满或者倒掉或者倒到另一个杯子  问能否通过这两个杯子量出c容量的水 和上一个倒可乐问题类似  只是这个操作更多了点  将两个杯子中各含有的水作为状态  每出队列一个状态  将所有可能到达的状态入队  直到有一个杯子里面水的体积为c   打印路径直接递归就行了 #include <map> #include <cstdio> #include <cstring> using namespace std;

poj 3414 pots (bfs+路径记录)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 4955   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

poj 3414 Pots(BFS)(简单题)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11551   Accepted: 4900   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

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

广搜+输出路径 POJ 3414 Pots

POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   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)

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

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

poj 3414 Pots【bfs+回溯路径 正向输出】

题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12080   Accepted: 5104   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operation