week2作業 bfs的簡單應用 倒水問題

2021-10-03 13:39:51 字數 2907 閱讀 4951

一道bfs的簡單應用(與迷宮題十分類似),用到的資料結構:

主要思路(bfs操作):

先將起始狀態push到佇列中並作標記(在集合中標記,不同於上一道迷宮題目,迷宮的位置可以窮盡但本題水的狀態過多,再用如二維陣列來記錄顯然不合適,遂使用集合),分別將佇列中元素依次pop出,然後分別檢查該點的「周圍」(這裡的周圍顯然不是普通意義上的周圍,而是經過給定的操作能夠到達的狀態;具體有六種:將a或b倒滿或倒空、a倒至b、b倒至a)的點是否可走(滿足:未到達過),直至佇列空

b - pour water

倒水問題 

fill a 表示倒滿a杯

empty a 表示倒空a杯

pour a b 表示把a的水倒到b杯並且把b杯倒滿或a倒空

input

輸入包含多組資料。

每組資料輸入 a, b, c

資料範圍 0 < a <= b 、c <= b <=1000 、a和b互質。

output

你的程式的輸出將由一系列的指令組成。

這些輸出行將導致任何乙個罐子正好包含c單位的水。

每組資料的最後一行輸出應該是「success」。

輸出行從第1列開始,不應該有空行或任何尾隨空格。

sample input

2 7 5

2 7 4

sample output

fill b

pour b a

success

fill a

pour a b

fill a

pour a b

success

notes

如果你的輸出與sample output不同,那沒關係。

對於某個"a b c"本題的答案是多解的,

不能通過標準的文字對比來判定你程式的正確與否。

所以本題由 spj(special judge)程式來判定你寫的**是否正確。

#include

#include

#include

#include

#include

using

namespace std;

typedef

struct tagstate

}state;

void

solution

(int a,

int b,

int c));

s.insert()

; state tmp=

; mp[tmp]=""

;while

(!q.

empty()

)}cout <<

"success"

<< endl;

break;}

//fill a

if(s.

find()

== s.

end())

);q.

push()

; tmp=

; mp[tmp]

= mp[now]

+"1";}

//fill b

if(s.

find()

== s.

end())

);q.

push()

; tmp=

; mp[tmp]

= mp[now]

+"2";}

//empty a

if(s.

find()

== s.

end())

);q.

push()

; tmp=

; mp[tmp]

= mp[now]

+"3";}

//empty b

if(s.

find()

== s.

end())

);q.

push()

; tmp=

; mp[tmp]

= mp[now]

+"4";}

//pour a to b

if(now.cup1 + now.cup2 > b)

)== s.

end())

);q.

push()

; tmp=

; mp[tmp]

= mp[now]

+"5";}

}else

)== s.

end())

);q.

push()

; tmp=

; mp[tmp]

= mp[now]

+"5";}

}//pour b to a

if(now.cup1 + now.cup2 > a)

)== s.

end())

);q.

push()

; tmp=

; mp[tmp]

= mp[now]

+"6";}

}else

)== s.

end())

);q.

push()

; tmp=

; mp[tmp]

= mp[now]

+"6";}

}}}int

main()

return0;

}

Week2 作業A 迷宮 bfs

輸入5 5矩陣,1表示牆,0表示可走。找出從左上角到右下角的最短路徑所經過的座標 輸入保證有唯一解 bool型二維陣列vis儲存輸入的01矩陣,1表示牆,0表示可走。map from儲存每乙個經過的座標的前驅結點座標。queue q儲存每一層擴充套件時的中心結點。為方便層層擴充套件時遍歷上下左右四個...

程式設計 Week2 作業

題意 東東有一張地圖,想通過地圖找到妹紙。地圖顯示,0表示可以走,1表示不可以走,左上角是入口,右下角是妹紙,這兩個位置保證為0。既然已經知道了地圖,那麼東東找到妹紙就不難了,請你編乙個程式,寫出東東找到妹紙的最短路線。input 輸入是乙個5 5的二維陣列,僅由0 1兩數字組成,表示法陣地圖。ou...

Week2 程式設計作業

j x theta y 2 j 1 2 m sum j 直接按代價函式的公式敲 需新增的 temp0 theta 1 alpha m sum x theta y x 1 temp1 theta 2 alpha m sum x theta y x 2 theta temp0 temp1 要讓theta...