返回列表 發帖

2/26 5x5的圖形請算出左上至右下不重複路線數量及路線

本帖最後由 justtim 於 2011-2-26 21:37 編輯

如果不是5x5 就將tra函數多加兩個參數x_end , y_end
  1. #include<iostream>
  2. #include<sstream>

  3. using namespace std ;
  4. int all = 0 ;
  5. void tra( int x , int y , string ta )
  6. {
  7.      if( x == 5 && y == 5 )
  8.      {
  9.          all ++ ;
  10.          cout << ta << "(" << x << "," << y << ")" << endl ;            
  11.      }
  12.      else
  13.      {
  14.          stringstream st ;
  15.          st << ta << "(" << x << "," << y << ")" ;
  16.          
  17.           if( x < 5 )
  18.           {
  19.               tra( x + 1 , y , st.str() ) ;
  20.           }
  21.           if( y < 5 )
  22.           {
  23.               tra( x , y + 1 , st.str() ) ;   
  24.           }
  25.      }
  26. }

  27. int main()
  28. {
  29.     tra( 0 , 0 , "" ) ;  
  30.     cout << all << endl ;
  31.     system( "Pause" ) ;
  32. }
複製代碼

返回列表