快速切题 poj3414 Pots

Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10042   Accepted: 4221   Special Judge

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

应用时:10min实际用时:57min原因:六种操作都是不可逆转的.思路:时间非常充裕到不需要建反边,数据太小
#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;
}

  

时间: 2025-01-17 21:08:29

快速切题 poj3414 Pots的相关文章

快速切题 cf118A

这教导人们一定要看题,要看题,元音包含了‘y’,完毕,要看题啊 #include <cstring> #include <cstdio> #include <cctype> using namespace std; char buff[1001]; int main(){ scanf("%s",buff); for(int i=0;buff[i];i++){ buff[i]=tolower(buff[i]); if(buff[i]=='a'||buf

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.

快速切题 poj2488 A Knight&#39;s Journey

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31195   Accepted: 10668 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

快速切题 poj3026

感受到出题人深深的~恶意 这提醒人们以后...数字后面要用gets~不要getchar 此外..不要相信那个100? Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8524   Accepted: 2872 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant o

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

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

快速切题 acdream手速赛(6)A-C

Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description Sudoku is a popular single player game. The objective is to fill a 9x9 matrix with digits so that each colu