POJ 3414-Pots(BFS)

题目地址:POJ 3414

题意:给你a,b两个容器的容量,六种操作方法,问最少多少次可以使其中一个容器里的水达到体积c,如果不能的话则输出impossible。

思路:一共有6种操作方法,0:倒满a,1:倒满b,2:把a里的水倒向b(两种情况b倒满a有剩余或者a倒完),3:把b里的水倒向a(类似2的两种情况),4:把a倒空,5:把b倒空。然后用vis[i][j]记录当a的容量为i,b的容量为j时走了多少步,op[i][j][k]记录当a的容量为i,b的容量为j时用的第k步操作是谁。然后就是乱搞就好了。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef __int64  LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-7;
const int Maxn=110;
struct node {
    int x,y;
} f1,f2;
int vis[Maxn][Maxn];
int op[Maxn][Maxn][Maxn];
int a,b,c;
void Operator(int i)
{
    switch(i) {
    case 0:
        printf("FILL(1)\n");
        break;
    case 1:
        printf("FILL(2)\n");
        break;
    case 2:
        printf("POUR(1,2)\n");
        break;
    case 3:
        printf("POUR(2,1)\n");
        break;
    case 4:
        printf("DROP(1)\n");
        break;
    case 5:
        printf("DROP(2)\n");
        break;
    }
}
void bfs()
{
    int i,j;
    queue<node >q;
    memset(vis,-1,sizeof(vis));
    vis[0][0]=0;
    f1.x=0;
    f1.y=0;
    q.push(f1);
    while(!q.empty()) {
        f1=q.front();
        q.pop();
        if(f1.x==c||f1.y==c) {
            printf("%d\n",vis[f1.x][f1.y]);
            for(i=0; i<vis[f1.x][f1.y]; i++) {
                Operator(op[f1.x][f1.y][i]);
            }
            return ;

        }
        for(i=0; i<6; i++) {
            if(i==0) {
                f2.x=a;
                f2.y=f1.y;
            }
            if(i==1) {
                f2.x=f1.x;
                f2.y=b;
            }
            if(i==2) {
                f2.y = f1.x + f1.y;
                if(f2.y>=b) {
                    f2.x=f2.y-b;
                    f2.y=b;

                } else
                    f2.x=0;
            }
            if(i==3) {
                f2.x=f1.y+f1.x;
                if(f2.x>=a) {
                    f2.y=f2.x-a;
                    f2.x=a;

                } else
                    f2.y=0;
            }
            if(i==4) {
                f2.x=0;
                f2.y=f1.y;
            }
            if (i==5) {
                f2.x=f1.x;
                f2.y=0;
            }
            if(vis[f2.x][f2.y]==-1) {
                vis[f2.x][f2.y]=vis[f1.x][f1.y]+1;
                for(j=0; j<vis[f1.x][f1.y]; j++)
                    op[f2.x][f2.y][j]=op[f1.x][f1.y][j];
                op[f2.x][f2.y][vis[f1.x][f1.y]]=i;
                q.push(f2);
            }
        }
    }
    puts("impossible");
}
int main()
{
    while(~scanf("%d%d%d",&a,&b,&c)) {
        bfs();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 18:31:24

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

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 倒水)

题意  你有两个容积分别为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/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(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];//更新最短步数

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

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+回溯路径 正向输出】

题目地址: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