POJ 3414 Pots 暴力,bfs 难度:1

http://poj.org/problem?id=3414

记录瓶子状态,广度优先搜索即可

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=101;
int n,m;
typedef unsigned long long ull;
int A,B,C;
int vis[maxn][maxn];
int ans[maxn][maxn][maxn*maxn];
struct node{
    int a,b;
    node (int  ta,int tb):a(ta),b(tb){}
};
void printop(int op){
    switch(op){
    case 0:
        puts("FILL(1)");
        break;
    case 1:
        puts("FILL(2)");
        break;
    case 2:
        puts("POUR(1,2)");
        break;
    case 3:
        puts("POUR(2,1)");
        break;
    case 4:
        puts("DROP(1)");
        break;
    case 5:
        puts("DROP(2)");
        break;
    }
}
void op(int &a,int &b,int op){
    switch(op){
    case 0:
        a=A;
        break;
    case 1:
        b=B;
        break;
    case 2:
        if(b+a<=B){
            b+=a;
            a=0;
        }
        else {
            a-=B-b;
            b=B;
        }
        break;
    case 3:
        if(b+a<=A){
            a+=b;
            b=0;
        }
        else {
            b-=A-a;
            a=A;
        }
        break;
    case 4:
        a=0;
        break;
    case 5:
        b=0;
        break;
    }
}
void bfs(){
    queue <node> que;
    que.push(node(0,0));
    vis[0][0]=0;
    while(!que.empty()){
        node tp=que.front();que.pop();
        int ta=tp.a;
        int tb=tp.b;
        if(tp.a==C||tp.b==C){
            printf("%d\n",vis[tp.a][tp.b]);
            for(int i=0;i<vis[ta][tb];i++){
                int op=ans[ta][tb][i];
                printop(op);
            }
            return ;
        }
        for(int i=0;i<6;i++){
            int ta=tp.a;
            int tb=tp.b;
            op(ta,tb,i);
            if(vis[ta][tb]==-1){
                vis[ta][tb]=vis[tp.a][tp.b]+1;
                for(int j=0;j<vis[tp.a][tp.b];j++){
                    ans[ta][tb][j]=ans[tp.a][tp.b][j];
                }
                ans[ta][tb][vis[tp.a][tp.b]]=i;
                que.push(node(ta,tb));
            }
        }
    }
    puts("impossible");
}
int main(){
    scanf("%d%d%d",&A,&B,&C);
        memset(vis,-1,sizeof(vis));
        bfs();
    return 0;
}

  

时间: 2024-12-13 19:04:50

POJ 3414 Pots 暴力,bfs 难度:1的相关文章

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【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

POJ - 3414 Pots (BFS+路径记录)

题目链接:http://poj.org/problem?id=3414 题意:两个空杯子倒水,使得任意一个杯子中的水量达到题目要求,有6种操作:装满a,装满b,倒掉a,倒掉b,a->b,b->a. 题解:模拟一下操作,就好了. 1 #include <queue> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7

(简单) 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(BFS+递归打印)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10255   Accepted: 4333   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: 11705   Accepted: 4956   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: 10071   Accepted: 4237   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 倒水问题)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int vis[120][120]; int vl,vr; int ok; int key; struct node { int l,r; int fa; int op; }; node str[100000]; int ans[100000]; void bfs() {

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