1. 程式人生 > >POJ 3414 Pots 均分水(bfs)

POJ 3414 Pots 均分水(bfs)

題目連結:

題目大意:

有兩個水杯,一開始都沒有裝水,給出兩個水杯的容量,和要達到的目標容量
每個水被有三種操作:1.裝滿水,2.倒掉所有水,3,將水倒入另一個杯子中.

思路:

兩個杯子,六種操作,,求最短路,並列印路徑,6入口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; 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)

程式碼:

/*************************************************************************
        > File Name: poj_3414.cpp
      > Author: dulun
      > Mail: [email protected]
> Created Time: 2016年04月13日 星期三 12時59分54秒 ************************************************************************/
#include<iostream> #include<stdio.h> #include<cstring> #include<cstdlib> #include<queue> #include<algorithm> #define LL long long using namespace std; const int N = 186; struct Node { int a, b, step; char str[N][N]; }; bool v[N][N]; void bfs(int a, int b, int c) { memset(v, 0, sizeof(v)); Node t, s; queue<Node> q; t = (Node){0, 0, 0}; q.push(t); v[t.a][t.b] = true; while(!q.empty()) { t = q.front(); q.pop(); v[t.a][t.b] = true; if(t.a == c || t.b == c) { printf("%d\n", t.step); for(int i = 1; i <= t.step; i++) { printf("%s\n", t.str[i]); } return ; } if(t.a == 0) { s = t; s.a = a; s.step++; strcpy(s.str[s.step], "FILL(1)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } } else if(t.a <= a) { s = t; s.a = 0; s.step++; strcpy(s.str[s.step], "DROP(1)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } if(t.b < b) { s = t; if(t.a+t.b <= b) { s.a = 0; s.b = t.a+t.b; } else{ s.a = t.a - (b-t.b); s.b = b; } s.step++; strcpy(s.str[s.step], "POUR(1,2)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } } } if(t.b == 0) { s = t; s.b = b; s.step++; strcpy(s.str[s.step], "FILL(2)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } } else if(t.b <= b) { s = t; s.b = 0; s.step++; strcpy(s.str[s.step], "DROP(2)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } if(t.a < a) { if(t.a+t.b < a) { s = t; s.a = t.a+t.b; s.b = 0; } else{ s = t; s.a = a; s.b = t.b - (a-t.a); } s.step++; strcpy(s.str[s.step], "POUR(2,1)"); if(!v[s.a][s.b]) { v[s.a][s.b] = true; q.push(s); } } } } printf("impossible\n"); return; } int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); bfs(a, b, c); return 0; }

相關推薦

POJ 3414 Pots 均分bfs

題目連結: 題目大意: 有兩個水杯,一開始都沒有裝水,給出兩個水杯的容量,和要達到的目標容量 每個水被有三種操作:1.裝滿水,2.倒掉所有水,3,將水倒入另一個杯子中. 思路: 兩個杯子,六種操作,,求最短路,並列印路徑,6入口b

POJ】1979 Red and BlackBFS

Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 44023 Accepted: 23850 Description There is a

POJ 3414 BFS

class cout () amp esp cst LV pan span 這道題還是比較簡單的,就是寫起來有點麻煩,剛開始做的時候一直一位有什麽簡單方法,拖了好久。。。還是自己分析問題的能力不行啊 1 #include<iostream> 2 #i

poj3414 PotsBFS

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

POJ 3126BFS

code stream using \n == bfs push string ostream 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #in

題解報告:poj 3669 Meteor Showerbfs

hat oca sam output ace for family gre mov Description Bessie hears that an extraordinary meteor shower is coming; reports say that these

POJ-3984-迷宮問題BFS

Description 定義一個二維陣列: int maze[5][5] = {0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0,}; 它表示一個迷宮,其中的1表示牆壁,0表示可以

POJ】3984迷宮問題BFS

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35895   Accepted:&n

POJ】2243Knight MovesBFS

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15703   Accepted:&n

POJ 3278 Catch That CowBFS

剛看了挑戰程式設計競賽上的搜尋入門專題,做完書上走迷宮的問題正好來做這個不是很難(可能對大佬來說這都不叫題)的題,算是正式做的第二道廣搜題吧,對搜尋已經有了一定的理解,簡單來說就是在現在這個位置,下一步應該往哪邊走,把所有的方向都要列舉出來。 下面是這個題的程式碼: #include<

Find The Multiple POJ - 1426 BFS

題目大意 給定一個整數,尋找一個只有0,1構成的十進位制數使得這個數能夠整除這個整數 解法 直接bfs第一位放入1,之後每一位放入1或者0 程式碼 #include <iostream> #include <queue> using namespace std; int n;

POJ 1426 Find The Multiplebfs

Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains onl

捕牛記1503解題報告bfs

字節數 first 檢測 表示 所有結點 sca c++ fin ++ 解題思路:每到一個坐標點都有三種走法,每個點只走一次,直到第一次發現牛的坐標為止。用廣度優先搜索(Breadth First Search)(bfs) 代碼實現:定義一個標記結點狀態的數組、一個記錄

三個水杯BFS

post pos acm class .net 一行 ora sea ref 三個水杯 時間限制:1000 ms | 內存限制:65535 KB 難度:4 描寫敘述給出三個水杯。大小不一,而且僅僅有最大的水杯的水是裝滿的,其余兩

POJ 2485 Highways 最小生成樹 Kruskal

between all pac pair content cross cte reel sca Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia

POJ 3069 Saruman's Army 貪心

依次 至少 一個 ide mage != cnblogs style man 題目大意:直線上有N個點,點i的位置是Xi,從這N個點中選取若幹,給他們加上標記,對每一個點,其距離為R以內的區域內必須有被標記的點。求至少需要多少個點被標記。 題目思路:設最左邊的點:點p的

POJ 3128 Leonardo's Notebook 置換

gif telling freopen for each align lock 需要 text page Leonardo‘s Notebook Time Limit: 1000MS Memory Limit: 65536K Total Submission

BFSHDU 4784 Dinner Coming Soon

same %d mod eas offer pack iostream amp ide   Coach Pang loves his boyfriend Uncle Yang very much. Today is Uncle Yang’s birthday, Coach

CSU-1975 機器人搬重物BFS

技術分享 問題 不能 三種方式 rmi right warning break ont 1975: 機器人搬重物 Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 64 Solved: 10

層層遞進——寬度優先搜索BFS

一個數 學習 情況 art 打印 能夠 blank alt .cn 問題引入 我們接著上次“解救小哈”的問題繼續探索,不過這次是用寬度優先搜索(BFS)。 註:問題來源可以點擊這裏 http://www.cnblogs.com/Octoptus