日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SDUT--Pots(二维BFS)

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

Pots

Time Limit: 1000ms?? Memory limit: 65536K??有疑問?點(diǎn)這里^_^

題目描寫敘述

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.

輸入

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

輸出

The first line of the output must contain the length of the sequence of operations K. ?If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

演示樣例輸入

3 5 4

演示樣例輸出

6

提示

FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
簡單搜索題,對于兩個(gè)空瓶子,容積分別為A B 有6種操作 把A(或B)清空,把A(或B)裝滿,把A倒入B,把B倒入A 。相應(yīng)這6種操作,有6種狀態(tài)。典型的bfs搜索。不多了,僅僅是這題明明說的是單組輸入結(jié)果答案卻要多組輸入才對。白白貢獻(xiàn)5個(gè)WA。 #include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include <queue> using namespace std; int m,n,c; typedef struct node {int v1,v2,op; }; bool vis[999][999]; void bfs() {node t={0,0,0};queue <node> Q;Q.push(t);vis[0][0]=1;while(!Q.empty()){node f=Q.front();Q.pop();if(f.v1==c||f.v2==c){cout<<f.op<<endl;return ;}if(f.v1!=m){t.v1=m;t.op=f.op+1;t.v2=f.v2;if(!vis[t.v1][t.v2]){vis[t.v1][t.v2]=1;Q.push(t);}}if(f.v2!=n){t.v2=n;t.op=f.op+1;t.v1=f.v1;if(!vis[t.v1][t.v2]){vis[t.v1][t.v2]=1;Q.push(t);}}if(f.v1!=0){t.v1=0;t.v2=f.v2;t.op=f.op+1;if(!vis[t.v1][t.v2]){vis[t.v1][t.v2]=1;Q.push(t);}}if(f.v2!=0){t.v2=0;t.v1=f.v1;t.op=f.op+1;if(!vis[t.v1][t.v2]){vis[t.v1][t.v2]=1;Q.push(t);}}if(f.v2!=0&&f.v1!=m){t.v2=f.v2-(m-f.v1);if(t.v2<0) t.v2=0;t.v1=f.v1+f.v2; if(t.v1>m) t.v1=m;t.op=f.op+1;if(!vis[t.v1][t.v2]){vis[t.v1][t.v2]=1;Q.push(t);}}if(f.v1!=0&&f.v2!=n){t.v1=f.v1-(n-f.v2);if(t.v1<0) t.v1=0;t.v2=f.v2+f.v1; if(t.v2>n) t.v2=n;t.op=f.op+1;if(!vis[t.v1][t.v2]){vis[t.v1][t.v2]=1;Q.push(t);}}}puts("impossible"); } int main() {while(cin>>m>>n>>c){memset(vis,0,sizeof(vis));bfs();}return 0; }

轉(zhuǎn)載于:https://www.cnblogs.com/jzssuanfa/p/7026035.html

總結(jié)

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

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