亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 4690 | 回復: 2
打印 上一主題 下一主題

請數(shù)學高手幫忙證明算法。 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-08-30 10:00 |只看該作者 |倒序瀏覽
前幾天看到hdu 1195:
Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9.
Each time, you can add or minus 1 to any digit. When add 1 to '9', the digit will change to be '1' and when minus 1 to '1', the digit will change to be '9'. You can also exchange the digit with its neighbor. Each action will take one step.

Now your task is to use minimal steps to open the lock.

Note: The leftmost digit is not the neighbor of the rightmost digit.
我用如下代碼解,其中用到了兩個假設(shè):
1. 對于給定操作步驟,操作順序可以打亂。(將各位編號,交換同時交換編號,增減操作綁定編號)
2. 對于可通過純交換實現(xiàn)的變化,則最少交換步驟等同于將源組合冒泡排序至目標組合。

calculate函數(shù)求解,參數(shù)為 源狀態(tài) 終狀態(tài),結(jié)果為 (最少步數(shù), _)
  1. import Data.Maybe
  2. import Data.List

  3. steps4Exchange :: [ Int ] -> Int
  4. steps4Exchange dst =
  5.   snd $ iterate swapPass (dst, 0) !! (length dst - 1)
  6.   where swapPass ([ x ], i) = ([ x ], i)
  7.         swapPass (x : y : zs, i)
  8.           | x > y =
  9.             let (xs, i') = swapPass (x : zs, i + 1) in
  10.             (y : xs, i')
  11.           | otherwise =
  12.               let (xs, i') = swapPass (y : zs, i) in
  13.               (x : xs, i')

  14. steps4Move :: [ Int ] -> [ Int ] -> Int
  15. steps4Move src dst =
  16.   sum $ map (\(s, i) ->
  17.               let d = dst !! i in
  18.               min (abs (d - s)) (9 + s - d)
  19.            ) $ zip src [ 0 .. ]

  20. calculate :: [ Int ] -> [ Int ] -> (Int, [ Int ])
  21. calculate src dst =
  22.   let src' = zip src [ 0 .. ] in
  23.   foldl1 (\(a, a') (b, b') ->
  24.            if a > b then
  25.              (b, b')
  26.            else
  27.              (a, a')
  28.         ) $ map (\mid' ->
  29.                    let mid = fst $ unzip mid' in
  30.                    (steps4Exchange (snd $ unzip mid') + steps4Move mid dst, mid)
  31.                 ) $ permutations src'
復制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2010-08-31 20:50 |只看該作者
這個貌似是BFS獲取最小步數(shù)吧,你想證明什么{:3_196:}

論壇徽章:
0
3 [報告]
發(fā)表于 2010-09-02 16:18 |只看該作者
這個貌似是BFS獲取最小步數(shù)吧,你想證明什么
daybreakcx 發(fā)表于 2010-08-31 20:50



    算是個bfs的應(yīng)用優(yōu)化問題吧。目的是減少需要遍歷的元素。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP