Skip to content

Instantly share code, notes, and snippets.

@ygabo
Created August 20, 2013 14:02
Show Gist options
  • Save ygabo/6281882 to your computer and use it in GitHub Desktop.
Save ygabo/6281882 to your computer and use it in GitHub Desktop.
Robot nxn board. Can only go right or go down. How many paths?
int main() {
rbtree<int> tree;
int n = 10;
vector<int> cur(n), prev(n,1);
vector<vector<int>> board(n, vector<int>(n));
///
//iota(prev.begin(), prev.end(), 0);
cout <<endl;
for( int i = 0; i < n; i++){
board[i][0] = 1;
board[0][i] = 1;
}
for( int i = 1; i < n; i++){
for( int j = 1; j < n; j++){
board[i][j] = board[i-1][j] + board[i][j-1];
}
}
cout << board[9][9] << endl;
for( int i = 0; i < n; i++){
for( int j = 0; j < n; j++){
cout << board[i][j] << " ";
}
cout <<endl;
}
for( int i = 1; i < n; i++){
for( int j = 0; j < n; j++){
if( j == 0 )
cur[j] = 1;
else
cur[j] = cur[j-1] + prev[j];
}
prev = cur;
cout << endl;
} for( auto i = prev.begin(); i != prev.end(); ++i)
cout << *i << " ";
cout << endl;
for( auto i = cur.begin(); i != cur.end(); ++i)
cout << *i << " ";
cout <<endl;
cout << cur[n-1] << endl;
cout << "done " << endl;
std::cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment