日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Pots——BFS

發(fā)布時(shí)間:2023/11/30 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pots——BFS 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【題目描述】

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,就是稍微復(fù)雜一點(diǎn),在做過非常可樂(也是一道BFS)后,這道題簡直一模一樣,就是稍微復(fù)雜一點(diǎn)點(diǎn),注意細(xì)節(jié)
還有就是題目要求輸出步驟,這就需要保存一下路徑,我用了一個(gè)vector(發(fā)現(xiàn)STL好好用)
【AC代碼】

#include<cstdio> #include<cstring> #include<queue> #include<climits> #include<vector>using namespace std;const int MAXN=105; int A,B,C; int check[MAXN][MAXN]; struct node1 {int x; int a; }tt; struct node {int a,b;vector<node1> path;int step; }t,p,ans;bool BFS() {queue<node> q;p.a=0; p.b=0; p.path.clear(); p.step=0;q.push(p);memset(check,-1,sizeof(check));check[0][0]=0;while(!q.empty()){p=q.front(); q.pop();if(p.a==C || p.b==C){ans=p;return true;}for(int i=1;i<=3;i++){if(i==1){for(int j=1;j<=2;j++){if(j==1 && p.a<A){t=p;t.a=A;if(check[t.a][t.b]!=-1) continue;tt.x=1; tt.a=1;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}else if(j==2 && p.b<B){t=p;t.b=B;if(check[t.a][t.b]!=-1) continue;tt.x=1; tt.a=2;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}}}else if(i==2){for(int j=1;j<=2;j++){if(j==1 && p.a>0){t=p;t.a=0;if(check[t.a][t.b]!=-1) continue;tt.x=2; tt.a=j;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}else if(j==2 &&p.b>0){t=p;t.b=0;if(check[t.a][t.b]!=-1) continue;tt.x=2; tt.a=j;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}}}else if(i==3){for(int j=1;j<=2;j++){if(j==1 && p.a>0 && p.b<B){t=p;if(p.a>=B-p.b){t.a=t.a+t.b-B;t.b=B;}else{t.b+=t.a;t.a=0;}if(check[t.a][t.b]!=-1) continue;tt.x=3; tt.a=1;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}else if(j==2 && p.b>0 && p.a<A){t=p;if(p.b>=A-p.a){t.b=t.a+t.b-A;t.a=A;}else{t.a+=t.b;t.b=0;}if(check[t.a][t.b]!=-1) continue;tt.x=3; tt.a=2;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}}}}}return false; }int main() {scanf("%d%d%d",&A,&B,&C);if(BFS()){printf("%d\n",ans.step);for(int i=0,j=ans.step;i<j;i++){if(ans.path[i].x==1){printf("FILL(%d)\n",ans.path[i].a);}else if(ans.path[i].x==2){printf("DROP(%d)\n",ans.path[i].a);}else if(ans.path[i].x==3){int tmp=(ans.path[i].a==1)?2:1;printf("POUR(%d,%d)\n",ans.path[i].a,tmp);}}}else{printf("impossible");}return 0; }

總結(jié)

以上是生活随笔為你收集整理的Pots——BFS的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。