10067: product
DESCRIPTION
10067: Product. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10067: Product 解題者: 雷皓博 解題日期: 20 14 年5月 5 日 題意: 給你初始值(ex 8056)和目地數字(ex 6508),並給你一些不可以到的數字,求到達目地數字的最少的步驟,無法到達的話輸出-1. (ex 8056 -> 8 9 56 算一個步驟 ) (ex 8056 -> 8 1 56 算一個步驟 ). 1. 題意範例: INPUT 1->幾組測資 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: 10067: Product](https://reader036.vdocuments.pub/reader036/viewer/2022082819/56813699550346895d9e2b1e/html5/thumbnails/1.jpg)
1
10067: Product ★★★★☆題組: Problem Set Archive with Online Judge
題號: 10067: Product解題者:雷皓博解題日期: 2014 年 5 月 5 日題意:給你初始值 (ex 8056) 和目地數字 (ex 6508), 並給你一些不可以到的數字 ,求到達目地數字的最少的步驟 ,無法到達的話輸出 -1.
(ex 8056 -> 8956 算一個步驟 )(ex 8056 -> 8156 算一個步驟 )
![Page 2: 10067: Product](https://reader036.vdocuments.pub/reader036/viewer/2022082819/56813699550346895d9e2b1e/html5/thumbnails/2.jpg)
2
題意範例:INPUT1 -> 幾組測資8 0 5 6 -> 初始值6 5 0 8 -> 目地數字5 -> 幾個不可到的數字8 0 5 7 -> 可到的數字8 0 4 75 5 0 87 5 0 86 4 0 8 OUTPUT14
![Page 3: 10067: Product](https://reader036.vdocuments.pub/reader036/viewer/2022082819/56813699550346895d9e2b1e/html5/thumbnails/3.jpg)
解法用 BFS 找最少步驟 , 每個數字有 8 個方向 .
(8056 有 8057,8055, 8066,8046, 8156,8956, 7056,9056)
我們可以把每一組 4 位數字都當成一個狀態 , 也就是 0000~9999
共一萬組狀態 , 外加跑過的要刪掉 ( 可用一個 array[10000] 來記錄 )
![Page 4: 10067: Product](https://reader036.vdocuments.pub/reader036/viewer/2022082819/56813699550346895d9e2b1e/html5/thumbnails/4.jpg)
解法範例:array 先記錄不可到的數字 .用 struct node 把 8056 跟 num = 0 包起來 Step1: 剛開始先把初始值 8056 丟入 queue 裡Step2: while(!queue.empty() )若 queue 是空得 , 跳到 Step7Step3: 拿 queue.front 的值 (current=queue.front), 並 pop
Step4: 判斷和目地數字是否相同 ,是的話跳出迴圈 ,並印出 current 的 num
Step5: 把 current 裡的數字的 8 個方向 (array 裡有的數字要除外 ) 的值跟 num+1 都放入 queue, 並且 array 紀錄跑過的數字 .
Step6: 到 Step2 繼續 Loop.Step7: 都找不到則印出 -1討論:無