1. 程式人生 > >POJ3414 Pots【倒水問題+BFS】

POJ3414 Pots【倒水問題+BFS】

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19641 Accepted: 8327 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 ≤ i ≤ 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 A, B, 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)

Source


題意簡述

給出兩個壺的容量A和B, 一個目標水量C,對A、B可以有3種操作,求最少經過幾步操作能夠在某個壺中得到目標水量C。輸入A、B和C,輸入最少運算元和操作過程。

  1. FILL(i)     fill the pot i (1 ≤ i ≤ 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 potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).

問題分析

  把A和B壺中水量作為狀態,初始狀態為<0,0>,每個操作都是狀態變化的過程。因為有2個壺,所以總共有6種操作。使用BFS搜尋來找到最少的操作步數。同時需要考慮操作的條件,以減少操作來加快程式執行速度。

程式說明

  搜尋過的狀態就不需要再搜尋了,用陣列notvist[][]來標記搜尋過的狀態。操作的前提條件已經寫在程式中。

AC的C++語言程式如下:

/* POJ3414 Pots */

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

const int MAXN = 100;

int a, b, c;
bool notvist[MAXN+1][MAXN+1];

struct node {
    int a, b, level;
    char path[MAXN+1];
    int plen;
};

string path[] = {
     "FILL(1)"
    ,"FILL(2)"
    ,"DROP(1)"
    ,"DROP(2)"
    ,"POUR(1,2)"
    ,"POUR(2,1)"
};

void output_result(int lvl, char p[], int n)
{
    cout << lvl << endl;
    for(int i=0; i<n; i++)
        cout << path[(int)p[i]] << endl;
}

void bfs()
{
    queue<node> q;

    memset(notvist, true, sizeof(notvist));

    node f;
    f.a = 0;
    f.b = 0;
    f.level = 0;
    memset(f.path, 0, sizeof(f.path));
    f.plen = 0;
    q.push(f);

    notvist[f.a][f.b] = false;

    while(!q.empty()) {
        f = q.front();
        q.pop();

        if(f.a == c || f.b == c) {
            output_result(f.level, f.path, f.plen);
            return;
        }

        node v;

        v = f;
        v.level++;
        v.plen++;
        // FILL(a)
        if(a - f.a > 0) {
            v.a = a;
            v.b = f.b;
            if(notvist[v.a][v.b]) {
                v.path[f.plen] = 0;
                q.push(v);
                notvist[v.a][v.b] = false;
            }
        }
        // FILL(b)
        if(b - f.b > 0) {
            v.a = f.a;
            v.b = b;
            if(notvist[v.a][v.b]) {
                v.path[f.plen] = 1;
                q.push(v);
                notvist[v.a][v.b] = false;
            }
        }
        // DROP(a)
        if(f.a) {
            v.a = 0;
            v.b = f.b;
            if(notvist[v.a][v.b]) {
                v.path[f.plen] = 2;
                q.push(v);
                notvist[v.a][v.b] = false;
            }
        }
        // DROP(b)
        if(f.b) {
            v.a = f.a;
            v.b = 0;
            if(notvist[v.a][v.b]) {
                v.path[f.plen] = 3;
                q.push(v);
                notvist[v.a][v.b] = false;
            }
        }
        // POUR(a,b)
        if(f.a && (f.b < b)) {
            if(f.a > (b - f.b)) {
                v.a = f.a -(b - f.b);
                v.b = b;
            } else {
                v.a = 0;
                v.b = f.b + f.a;
            }
            if(notvist[v.a][v.b]) {
                v.path[f.plen] = 4;
                q.push(v);
                notvist[v.a][v.b] = false;
            }
        }
        // POUR(b,a)
        if(f.b && (f.a < a)) {
            if(f.b > (a - f.a)) {
                v.a = a;
                v.b = f.b -(a - f.a);
            } else {
                v.a = f.a + f.b;
                v.b = 0;
            }
            if(notvist[v.a][v.b]) {
                v.path[f.plen] = 5;
                q.push(v);
                notvist[v.a][v.b] = false;
            }
        }
    }

    cout << "impossible" << endl;
}

