1 / 4

10067: Product

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 ->幾組測資

kyra-rogers
Télécharger la présentation

10067: Product

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 算一個步驟 ) 1

  2. 題意範例: INPUT 1 ->幾組測資 8 0 5 6 ->初始值 6 5 0 8 ->目地數字 5 ->幾個不可到的數字 8 0 5 7 ->可到的數字 8 0 4 7 5 5 0 8 7 5 0 8 6 4 0 8 OUTPUT 14 2

  3. 解法用BFS找最少步驟,每個數字有8個方向. (8056有8057,8055, 8066,8046, 8156,8956, 7056,9056) 我們可以把每一組4位數字都當成一個狀態,也就是0000~9999 共一萬組狀態,外加跑過的要刪掉 (可用一個array[10000]來記錄)

  4. 解法範例: array 先記錄不可到的數字. 用struct node 把 8056 跟num = 0包起來 Step1:剛開始先把初始值8056丟入queue裡 Step2: while(!queue.empty() ) 若queue是空得,跳到Step7 Step3:拿queue.front的值(current=queue.front),並pop Step4:判斷和目地數字是否相同,是的話跳出迴圈,並印出current的num Step5:把current 裡的數字的8個方向(array裡有的數字要除外)的值跟num+1都放入queue,並且array紀錄跑過的數字. Step6:到Step2繼續Loop. Step7: 都找不到則印出-1 討論:無

More Related