1. 程式人生 > >POJ-3414 POTS(BFS列印路徑)

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

分析:
典型的BFS題,在沒有做BFS類別中的迷宮問題時,我是不會做這個題的,當那個題目會了,這個題也就沒什麼問題了,並且還需要計算最少次數,那就正常的BFS就可以了。
列印路徑關鍵是記錄父節點狀態。
一共6個operations,每個進行BFS就行。

程式碼如下;

#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring> using namespace std; const int maxn = 100+10; int visited[maxn][maxn]; int A, B, C; int enda, endb; int ok; struct State{ int litera, literb; int step; }begin; struct Par{ int x, y; int num; }par[maxn][maxn]; /*初始化,很關鍵*/ void Init() { ok = 0; memset(visited, 0, sizeof(visited)); memset(par, 0, sizeof(par)); visited[0][0] = 1; begin.litera = 0; begin.literb = 0; begin.step = 0; } void BFS() { queue<State>q; q.push(begin); while(!q.empty()) { State u = q.front(); q.pop(); if(u.litera == C || u.literb == C) { ok = 1; printf("%d\n",u.step); enda = u.litera; endb = u.literb; return; } //FILL(A) if(!visited[A][u.literb]) { visited[A][u.literb] = 1; State v; v.litera = A; v.literb = u.literb; v.step = u.step + 1; q.push(v); par[v.litera][v.literb].x = u.litera; par[v.litera][v.literb].y = u.literb; par[v.litera][v.literb].num = 1; } //FILL(B) if(!visited[u.litera][B]) { visited[u.litera][B] = 1; State v; v.litera = u.litera; v.literb = B; v.step = u.step + 1; q.push(v); par[v.litera][v.literb].x = u.litera; par[v.litera][v.literb].y = u.literb; par[v.litera][v.literb].num = 2; } //DROP(A) empty the pot A to the drain; if(!visited[0][u.literb]) { visited[0][u.literb] = 1; State v; v.litera = 0; v.literb = u.literb; v.step = u.step + 1; q.push(v); par[v.litera][v.literb].x = u.litera; par[v.litera][v.literb].y = u.literb; par[v.litera][v.literb].num = 3; } //DROP(B) empty the pot B to the drain; if(!visited[u.litera][0]) { visited[u.litera][0] = 1; State v; v.litera = u.litera; v.literb = 0; v.step = u.step + 1; q.push(v); par[v.litera][v.literb].x = u.litera; par[v.litera][v.literb].y = u.literb; par[v.litera][v.literb].num = 4; } //POUR(A,B) int da, db; if(u.litera > B - u.literb) { da = u.litera - B + u.literb; db = B; } else if(u.litera <= B - u.literb) { da = 0; db = u.literb + u.litera; } if(!visited[da][db]) { // printf("%d %d\n", da, db); visited[da][db] = 1; State v; v.litera = da; v.literb = db; v.step = u.step + 1; q.push(v); /*記錄父節點*/ par[v.litera][v.literb].x = u.litera; par[v.litera][v.literb].y = u.literb; par[v.litera][v.literb].num = 5; } //POUR(B,A) if(u.literb > A - u.litera) { db = u.literb - A + u.litera; da = A; } else if(u.literb <= A - u.litera) { db = 0; da = u.litera + u.literb; } if(!visited[da][db]) { // printf("%d %d\n", da, db); visited[da][db] = 1; State v; v.litera = da; v.literb = db; v.step = u.step + 1; q.push(v); par[v.litera][v.literb].x = u.litera; par[v.litera][v.literb].y = u.literb; par[v.litera][v.literb].num = 6; } } printf("impossible\n"); return; } /*DFS列印路徑*/ void DFS(int x, int y) { if(x == 0 && y == 0) return; DFS(par[x][y].x, par[x][y].y); if(par[x][y].num == 1) printf("FILL(1)\n"); else if(par[x][y].num == 2) printf("FILL(2)\n"); else if(par[x][y].num == 3) printf("DROP(1)\n"); else if(par[x][y].num == 4) printf("DROP(2)\n"); else if(par[x][y].num == 5) printf("POUR(1,2)\n"); else if(par[x][y].num == 6) printf("POUR(2,1)\n"); } int main() { scanf("%d%d%d", &A, &B, &C); Init(); BFS(); if(ok == 1) { DFS(enda, endb);//如果有解,則列印 } return 0; }