Posted 娣卞叆鍘熺悊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鐐瑰嚮馃憜钃濊壊鈥?nbsp;娣卞叆鍘熺悊鈥濓紝鍏虫敞骞垛€?/span>璁句负鏄熸爣鈥?/span>
鎶€鏈共璐э紝绗竴鏃堕棿鎺ㄩ€?/span>
浠婂ぉ锛屾垜涓昏璁插姩鎬佽鍒掔殑涓€浜涚悊璁虹煡璇嗐€傚瀹岃繖鑺傚唴瀹癸紝鍙互甯綘瑙e喅杩欐牱鍑犱釜闂锛氫粈涔堟牱鐨勯棶棰樺彲浠ョ敤鍔ㄦ€佽鍒掕В鍐筹紵瑙e喅鍔ㄦ€佽鍒掗棶棰樼殑涓€鑸€濊€冭繃绋嬫槸浠€涔堟牱鐨勶紵璐績銆佸垎娌汇€佸洖婧€佸姩鎬佽鍒掕繖鍥涚绠楁硶鎬濇兂鍙堟湁浠€涔堝尯鍒拰鑱旂郴锛?/span>
鐞嗚鐨勪笢瑗块兘姣旇緝鎶借薄锛屼笉杩囦綘涓嶇敤鎷呭績锛屾垜浼氱粨鍚堝叿浣撶殑渚嬪瓙鏉ヨ瑙o紝浜夊彇璁╀綘杩欐灏辫兘鐪熸鐞嗚В杩欎簺鐭ヨ瘑鐐癸紝涔熶负鍚庨潰鐨勫簲鐢ㄥ拰瀹炴垬鍋氬ソ鍑嗗銆?/span>
鈥滀竴涓ā鍨嬩笁涓壒寰佲€濈悊璁鸿瑙?/span>
浠€涔堟牱鐨勯棶棰橀€傚悎鐢ㄥ姩鎬佽鍒掓潵瑙e喅鍛紵鎹㈠彞璇濊锛屽姩鎬佽鍒掕兘瑙e喅鐨勯棶棰樻湁浠€涔堣寰嬪彲寰憿锛熷疄闄呬笂锛屽姩鎬佽鍒掍綔涓轰竴涓潪甯告垚鐔熺殑绠楁硶鎬濇兂锛屽緢澶氫汉瀵规宸茬粡鍋氫簡闈炲父鍏ㄩ潰鐨勬€荤粨銆傛垜鎶婅繖閮ㄥ垎鐞嗚鎬荤粨涓衡€滀竴涓ā鍨嬩笁涓壒寰佲€濄€?/span>
棣栧厛锛屾垜浠潵鐪嬶紝浠€涔堟槸鈥?/span>涓€涓ā鍨?/span>鈥濓紵瀹冩寚鐨勬槸鍔ㄦ€佽鍒掗€傚悎瑙e喅鐨勯棶棰樼殑妯″瀷銆傛垜鎶婅繖涓ā鍨嬪畾涔変负鈥?/span>澶氶樁娈靛喅绛栨渶浼樿В妯″瀷鈥濄€備笅闈㈡垜鍏蜂綋鏉ョ粰浣犺璁层€?/span>
鎴戜滑涓€鑸槸鐢ㄥ姩鎬佽鍒掓潵瑙e喅鏈€浼橀棶棰樸€傝€岃В鍐抽棶棰樼殑杩囩▼锛岄渶瑕佺粡鍘嗗涓喅绛栭樁娈点€傛瘡涓喅绛栭樁娈甸兘瀵瑰簲鐫€涓€缁勭姸鎬併€傜劧鍚庢垜浠鎵句竴缁勫喅绛栧簭鍒楋紝缁忚繃杩欑粍鍐崇瓥搴忓垪锛岃兘澶熶骇鐢熸渶缁堟湡鏈涙眰瑙g殑鏈€浼樺€笺€?/span>
鐜板湪锛屾垜浠啀鏉ョ湅锛屼粈涔堟槸鈥?/span>涓変釜鐗瑰緛鈥濓紵瀹冧滑鍒嗗埆鏄?/span>鏈€浼樺瓙缁撴瀯銆?/span>鏃犲悗鏁堟€?/span>鍜?/span>閲嶅瀛愰棶棰?/span>銆傝繖涓変釜姒傚康姣旇緝鎶借薄锛屾垜鏉ラ€愪竴璇︾粏瑙i噴涓€涓嬨€?/span>
1. 鏈€浼樺瓙缁撴瀯
鏈€浼樺瓙缁撴瀯鎸囩殑鏄紝闂鐨勬渶浼樿В鍖呭惈瀛愰棶棰樼殑鏈€浼樿В銆傚弽杩囨潵璇村氨鏄紝鎴戜滑鍙互閫氳繃瀛愰棶棰樼殑鏈€浼樿В锛屾帹瀵煎嚭闂鐨勬渶浼樿В銆傚鏋滄垜浠妸鏈€浼樺瓙缁撴瀯锛屽搴斿埌鎴戜滑鍓嶉潰瀹氫箟鐨勫姩鎬佽鍒掗棶棰樻ā鍨嬩笂锛岄偅鎴戜滑涔熷彲浠ョ悊瑙d负锛屽悗闈㈤樁娈电殑鐘舵€佸彲浠ラ€氳繃鍓嶉潰闃舵鐨勭姸鎬佹帹瀵煎嚭鏉ャ€?/span>
2. 鏃犲悗鏁堟€?/span>
鏃犲悗鏁堟€ф湁涓ゅ眰鍚箟锛岀涓€灞傚惈涔夋槸锛屽湪鎺ㄥ鍚庨潰闃舵鐨勭姸鎬佺殑鏃跺€欙紝鎴戜滑鍙叧蹇冨墠闈㈤樁娈电殑鐘舵€佸€硷紝涓嶅叧蹇冭繖涓姸鎬佹槸鎬庝箞涓€姝ヤ竴姝ユ帹瀵煎嚭鏉ョ殑銆傜浜屽眰鍚箟鏄紝鏌愰樁娈电姸鎬佷竴鏃︾‘瀹氾紝灏变笉鍙椾箣鍚庨樁娈电殑鍐崇瓥褰卞搷銆傛棤鍚庢晥鎬ф槸涓€涓潪甯糕€滃鏉锯€濈殑瑕佹眰銆傚彧瑕佹弧瓒冲墠闈㈡彁鍒扮殑鍔ㄦ€佽鍒掗棶棰樻ā鍨嬶紝鍏跺疄鍩烘湰涓婇兘浼氭弧瓒虫棤鍚庢晥鎬с€?/span>
3. 閲嶅瀛愰棶棰?/span>
杩欎釜姒傚康姣旇緝濂界悊瑙c€傚墠闈竴鑺傦紝鎴戝凡缁忓娆℃彁杩囥€傚鏋滅敤涓€鍙ヨ瘽姒傛嫭涓€涓嬶紝閭e氨鏄紝涓嶅悓鐨勫喅绛栧簭鍒楋紝鍒拌揪鏌愪釜鐩稿悓鐨勯樁娈垫椂锛屽彲鑳戒細浜х敓閲嶅鐨勭姸鎬併€?/span>
鈥滀竴涓ā鍨嬩笁涓壒寰佲€濆疄渚嬪墫鏋?/span>
鈥滀竴涓ā鍨嬩笁涓壒寰佲€濊繖閮ㄥ垎鏄悊璁虹煡璇嗭紝姣旇緝鎶借薄锛屼綘鐪嬩簡涔嬪悗鍙兘杩樻槸鏈夌偣鎳碉紝鏈夌浼兼噦闈炴噦鐨勬劅瑙夛紝娌″叧绯伙紝杩欎釜寰堟甯搞€傛帴涓嬫潵锛屾垜缁撳悎涓€涓叿浣撶殑鍔ㄦ€佽鍒掗棶棰橈紝鏉ョ粰浣犺缁嗚В閲娿€?/span>
鍋囪鎴戜滑鏈変竴涓?n 涔樹互 n 鐨勭煩闃?w[n][n]銆傜煩闃靛瓨鍌ㄧ殑閮芥槸姝f暣鏁般€傛瀛愯捣濮嬩綅缃湪宸︿笂瑙掞紝缁堟浣嶇疆鍦ㄥ彸涓嬭銆傛垜浠皢妫嬪瓙浠庡乏涓婅绉诲姩鍒板彸涓嬭銆傛瘡娆″彧鑳藉悜鍙虫垨鑰呭悜涓嬬Щ鍔ㄤ竴浣嶃€備粠宸︿笂瑙掑埌鍙充笅瑙掞紝浼氭湁寰堝涓嶅悓鐨勮矾寰勫彲浠ヨ蛋銆傛垜浠妸姣忔潯璺緞缁忚繃鐨勬暟瀛楀姞璧锋潵鐪嬩綔璺緞鐨勯暱搴︺€傞偅浠庡乏涓婅绉诲姩鍒板彸涓嬭鐨勬渶鐭矾寰勯暱搴︽槸澶氬皯鍛紵
鎴戜滑鍏堢湅鐪嬶紝杩欎釜闂鏄惁绗﹀悎鈥滀竴涓ā鍨嬧€濓紵
浠?(0, 0) 璧板埌 (n-1, n-1)锛屾€诲叡瑕佽蛋 2*(n-1) 姝ワ紝涔熷氨瀵瑰簲鐫€ 2*(n-1) 涓樁娈点€傛瘡涓樁娈甸兘鏈夊悜鍙宠蛋鎴栬€呭悜涓嬭蛋涓ょ鍐崇瓥锛屽苟涓旀瘡涓樁娈甸兘浼氬搴斾竴涓姸鎬侀泦鍚堛€?/span>
鎴戜滑鎶婄姸鎬佸畾涔変负 min_dist(i, j)锛屽叾涓?i 琛ㄧず琛岋紝j 琛ㄧず鍒椼€俶in_dist 琛ㄨ揪寮忕殑鍊艰〃绀轰粠 (0, 0) 鍒拌揪 (i, j) 鐨勬渶鐭矾寰勯暱搴︺€傛墍浠ワ紝杩欎釜闂鏄竴涓闃舵鍐崇瓥鏈€浼樿В闂锛岀鍚堝姩鎬佽鍒掔殑妯″瀷銆?/span>
鎴戜滑鍐嶆潵鐪嬶紝杩欎釜闂鏄惁绗﹀悎鈥滀笁涓壒寰佲€濓紵
鎴戜滑鍙互鐢ㄥ洖婧畻娉曟潵瑙e喅杩欎釜闂銆傚鏋滀綘鑷繁鍐欎竴涓嬩唬鐮侊紝鐢讳竴涓嬮€掑綊鏍戯紝灏变細鍙戠幇锛岄€掑綊鏍戜腑鏈夐噸澶嶇殑鑺傜偣銆傞噸澶嶇殑鑺傜偣琛ㄧず锛屼粠宸︿笂瑙掑埌鑺傜偣瀵瑰簲鐨勪綅缃紝鏈夊绉嶈矾绾匡紝杩欎篃鑳借鏄庤繖涓棶棰樹腑瀛樺湪閲嶅瀛愰棶棰樸€?/span>
濡傛灉鎴戜滑璧板埌 (i, j) 杩欎釜浣嶇疆锛屾垜浠彧鑳介€氳繃 (i-1, j)锛?i, j-1) 杩欎袱涓綅缃Щ鍔ㄨ繃鏉ワ紝涔熷氨鏄锛屾垜浠兂瑕佽绠?(i, j) 浣嶇疆瀵瑰簲鐨勭姸鎬侊紝鍙渶瑕佸叧蹇?(i-1, j)锛?i, j-1) 涓や釜浣嶇疆瀵瑰簲鐨勭姸鎬侊紝骞朵笉鍏冲績妫嬪瓙鏄€氳繃浠€涔堟牱鐨勮矾绾垮埌杈捐繖涓や釜浣嶇疆鐨勩€傝€屼笖锛屾垜浠粎浠呭厑璁稿線涓嬪拰寰€鍙崇Щ鍔紝涓嶅厑璁稿悗閫€锛屾墍浠ワ紝鍓嶉潰闃舵鐨勭姸鎬佺‘瀹氫箣鍚庯紝涓嶄細琚悗闈㈤樁娈电殑鍐崇瓥鎵€鏀瑰彉锛屾墍浠ワ紝杩欎釜闂绗﹀悎鈥滄棤鍚庢晥鎬р€濊繖涓€鐗瑰緛銆?/span>
鍒氬垰瀹氫箟鐘舵€佺殑鏃跺€欙紝鎴戜滑鎶婁粠璧峰浣嶇疆 (0, 0) 鍒?(i, j) 鐨勬渶灏忚矾寰勶紝璁颁綔 min_dist(i, j)銆傚洜涓烘垜浠彧鑳藉線鍙虫垨寰€涓嬬Щ鍔紝鎵€浠ワ紝鎴戜滑鍙湁鍙兘浠?(i, j-1) 鎴栬€?(i-1, j) 涓や釜浣嶇疆鍒拌揪 (i, j)銆備篃灏辨槸璇达紝鍒拌揪 (i, j) 鐨勬渶鐭矾寰勮涔堢粡杩?(i, j-1)锛岃涔堢粡杩?(i-1, j)锛岃€屼笖鍒拌揪 (i, j) 鐨勬渶鐭矾寰勮偗瀹氬寘鍚埌杈捐繖涓や釜浣嶇疆鐨勬渶鐭矾寰勪箣涓€銆傛崲鍙ヨ瘽璇村氨鏄紝min_dist(i, j) 鍙互閫氳繃 min_dist(i, j-1) 鍜?min_dist(i-1, j) 涓や釜鐘舵€佹帹瀵煎嚭鏉ャ€傝繖灏辫鏄庯紝杩欎釜闂绗﹀悎鈥滄渶浼樺瓙缁撴瀯鈥濄€?/span>
min_dist(i, j) = w[i][j] + min(min_dist(i, j-1), min_dist(i-1, j))
涓ょ鍔ㄦ€佽鍒掕В棰樻€濊矾鎬荤粨
鍒氬垰鎴戣浜嗭紝濡備綍閴村埆涓€涓棶棰樻槸鍚﹀彲浠ョ敤鍔ㄦ€佽鍒掓潵瑙e喅銆傜幇鍦紝鎴戝啀鎬荤粨涓€涓嬶紝鍔ㄦ€佽鍒掕В棰樼殑涓€鑸€濊矾锛岃浣犻潰瀵瑰姩鎬佽鍒掗棶棰樼殑鏃跺€欙紝鑳藉鏈夌珷鍙惊锛屼笉鑷充簬鏉熸墜鏃犵瓥銆?/span>
鎴戜釜浜鸿寰楋紝瑙e喅鍔ㄦ€佽鍒掗棶棰橈紝涓€鑸湁涓ょ鎬濊矾銆傛垜鎶婂畠浠垎鍒彨浣滐紝鐘舵€佽浆绉昏〃娉曞拰鐘舵€佽浆绉绘柟绋嬫硶銆?/span>
1. 鐘舵€佽浆绉昏〃娉?/span>
涓€鑸兘鐢ㄥ姩鎬佽鍒掕В鍐崇殑闂锛岄兘鍙互浣跨敤鍥炴函绠楁硶鐨勬毚鍔涙悳绱㈣В鍐炽€傛墍浠ワ紝褰撴垜浠嬁鍒伴棶棰樼殑鏃跺€欙紝鎴戜滑鍙互鍏堢敤绠€鍗曠殑鍥炴函绠楁硶瑙e喅锛岀劧鍚庡畾涔夌姸鎬侊紝姣忎釜鐘舵€佽〃绀轰竴涓妭鐐癸紝鐒跺悗瀵瑰簲鐢诲嚭閫掑綊鏍戙€備粠閫掑綊鏍戜腑锛屾垜浠緢瀹规槗鍙互鐪嬪嚭鏉ワ紝鏄惁瀛樺湪閲嶅瀛愰棶棰橈紝浠ュ強閲嶅瀛愰棶棰樻槸濡備綍浜х敓鐨勩€備互姝ゆ潵瀵绘壘瑙勫緥锛岀湅鏄惁鑳界敤鍔ㄦ€佽鍒掕В鍐炽€?/span>
鎵惧埌閲嶅瀛愰棶棰樹箣鍚庯紝鎺ヤ笅鏉ワ紝鎴戜滑鏈変袱绉嶅鐞嗘€濊矾锛岀涓€绉嶆槸鐩存帴鐢?/span>鍥炴函鍔犫€滃蹇樺綍鈥?/span>鐨勬柟娉曪紝鏉ラ伩鍏嶉噸澶嶅瓙闂銆備粠鎵ц鏁堢巼涓婃潵璁诧紝杩欒窡鍔ㄦ€佽鍒掔殑瑙e喅鎬濊矾娌℃湁宸埆銆傜浜岀鏄娇鐢ㄥ姩鎬佽鍒掔殑瑙e喅鏂规硶锛?/span>鐘舵€佽浆绉昏〃娉?/span>銆傜涓€绉嶆€濊矾锛屾垜灏变笉璁蹭簡锛屼綘鍙互鐪嬬湅涓婁竴鑺傜殑涓や釜渚嬪瓙銆傛垜浠噸鐐规潵鐪嬬姸鎬佽浆绉昏〃娉曟槸濡備綍宸ヤ綔鐨勩€?/span>
鎴戜滑鍏堢敾鍑轰竴涓姸鎬佽〃銆傜姸鎬佽〃涓€鑸兘鏄簩缁寸殑锛屾墍浠ヤ綘鍙互鎶婂畠鎯宠薄鎴愪簩缁存暟缁勩€傚叾涓紝姣忎釜鐘舵€佸寘鍚笁涓彉閲忥紝琛屻€佸垪銆佹暟缁勫€笺€傛垜浠牴鎹喅绛栫殑鍏堝悗杩囩▼锛屼粠鍓嶅線鍚庯紝鏍规嵁閫掓帹鍏崇郴锛屽垎闃舵濉厖鐘舵€佽〃涓殑姣忎釜鐘舵€併€傛渶鍚庯紝鎴戜滑灏嗚繖涓€掓帹濉〃鐨勮繃绋嬶紝缈昏瘧鎴愪唬鐮侊紝灏辨槸鍔ㄦ€佽鍒掍唬鐮佷簡銆?/span>
灏界澶ч儴鍒嗙姸鎬佽〃閮芥槸浜岀淮鐨勶紝浣嗘槸濡傛灉闂鐨勭姸鎬佹瘮杈冨鏉傦紝闇€瑕佸緢澶氬彉閲忔潵琛ㄧず锛岄偅瀵瑰簲鐨勭姸鎬佽〃鍙兘灏辨槸楂樼淮鐨勶紝姣斿涓夌淮銆佸洓缁淬€傞偅杩欎釜鏃跺€欙紝鎴戜滑灏变笉閫傚悎鐢ㄧ姸鎬佽浆绉昏〃娉曟潵瑙e喅浜嗐€備竴鏂归潰鏄洜涓洪珮缁寸姸鎬佽浆绉昏〃涓嶅ソ鐢诲浘琛ㄧず锛屽彟涓€鏂归潰鏄洜涓轰汉鑴戠‘瀹炲緢涓嶆搮闀挎€濊€冮珮缁寸殑涓滆タ銆?/span>
鐜板湪锛屾垜浠潵鐪嬩竴涓嬶紝濡備綍濂楃敤杩欎釜鐘舵€佽浆绉昏〃娉曪紝鏉ヨВ鍐充箣鍓嶉偅涓煩闃垫渶鐭矾寰勭殑闂锛?/span>
浠庤捣鐐瑰埌缁堢偣锛屾垜浠湁寰堝绉嶄笉鍚岀殑璧版硶銆傛垜浠彲浠ョ┓涓炬墍鏈夎蛋娉曪紝鐒跺悗瀵规瘮鎵惧嚭涓€涓渶鐭蛋娉曘€備笉杩囧浣曟墠鑳芥棤閲嶅鍙堜笉閬楁紡鍦扮┓涓惧嚭鎵€鏈夎蛋娉曞憿锛熸垜浠彲浠ョ敤鍥炴函绠楁硶杩欎釜姣旇緝鏈夎寰嬬殑绌蜂妇绠楁硶銆?/span>
鍥炴函绠楁硶鐨勪唬鐮佸疄鐜板涓嬫墍绀恒€備唬鐮佸緢鐭紝鑰屼笖鎴戝墠闈篃鍒嗘瀽杩囧緢澶氬洖婧畻娉曠殑渚嬮锛岃繖閲屾垜灏变笉澶氬仛瑙i噴浜嗭紝浣犺嚜宸辨潵鐪嬬湅銆?/span>
private int minDist = Integer.MAX_VALUE; // 鍏ㄥ眬鍙橀噺鎴栬€呮垚鍛樺彉閲?/span>
// 璋冪敤鏂瑰紡锛歮inDistBacktracing(0, 0, 0, w, n);
public void minDistBT(int i, int j, int dist, int[][] w, int n) {
// 鍒拌揪浜唍-1, n-1杩欎釜浣嶇疆浜嗭紝杩欓噷鐪嬬潃鏈夌偣濂囨€搱锛屼綘鑷繁涓句釜渚嬪瓙鐪嬩笅
if (i == n && j == n) {
if (dist < minDist) minDist = dist;
return;
}
if (i < n) { // 寰€涓嬭蛋锛屾洿鏂癷=i+1, j=j
minDistBT(i + 1, j, dist+w[i][j], w, n);
}
if (j < n) { // 寰€鍙宠蛋锛屾洿鏂癷=i, j=j+1
minDistBT(i, j+1, dist+w[i][j], w, n);
}
}
鏈変簡鍥炴函浠g爜涔嬪悗锛屾帴涓嬫潵锛屾垜浠鐢诲嚭閫掑綊鏍戯紝浠ユ鏉ュ鎵鹃噸澶嶅瓙闂銆傚湪閫掑綊鏍戜腑锛屼竴涓姸鎬侊紙涔熷氨鏄竴涓妭鐐癸級鍖呭惈涓変釜鍙橀噺 (i, j, dist)锛屽叾涓?i锛宩 鍒嗗埆琛ㄧず琛屽拰鍒楋紝dist 琛ㄧず浠庤捣鐐瑰埌杈?(i, j) 鐨勮矾寰勯暱搴︺€備粠鍥句腑锛屾垜浠湅鍑猴紝灏界 (i, j, dist) 涓嶅瓨鍦ㄩ噸澶嶇殑锛屼絾鏄?(i, j) 閲嶅鐨勬湁寰堝銆傚浜?(i, j) 閲嶅鐨勮妭鐐癸紝鎴戜滑鍙渶瑕侀€夋嫨 dist 鏈€灏忕殑鑺傜偣锛岀户缁€掑綊姹傝В锛屽叾浠栬妭鐐瑰氨鍙互鑸嶅純浜嗐€?/span>
鏃㈢劧瀛樺湪閲嶅瀛愰棶棰橈紝鎴戜滑灏卞彲浠ュ皾璇曠湅涓嬶紝鏄惁鍙互鐢ㄥ姩鎬佽鍒掓潵瑙e喅鍛紵
鎴戜滑鐢诲嚭涓€涓簩缁寸姸鎬佽〃锛岃〃涓殑琛屻€佸垪琛ㄧず妫嬪瓙鎵€鍦ㄧ殑浣嶇疆锛岃〃涓殑鏁板€艰〃绀轰粠璧风偣鍒拌繖涓綅缃殑鏈€鐭矾寰勩€傛垜浠寜鐓у喅绛栬繃绋嬶紝閫氳繃涓嶆柇鐘舵€侀€掓帹婕旇繘锛屽皢鐘舵€佽〃濉ソ銆備负浜嗘柟渚夸唬鐮佸疄鐜帮紝鎴戜滑鎸夎鏉ヨ繘琛屼緷娆″~鍏呫€?/span>
寮勬噦浜嗗~琛ㄧ殑杩囩▼锛屼唬鐮佸疄鐜板氨绠€鍗曞浜嗐€傛垜浠皢涓婇潰鐨勮繃绋嬶紝缈昏瘧鎴愪唬鐮侊紝灏辨槸涓嬮潰杩欎釜鏍峰瓙銆傜粨鍚堢潃浠g爜銆佸浘鍜屾枃瀛楁弿杩帮紝搴旇鏇村鏄撶悊瑙f垜璁茬殑鍐呭銆?/span>
public int minDistDP(int[][] matrix, int n) {
int[][] states = new int[n][n];
int sum = 0;
for (int j = 0; j < n; ++j) { // 鍒濆鍖?span class="code-snippet__attr">states鐨勭涓€琛屾暟鎹?/span>
sum += matrix[0][j];
states[0][j] = sum;
}
sum = 0;
for (int i = 0; i < n; ++i) { // 鍒濆鍖?span class="code-snippet__attr">states鐨勭涓€鍒楁暟鎹?/span>
sum += matrix[i][0];
states[i][0] = sum;
}
for (int i = 1; i < n; ++i) {
for (int j = 1; j < n; ++j) {
states[i][j] =
matrix[i][j] + Math.min(states[i][j-1], states[i-1][j]);
}
}
return states[n-1][n-1];
}
2. 鐘舵€佽浆绉绘柟绋嬫硶
鐘舵€佽浆绉绘柟绋嬫硶鏈夌偣绫讳技閫掑綊鐨勮В棰樻€濊矾銆傛垜浠渶瑕佸垎鏋愶紝鏌愪釜闂濡備綍閫氳繃瀛愰棶棰樻潵閫掑綊姹傝В锛屼篃灏辨槸鎵€璋撶殑鏈€浼樺瓙缁撴瀯銆傛牴鎹渶浼樺瓙缁撴瀯锛屽啓鍑洪€掑綊鍏紡锛屼篃灏辨槸鎵€璋撶殑鐘舵€佽浆绉绘柟绋嬨€傛湁浜嗙姸鎬佽浆绉绘柟绋嬶紝浠g爜瀹炵幇灏遍潪甯哥畝鍗曚簡銆備竴鑸儏鍐典笅锛屾垜浠湁涓ょ浠g爜瀹炵幇鏂规硶锛屼竴绉嶆槸閫掑綊鍔犫€滃蹇樺綍鈥?/span>锛屽彟涓€绉嶆槸杩唬閫掓帹銆?/span>
鎴戜滑杩樻槸鎷垮垰鎵嶇殑渚嬪瓙鏉ヤ妇渚嬨€傛渶浼樺瓙缁撴瀯鍓嶉潰宸茬粡鍒嗘瀽杩囦簡锛屼綘鍙互鍥炶繃澶村幓鍐嶇湅涓嬨€備负浜嗘柟渚夸綘鏌ョ湅锛屾垜鎶婄姸鎬佽浆绉绘柟绋嬫斁鍒拌繖閲屻€?/span>
min_dist(i, j) = w[i][j] + min(min_dist(i, j-1), min_dist(i-1, j))
杩欓噷鎴戝己璋冧竴涓嬶紝鐘舵€佽浆绉绘柟绋嬫槸瑙e喅鍔ㄦ€佽鍒掔殑鍏抽敭銆?/span>濡傛灉鎴戜滑鑳藉啓鍑虹姸鎬佽浆绉绘柟绋嬶紝閭e姩鎬佽鍒掗棶棰樺熀鏈笂灏辫В鍐充竴澶у崐浜嗭紝鑰岀炕璇戞垚浠g爜闈炲父绠€鍗曘€備絾鏄緢澶氬姩鎬佽鍒掗棶棰樼殑鐘舵€佹湰韬氨涓嶅ソ瀹氫箟锛岀姸鎬佽浆绉绘柟绋嬩篃灏辨洿涓嶅ソ鎯冲埌銆?/span>
涓嬮潰鎴戠敤閫掑綊鍔犫€滃蹇樺綍鈥濈殑鏂瑰紡锛屽皢鐘舵€佽浆绉绘柟绋嬬炕璇戞垚鏉ヤ唬鐮侊紝浣犲彲浠ョ湅鐪嬨€傚浜庡彟涓€绉嶅疄鐜版柟寮忥紝璺熺姸鎬佽浆绉昏〃娉曠殑浠g爜瀹炵幇鏄竴鏍风殑锛屽彧鏄€濊矾涓嶅悓銆?/span>
private int[][] matrix =
{{1锛?锛?锛?}, {2锛?锛?锛?}锛寋5锛?锛?锛?}锛寋6锛?锛?锛?}};
private int n = 4;
private int[][] mem = new int[4][4];
public int minDist(int i, int j) { // 璋冪敤minDist(n-1, n-1);
if (i == 0 && j == 0) return matrix[0][0];
if (mem[i][j] > 0) return mem[i][j];
int minLeft = Integer.MAX_VALUE;
if (j-1 >= 0) {
minLeft = minDist(i, j-1);
}
int minUp = Integer.MAX_VALUE;
if (i-1 >= 0) {
minUp = minDist(i-1, j);
}
int currMinDist = matrix[i][j] + Math.min(minLeft, minUp);
mem[i][j] = currMinDist;
return currMinDist;
}
涓ょ鍔ㄦ€佽鍒掕В棰樻€濊矾鍒拌繖閲屽氨璁插畬浜嗐€傛垜瑕佸己璋冧竴鐐癸紝涓嶆槸姣忎釜闂閮藉悓鏃堕€傚悎杩欎袱绉嶈В棰樻€濊矾銆傛湁鐨勯棶棰樺彲鑳界敤绗竴绉嶆€濊矾鏇存竻鏅帮紝鑰屾湁鐨勯棶棰樺彲鑳界敤绗簩绉嶆€濊矾鏇存竻鏅帮紝鎵€浠ワ紝浣犺缁撳悎鍏蜂綋鐨勯鐩潵鐪嬶紝鍒板簳閫夋嫨鐢ㄥ摢绉嶈В棰樻€濊矾銆?/span>
鍥涚绠楁硶鎬濇兂姣旇緝鍒嗘瀽
鍒颁粖澶╀负姝紝鎴戜滑宸茬粡瀛︿範浜嗗洓绉嶇畻娉曟€濇兂锛岃椽蹇冦€佸垎娌汇€佸洖婧拰鍔ㄦ€佽鍒掋€備粖澶╃殑鍐呭涓昏璁蹭簺鐞嗚鐭ヨ瘑锛屾垜姝eソ涓€鍧楀効涔熷垎鏋愪竴涓嬭繖鍥涚绠楁硶锛岀湅鐪嬪畠浠箣闂存湁浠€涔堝尯鍒拰鑱旂郴銆?/span>
濡傛灉鎴戜滑灏嗚繖鍥涚绠楁硶鎬濇兂鍒嗕竴涓嬬被锛岄偅璐績銆佸洖婧€佸姩鎬佽鍒掑彲浠ュ綊涓轰竴绫伙紝鑰屽垎娌诲崟鐙彲浠ヤ綔涓轰竴绫伙紝鍥犱负瀹冭窡鍏朵粬涓変釜閮戒笉澶т竴鏍枫€備负浠€涔堣繖涔堣鍛紵鍓嶄笁涓畻娉曡В鍐抽棶棰樼殑妯″瀷锛岄兘鍙互鎶借薄鎴愭垜浠粖澶╄鐨勯偅涓闃舵鍐崇瓥鏈€浼樿В妯″瀷锛岃€屽垎娌荤畻娉曡В鍐崇殑闂灏界澶ч儴鍒嗕篃鏄渶浼樿В闂锛屼絾鏄紝澶ч儴鍒嗛兘涓嶈兘鎶借薄鎴愬闃舵鍐崇瓥妯″瀷銆?/span>
鍥炴函绠楁硶鏄釜鈥滀竾閲戞补鈥濄€傚熀鏈笂鑳界敤鐨勫姩鎬佽鍒掋€佽椽蹇冭В鍐崇殑闂锛屾垜浠兘鍙互鐢ㄥ洖婧畻娉曡В鍐炽€傚洖婧畻娉曠浉褰撲簬绌蜂妇鎼滅储銆傜┓涓炬墍鏈夌殑鎯呭喌锛岀劧鍚庡姣斿緱鍒版渶浼樿В銆備笉杩囷紝鍥炴函绠楁硶鐨勬椂闂村鏉傚害闈炲父楂橈紝鏄寚鏁扮骇鍒殑锛屽彧鑳界敤鏉ヨВ鍐冲皬瑙勬ā鏁版嵁鐨勯棶棰樸€傚浜庡ぇ瑙勬ā鏁版嵁鐨勯棶棰橈紝鐢ㄥ洖婧畻娉曡В鍐崇殑鎵ц鏁堢巼灏卞緢浣庝簡銆?/span>
灏界鍔ㄦ€佽鍒掓瘮鍥炴函绠楁硶楂樻晥锛屼絾鏄紝骞朵笉鏄墍鏈夐棶棰橈紝閮藉彲浠ョ敤鍔ㄦ€佽鍒掓潵瑙e喅銆傝兘鐢ㄥ姩鎬佽鍒掕В鍐崇殑闂锛岄渶瑕佹弧瓒充笁涓壒寰侊紝鏈€浼樺瓙缁撴瀯銆佹棤鍚庢晥鎬у拰閲嶅瀛愰棶棰樸€傚湪閲嶅瀛愰棶棰樿繖涓€鐐逛笂锛屽姩鎬佽鍒掑拰鍒嗘不绠楁硶鐨勫尯鍒嗛潪甯告槑鏄俱€傚垎娌荤畻娉曡姹傚垎鍓叉垚鐨勫瓙闂锛屼笉鑳芥湁閲嶅瀛愰棶棰橈紝鑰屽姩鎬佽鍒掓濂界浉鍙嶏紝鍔ㄦ€佽鍒掍箣鎵€浠ラ珮鏁堬紝灏辨槸鍥犱负鍥炴函绠楁硶瀹炵幇涓瓨鍦ㄥぇ閲忕殑閲嶅瀛愰棶棰樸€?/span>
璐績绠楁硶瀹為檯涓婃槸鍔ㄦ€佽鍒掔畻娉曠殑涓€绉嶇壒娈婃儏鍐点€傚畠瑙e喅闂璧锋潵鏇村姞楂樻晥锛屼唬鐮佸疄鐜颁篃鏇村姞绠€娲併€備笉杩囷紝瀹冨彲浠ヨВ鍐崇殑闂涔熸洿鍔犳湁闄愩€傚畠鑳借В鍐崇殑闂闇€瑕佹弧瓒充笁涓潯浠讹紝鏈€浼樺瓙缁撴瀯銆佹棤鍚庢晥鎬у拰璐績閫夋嫨鎬э紙杩欓噷鎴戜滑涓嶆€庝箞寮鸿皟閲嶅瀛愰棶棰橈級銆?/span>
鍏朵腑锛屾渶浼樺瓙缁撴瀯銆佹棤鍚庢晥鎬ц窡鍔ㄦ€佽鍒掍腑鐨勬棤寮傘€傗€滆椽蹇冮€夋嫨鎬р€濈殑鎰忔€濇槸锛岄€氳繃灞€閮ㄦ渶浼樼殑閫夋嫨锛岃兘浜х敓鍏ㄥ眬鐨勬渶浼橀€夋嫨銆傛瘡涓€涓樁娈碉紝鎴戜滑閮介€夋嫨褰撳墠鐪嬭捣鏉ユ渶浼樼殑鍐崇瓥锛屾墍鏈夐樁娈电殑鍐崇瓥瀹屾垚涔嬪悗锛屾渶缁堢敱杩欎簺灞€閮ㄦ渶浼樿В鏋勬垚鍏ㄥ眬鏈€浼樿В銆?/span>
鍐呭灏忕粨
浠婂ぉ鐨勫唴瀹瑰埌姝ゅ氨璁插畬浜嗭紝鎴戝甫浣犳潵澶嶄範涓€涓嬨€?/span>
鎴戦鍏堣浜嗕粈涔堟牱鐨勯棶棰橀€傚悎鐢ㄥ姩鎬佽鍒掕В鍐炽€傝繖浜涢棶棰樺彲浠ユ€荤粨姒傛嫭涓衡€滀竴涓ā鍨嬩笁涓壒寰佲€濄€傚叾涓紝鈥滀竴涓ā鍨嬧€濇寚鐨勬槸锛岄棶棰樺彲浠ユ娊璞℃垚鍒嗛樁娈靛喅绛栨渶浼樿В妯″瀷銆傗€滀笁涓壒寰佲€濇寚鐨勬槸鏈€浼樺瓙缁撴瀯銆佹棤鍚庢晥鎬у拰閲嶅瀛愰棶棰樸€?/span>
鐒跺悗锛屾垜璁蹭簡涓ょ鍔ㄦ€佽鍒掔殑瑙i鎬濊矾銆傚畠浠垎鍒槸鐘舵€佽浆绉昏〃娉曞拰鐘舵€佽浆绉绘柟绋嬫硶銆傚叾涓紝鐘舵€佽浆绉昏〃娉曡В棰樻€濊矾澶ц嚧鍙互姒傛嫭涓猴紝鍥炴函绠楁硶瀹炵幇 - 瀹氫箟鐘舵€?- 鐢婚€掑綊鏍?- 鎵鹃噸澶嶅瓙闂 - 鐢荤姸鎬佽浆绉昏〃 - 鏍规嵁閫掓帹鍏崇郴濉〃 - 灏嗗~琛ㄨ繃绋嬬炕璇戞垚浠g爜銆傜姸鎬佽浆绉绘柟绋嬫硶鐨勫ぇ鑷存€濊矾鍙互姒傛嫭涓猴紝鎵炬渶浼樺瓙缁撴瀯 - 鍐欑姸鎬佽浆绉绘柟绋?- 灏嗙姸鎬佽浆绉绘柟绋嬬炕璇戞垚浠g爜銆?/span>
鏈€鍚庯紝鎴戜滑瀵规瘮浜嗕箣鍓嶈杩囩殑鍥涚绠楁硶鎬濇兂銆傝椽蹇冦€佸洖婧€佸姩鎬佽鍒掑彲浠ヨВ鍐崇殑闂妯″瀷绫讳技锛岄兘鍙互鎶借薄鎴愬闃舵鍐崇瓥鏈€浼樿В妯″瀷銆傚敖绠″垎娌荤畻娉曚篃鑳借В鍐虫渶浼橀棶棰橈紝浣嗘槸澶ч儴鍒嗛棶棰樼殑鑳屾櫙閮戒笉閫傚悎鎶借薄鎴愬闃舵鍐崇瓥妯″瀷銆?/span>
浠婂ぉ鐨勫唴瀹规瘮杈冨亸鐞嗚锛屽彲鑳戒細涓嶅ソ鐞嗚В銆傚緢澶氱悊璁虹煡璇嗙殑瀛︿範锛屽崟绾殑濉腑寮忚缁欎綘鍚紝瀹為檯涓婃晥鏋滃苟涓嶅ソ銆傝鎯崇湡鐨勬妸杩欎簺鐞嗚鐭ヨ瘑鐞嗚В閫忥紝鍖栦负宸辩敤锛岃繕鏄渶瑕佷綘鑷繁澶氭€濊€冿紝澶氱粌涔犮€傜瓑浣犲仛浜嗚冻澶熷鐨勯鐩箣鍚庯紝鑷劧灏辫兘鑷繁鎮熷嚭涓€浜涗笢瑗匡紝杩欐牱鍐嶅洖杩囧ご鏉ョ湅鐞嗚锛屽氨浼氶潪甯稿鏄撶湅鎳傘€?/span>
鎵€浠ワ紝鍦ㄤ粖澶╃殑鍐呭涓紝濡傛灉鏈夊摢浜涘湴鏂逛綘杩樹笉鑳界悊瑙o紝閭d篃娌″叧绯伙紝鍏堟斁涓€鏀俱€備笅涓€鑺傦紝鎴戜細杩愮敤浠婂ぉ璁插埌鐨勭悊璁猴紝鍐嶈В鍐冲嚑涓姩鎬佽鍒掔殑闂銆傜瓑浣犲瀹屼笅涓€鑺傦紝鍙互鍐嶅洖杩囧ご鏉ョ湅涓嬩粖澶╃殑鐞嗚鐭ヨ瘑锛屽彲鑳藉氨浼氭湁涓€绉嶉】鎮熺殑鎰熻銆?/span>
-娣卞叆鍘熺悊-
鐭ュ叾鐒跺苟鐭ュ叾鎵€浠ョ劧
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章