int main()
{
    cin >> a >> b >> c;

    bfs();

    return 0;
}


相關推薦

POJ3414 Pots倒水問題+BFS

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

POJ_3414 Pots 複雜BFS

一、題面 You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)  &nbs

POJ3414 PotsBFS

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23177   Acce

poj 1915 Knight Moves 雙向bfs

ask blank time problem one sum for urn == Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22121 Acc

雙向bfs2017多校訓練十 HDU 6171 Admiral

isp hide splay 編號 sig push pac ans logs 【題意】 現在給出一個三角矩陣,如果0編號的在點(x,y)的話,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)這些點進行交換。 我們每一次只能對0點和其他點進行交

POJ 3278 Catch That Cow 簡單bfs

ems str empty next class pri eof color 奶牛 題目鏈接 題目大意: FJ要抓奶牛。 開始輸入N(FJ的位置)K(奶牛的位置)。 FJ有三種移動方法:1、向前走一步,耗時一分鐘。

UVA 11624 Fire!兩點BFS

ring help eth oca oid 全部 push_back 逃離 pre Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of

ACM/ICPC 2018亞洲區預選賽北京賽站網路賽 A Saving Tang Monk II分層bfs

時間限制:1000ms 單點時限:1000ms 記憶體限制:256MB 描述 《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese liter

UVa雙向BFS1601 The Morning after Halloween

UVa 1601 The Morning after Halloween 題目 題目大意 在一個W×HW\times HW×H的網格上有N(N≤3)N(N\le3)N(N≤3)個鬼,用小寫字母表示。要求將所有鬼移動到對應的大寫字母處。每步可以有多個鬼移動(均為

HDU 3085 Nightmare Ⅱ雙向bfs

Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3927    Accepted Sub

POJ3984 迷宮問題BFS

statistic \n algo 方向 lease 文章 gre mat dfs 版權聲明:本文為博主原創文章,未經博主同意不得轉載。 https://blog.csdn.net/u01

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

BFSPots

-c 記錄操作 ber cti bfs contents 0ms www med 整理自http://www.cnblogs.com/yzm10/p/7242590.html 總時間限制:1000ms內存限制:65536kB描述 You are given two p

bfs+模擬(poj3414 pots倒水問題)

PotsTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 21047Accepted: 8976Special JudgeDescriptionYou are given two pots, having the

BFSCODE[VS] 1226 倒水問題 (BFS+模擬)

這道題主要考察程式碼能力 模擬倒水的過程,結果因為碼力太弱,漏了兩種情況(x和y全倒入一個(x或y)不滿) 然後就沒有然後了 最近超喜歡壓程式碼怎麼破??? 程式碼如下: #i

搜索 HDU 3533 Escape BFS 預處理

cap 擁有 ace deque i++ const tdi code -m 要從0,0 點 跑到m,n點 路上會有k個堡壘發射子彈。有子彈的地方不能走,子彈打到別的堡壘就會消失,或者一直飛出邊界(人不能經過堡壘 能夠上下左右或者站著不動 每步都須要消耗能量 一共同

分類討論spfaBFSCodeforces Round #416 (Div. 2) D. Vladik and Favorite Game

邊界情況 code def bfs spa eof scan string amp 那個人第一步肯定要麽能向下走,要麽能向右走。於是一定可以判斷出上下是否對調,或者左右是否對調。 然後他往這個方向再走一走就能發現一定可以再往旁邊走,此時就可以判斷出另一個方向是否對調。 都判

BFS余數剪枝Multiple

wid def ever ems 如何 decimal following exists ota [poj1465]Multiple Time Limit: 1000MS Memory Limit: 32768K Total Submis

BFSThe New Villa

ron ads long long 清空 long new term state swa [poj1137] The New Villa Time Limit: 1000MS Memory Limit: 10000K Total Subm

poj 3278 Catch That Cow bfs

single void mem data tex port -s cst ssi Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 52335 Ac