鍙﹀緇欏嚭鍙︿竴縐嶉潪閫掑綊綆楁硶 綆楁硶浠嬬粛錛?br>棣栧厛瀹規(guī)槗璇佹槑錛屽綋鐩樺瓙鐨勪釜鏁頒負(fù)n鏃訛紝縐誨姩鐨勬鏁板簲絳変簬2^n - 1銆?br>涓浣嶇編鍥藉鑰呭彂鐜頒竴縐嶅嚭浜烘剰鏂欑殑鏂規(guī)硶錛屽彧瑕佽疆嫻佽繘琛屼袱姝ユ搷浣滃氨鍙互浜嗐?br>棣栧厛鎶婁笁鏍規(guī)煴瀛愭寜欏哄簭鎺掓垚鍝佸瓧鍨嬶紝鎶婃墍鏈夌殑鍦嗙洏鎸変粠澶у埌灝忕殑欏哄簭鏀懼湪鏌卞瓙A涓娿?br>鏍規(guī)嵁鍦嗙洏鐨勬暟閲忕‘瀹氭煴瀛愮殑鎺掓斁欏哄簭錛氳嫢n涓哄伓鏁幫紝鎸夐『鏃墮拡鏂瑰悜渚濇鎽嗘斁 A B C錛?br>鑻涓哄鏁幫紝鎸夐『鏃墮拡鏂瑰悜渚濇鎽嗘斁 A C B銆?br>錛?錛夋寜欏烘椂閽堟柟鍚戞妸鍦嗙洏1浠庣幇鍦ㄧ殑鏌卞瓙縐誨姩鍒頒笅涓鏍規(guī)煴瀛愶紝鍗沖綋n涓哄伓鏁版椂錛岃嫢鍦嗙洏1鍦ㄦ煴瀛怉錛屽垯鎶婂畠縐誨姩鍒癇錛?br>鑻ュ渾鐩?鍦ㄦ煴瀛怋錛屽垯鎶婂畠縐誨姩鍒癈錛涜嫢鍦嗙洏1鍦ㄦ煴瀛怌錛屽垯鎶婂畠縐誨姩鍒癆銆?br>錛?錛夋帴鐫錛屾妸鍙﹀涓ゆ牴鏌卞瓙涓婂彲浠ョЩ鍔ㄧ殑鍦嗙洏縐誨姩鍒版柊鐨勬煴瀛愪笂銆?br>鍗蟲妸闈炵┖鏌卞瓙涓婄殑鍦嗙洏縐誨姩鍒扮┖鏌卞瓙涓婏紝褰撲袱鏍規(guī)煴瀛愰兘闈炵┖鏃訛紝縐誨姩杈冨皬鐨勫渾鐩?br>榪欎竴姝ユ病鏈夋槑紜瀹氱Щ鍔ㄥ摢涓渾鐩橈紝浣犲彲鑳戒互涓轟細(xì)鏈夊縐嶅彲鑳芥э紝鍏跺疄涓嶇劧錛屽彲瀹炴柦鐨勮鍔ㄦ槸鍞竴鐨勩?br>錛?錛夊弽澶嶈繘琛岋紙1錛夛紙2錛夋搷浣滐紝鏈鍚庡氨鑳芥寜瑙勫畾瀹屾垚姹夎濉旂殑縐誨姩銆?br> 鏈鍚庡氨鏄敤鏍堟ā鎷熼掑綊綆楁硶,鐒跺悗涔熷彲浠ュ疄鐜伴潪閫掑綊綆楁硶.... [濡傛湁閿欒,璇鋒寚鍑篯
]]>[Exercise/31#]榪峰闂--BFS and DFShttp://www.shnenglu.com/IssacAsimoy/articles/55454.htmlIssAcIssAcSun, 06 Jul 2008 02:23:00 GMThttp://www.shnenglu.com/IssacAsimoy/articles/55454.htmlhttp://www.shnenglu.com/IssacAsimoy/comments/55454.htmlhttp://www.shnenglu.com/IssacAsimoy/articles/55454.html#Feedback0http://www.shnenglu.com/IssacAsimoy/comments/commentRss/55454.htmlhttp://www.shnenglu.com/IssacAsimoy/services/trackbacks/55454.html#include <iostream> #include <utility> #include <queue> #include <stack> usingnamespace std; constint SPACE=0; constint WALL=1; bool bFinish; int Path[4][2]={{-1,0},{0,-1},{0,1},{1,0}}; int M[5][5]={{0,1,1,1,0}, {0,0,0,0,0}, {0,1,0,0,1}, {0,1,0,0,0}, {0,1,1,1,0}}; bool fnIsInSide(int a,int b){ return (a>=0&&a<=4&&b>=0&&b<=4); } int fnBfs(){ queue<pair<int,int>>MyQ; int nX,nY,nCnt=1; pair<int,int>MyP_A,MyP_B; MyP_A.first=0; MyP_A.second=0; MyQ.push(MyP_A); while(!MyQ.empty()) { MyP_A=MyQ.front(); ++nCnt; if(MyP_A.first==4&&MyP_A.second==4) return++bFinish; MyQ.pop(); M[MyP_A.first][MyP_A.second]=WALL; for(int i=0;i<4;++i) { nX=MyP_A.first+Path[i][0]; nY=MyP_A.second+Path[i][1]; if(fnIsInSide(nX,nY)&&M[nX][nY]!=WALL) { MyP_B.first=nX; MyP_B.second=nY; MyQ.push(MyP_B); } } } return bFinish; } void fnDfs(int x,int y){ int nX,nY; if(x==4&&y==4) ++bFinish; if(bFinish) return; for(int i=0;i<4;++i) { nX=x+Path[i][0]; nY=y+Path[i][1]; if(fnIsInSide(nX,nY)&&M[nX][nY]!=WALL) { M[nX][nY]=WALL; fnDfs(nX,nY); M[nX][nY]=SPACE; } } } int main(){ int hWay; cout<<"If you want Breadth First Search ,input(0),else input(1) -- Depth First Search."<<endl; cin>>hWay; hWay?fnDfs(0,0):(void)fnBfs(); bFinish?cout<<"The path is found."<<endl:cout<<"The path isn't found"<<endl; return0; } /**//* cin>>hWay; hWay?fnDfs(0,0):fnBfs(); 2涓嚱鏁?br>void fnDfs(0,0); int fnBfs(); 涓嶆噦涓轟粈涔圙++ 緙栬瘧鍣ㄦ彁紺?br>error: `fnDfs(0, 0)' has type `void' and is not a throw-expression 鍘熸潵榪欓噷蹇呴』 A?B:C 鏉′歡鎿嶄綔絎鍜孋瑕佹湁鐩稿悓鐨勭被鍨嬫垨鑰呭彲浠ョ浉浜掕漿鍖?br>浣犵殑涓涓獀oid錛屼竴涓猧nt鑲畾涓嶈 The rule is: if one the operand is of type void, then the other | > operand must be of type void or a throw expression -- but both cannot | > be a throw-expression. */