1. 程式人生 > >poj 3414 Pots (bfs+路徑記錄)

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:

  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)

思路:一共有6種操作:把A中水倒掉,把A加滿,把B裡的水倒入A中;B和A類似。

罐子最大容積為100,設一個常量N=100, 開一個二維陣列記錄狀態變化的值。

1、從水龍頭往A里加水t,記錄-t,

2、從水龍頭往B里加水t,記錄-t-N,

3、從B裡面往A加水t,記錄t

4、從A裡面往B加水t,記錄N+t

5、把A裡水倒掉,記錄2*N+t,(A原有水t)

6、把B裡水倒掉,記錄3*N+t,(B原有水t)

#include<stdio.h>
#include<queue>
#include<map>
#include<string>
#include<string.h>
using namespace std;
#define N 105
const int inf=0x1f1f1f1f;
int a,b,c,flag;
int mark[N][N];
struct node
{
    int x,y,t;
    friend bool operator<(node a,node b)
    {
        return a.t>b.t;
    }
};
void prif(int x,int y)       //遞迴輸出路徑
{
    if(x==0&&y==0)
        return ;
    if(mark[x][y]>3*N)
    {
        prif(x,mark[x][y]-3*N);
        printf("DROP(2)\n");
    }
    else if(mark[x][y]>2*N)
    {
        prif(mark[x][y]-2*N,y);
        printf("DROP(1)\n");
    }
    else if(mark[x][y]>N)
    {
        int tmp=mark[x][y]-N;
        prif(x+tmp,y-tmp);
        printf("POUR(1,2)\n");
    }
    else if(mark[x][y]>0)
    {
        int tmp=mark[x][y];
        prif(x-tmp,y+tmp);
        printf("POUR(2,1)\n");
    }
    else if(mark[x][y]>-N)
    {
        int tmp=-mark[x][y];
        prif(x-tmp,y);
        printf("FILL(1)\n");
    }
    else if(mark[x][y]<-N)
    {
        int tmp=N+mark[x][y];
        prif(x,y+tmp);
        printf("FILL(2)\n");
    }
}
void bfs()
{
    priority_queue<node>q;
    node cur,next;
    mark[0][0]=inf;  //該狀態只能出現一次,賦值為inf防止干擾其他值
    mark[a][0]=-a;
    mark[0][b]=-b-N;
    cur.t=1;       
    cur.x=a;
    cur.y=0;
    q.push(cur);
    cur.x=0;
    cur.y=b;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        next.t=cur.t+1;
        if(cur.x==c||cur.y==c)
        {
            flag=1;
            printf("%d\n",cur.t);
            prif(cur.x,cur.y);
            return ;
        }
        if(cur.x<a)       //向A加水
        {
            int tmp=a-cur.x;
            next.y=cur.y;
            next.x=a;         //來自水龍頭的水
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=-tmp;
                q.push(next);
            }
            if(cur.y>0)      //來自B的水
            {
                int tmp=min(cur.y,a-cur.x);
                next.x=cur.x+tmp;
                next.y=cur.y-tmp;
                if(!mark[next.x][next.y])
                {
                    mark[next.x][next.y]=tmp;
                    q.push(next);
                }
            }
        }
        if(cur.y<b)        //向B加水
        {
            int tmp=b-cur.y;
            next.x=cur.x;
            next.y=b;      //來自水龍頭的水
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=-tmp-N;
                q.push(next);
            }
            if(cur.x>0)      //來自A的水
            {
                int tmp=min(cur.x,b-cur.y);
                next.y=cur.y+tmp;
                next.x=cur.x-tmp;
                if(!mark[next.x][next.y])
                {
                    mark[next.x][next.y]=tmp+N;
                    q.push(next);
                }
            }
        }
        if(cur.x>0)     //倒掉水
        {
            int tmp=cur.x;
            next.x=0;
            next.y=cur.y;
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=2*N+tmp;
                q.push(next);
            }
        }
        if(cur.y>0)
        {
            int tmp=cur.y;
            next.y=0;
            next.x=cur.x;
            if(!mark[next.x][next.y])
            {
                mark[next.x][next.y]=3*N+tmp;
                q.push(next);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d%d",&a,&b,&c)!=-1)
    {
        memset(mark,0,sizeof(mark));
        flag=0;
        bfs();
        if(!flag)
            printf("impossible\n");
    }
    return 0;
}


相關推薦

poj 3414 Pots bfs+路徑記錄

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special Judge Description You are given two

PotsBFS+路徑記錄

023:Pots 檢視 提交 統計 提問 總時間限制:  1000ms 記憶體限制:  65536kB 描述 You are given two pots, having the volume of A and B liters respectively

POJ 3414 PotsBFS記錄路徑

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10085 Accepted: 4245 Special Judge Description You are given two p

poj 3414 Potsbfs+輸出回溯路徑

給你兩個容器,分別能裝下A升水和B升水,並且可以進行以下操作 FILL(i) 將第i個容器從水龍頭裡裝滿(1 ≤ i ≤ 2); DROP(i) 將第i個容器抽乾 POUR(i,j) 將第i個容

POJ ~ 3414 ~ Pots BFS+列印路徑

題意:有兩個無刻度的容量分別為A,B升的杯子,通過一些操作使某一個杯子中有C升的水。 1. FILL(i) ,將i這個杯子中的水接滿 2. DROP(i),將i這個杯子中的水倒掉 3. POUR(i,j),將i這個杯子中的水倒入j這個杯子,能倒完就倒完,倒不完就留在杯子中。

POJ-3414 POTSBFS列印路徑

問題描述 You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i

POJ-3414-PotsBFS 模擬

H - Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3414 App

迷宮問題bfs+路徑記錄

思路:正常bfs搜尋,然後記錄每個點的父節點,最後找父節點,最後輸出路徑; 程式碼如下: #include <iostream> #include <string> #inc

poj 3414 Pots廣搜BFS+路徑輸出

contents imp 進行 ace main 數組 ems string oid 轉載請註明出處:http://blog.csdn.net/u012860063?viewmode=contents 題目鏈接:http://poj.org/problem?id=3414

POJ-3984___迷宮問題——BFS+路徑記錄

題目大意:     從(1,1)(1,1)(1,1)走到(5,5)(5,5)(5,5)求最短路徑並輸出 解題思路:     無 程式碼思路:     無 核心:用一道簡單題記錄一下模板 #include<bits/stdc++.h> using n

poj 3414 Pots 廣搜鏈式儲存

1.題意:有兩個罐子A,B,可以進行三種操作, FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;//把第i個罐子裝滿;DROP(i)      empty the pot i to the drain;//把第i

POJ 3414 Pots 記錄路徑的廣搜

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 記錄路徑的搜尋

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

Uva 816 Abbott的復仇三元組BFS + 路徑還原

tor init false ace names strlen truct pos int 題意: 有一個最多9*9個點的迷宮, 給定起點坐標(r0,c0)和終點坐標(rf,cf), 求出最短路徑並輸出。 分析: 因為多了朝向這個元素, 所以我們bfs的隊列元素就是一個三

POJ 3669 Meteor Shower BFS+預處理

tro ports hid 否則 class fin leaves style 影響 Description Bessie hears that an extraordinary meteor shower is coming; reports say that th

HDU 1104 Remainder(BFS路徑記錄+數論

每次 mem urn 我們 這樣的 ans to do con .cn Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su

poj3414 PotsBFS

stat col 操作 sizeof ots www 思路 修改 mem 題目鏈接 http://poj.org/problem?id=3414 題意 有兩個杯子,容量分別為A升,B升,可以向杯子裏倒滿水,將杯子裏的水倒空,將一個杯子裏的水倒到另一個杯子裏,求怎樣倒才

POJ 3984 迷宮問題【BFS/路徑記錄

++ debug r++ scanf out ace AS ctype mis 迷宮問題 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description

POJ-3414 Pots(兩個杯子倒水問題) 【BFS

題目傳送門 題目: 給你兩個杯子a,b,容量分別是A和B。可以執行以下操作: 1.FILL(i):將i倒滿水。 2.DROP(i):將i倒空水。 3.POUR(i,j): 將ipot的水倒到jpot上,直至要麼ipot為空,要麼jpot為滿。 求能否在一定步數的操作後,使得a,b

POJ】1465MultipleBFS+同餘剪枝

Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 8365   Accepted:&nb