Pots

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int a,b,c,s,e;
int v[101][101];
struct node
{
    int x,y,ans;
}q[100001],t,f;
void FILI(int xx,int yy,int zz,int i)
{
    if(i==0)
    {
        f.x=a;
        f.y=yy;
        if(v[f.x][f.y]==0)
        {
            f.ans=zz+1;
            q[e++]=f;
            v[f.x][f.y]=1;
        }
    }
    else if(i==1)
    {
        f.x=xx;
        f.y=b;
        if(v[f.x][f.y]==0)
        {
            f.ans=zz+1;
            q[e++]=f;
            v[f.x][f.y]=1;
        }
    }
}
void POUR(int xx,int yy,int zz,int i)
{
    if(i==2)
    {
        f.x=0;
        f.y=yy;
        if(v[f.x][f.y]==0)
        {
            f.ans=zz+1;
            q[e++]=f;
             v[f.x][f.y]=1;
        }
    }
    else if(i==3)
    {
        f.x=xx;
        f.y=0;
        if(v[f.x][f.y]==0)
        {
            f.ans=zz+1;
            q[e++]=f;
            v[f.x][f.y]=1;
        }
    }
}
void D(int xx,int yy,int zz,int i)
{
    if(i==4)
    {
        if(xx+yy>=a)
        {
             f.x=a;
             f.y=yy+xx-a;
        }
        else
        {
            f.x=xx+yy;
            f.y=0;
        }
        if(v[f.x][f.y]==0)
        {
            f.ans=zz+1;
            q[e++]=f;
              v[f.x][f.y]=1;
        }
    }
    else if(i==5)
    {
        if(xx+yy>=b)
        {
             f.y=b;
             f.x=xx+yy-b;
        }
        else
        {
            f.y=xx+yy;
            f.x=0;
        }
        if(v[f.x][f.y]==0)
        {
            f.ans=zz+1;
            q[e++]=f;
              v[f.x][f.y]=1;
        }
    }
}
void bfs()
{
    e=0;
    s=0;
    memset(v,0,sizeof(v));
    v[0][0]=1;
    t.x=0;
    t.y=0;
    t.ans=0;
    q[e++]=t;
    while(s<e)
    {
        t=q[s++];
        if(t.x==c||t.y==c)
        {
            printf("%d\n",t.ans);
            return ;
        }
        for(int i=0;i<6;i++)
        {
            if(i==0)
            FILI(t.x,t.y,t.ans,i);
            else if(i==1)
            FILI(t.x,t.y,t.ans,i);
            else if(i==2)
            POUR(t.x,t.y,t.ans,i);
            else if(i==3)
            POUR(t.x,t.y,t.ans,i);
            else if(i==4)
            D(t.x,t.y,t.ans,i);
            else if(i==5)
            D(t.x,t.y,t.ans,i);
        }
    }
    printf ("impossible\n");
}
int main()
{
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        bfs();
    }
    return 0;
}

Pots,布布扣,bubuko.com

时间: 2024-08-26 07:20:38

Pots的相关文章

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 广度优先搜索+记忆化

题意:有二个水壶,对水壶有三种操作,1)FILL(i),将i水壶的水填满,2)DROP(i),将水壶i中的水全部倒掉,3)POUR(i,j)将水壶i中的水倒到水壶j中,若水壶 j 满了,则 i 剩下的就不倒了,问进行多少步操作,并且怎么操作,输出操作的步骤,两个水壶中的水可以达到C这个水量.如果不可能则输出impossible.初始时两个水壶是空的,没有水. 简单题目,纯属练习...跟这个类似的------>>>点击打开链接 Description You are given two p

(简单) 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

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

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 

广搜+输出路径 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)

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.

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