Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor of the solution for Problem #15
- Loading branch information
Showing
2 changed files
with
31 additions
and
14 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,45 @@ | ||
#include <cstdio> | ||
#include <algorithm> | ||
#include <cstdlib> | ||
|
||
using namespace std; | ||
|
||
const int N = 2; | ||
#define N 20 | ||
|
||
int i, A[ 2*N ]; | ||
long int** C, i, j; | ||
|
||
int main() { | ||
for ( i = 0; i < N; ++i ) { | ||
A[ i ] = 0; | ||
A[ i + N ] = 1; | ||
long int CountRoutes( int x, int y ) { | ||
if ( x == 0 && y == 0 ) { | ||
return 1; | ||
} | ||
long int cost = 0; | ||
if ( y > 0 ) { | ||
if ( C[ x ][ y - 1 ] == -1 ) { | ||
C[ x ][ y - 1 ] = CountRoutes( x, y - 1 ); | ||
} | ||
|
||
i = 1; | ||
while ( next_permutation( A, A + 2 * N ) ) { | ||
for ( i = 0; i < 2 * N; ++i ) { | ||
printf( "%i ", A[ i ] ); | ||
cost += C[ x ][ y - 1 ]; | ||
} | ||
if ( x > 0 ) { | ||
if ( C[ x - 1 ][ y ] == -1 ) { | ||
C[ x - 1 ][ y ] = CountRoutes( x - 1, y ); | ||
} | ||
printf( "\n" ); | ||
++i; | ||
|
||
cost += C[ x - 1 ][ y ]; | ||
} | ||
|
||
printf( "Solution: %i\n", i ); | ||
return cost; | ||
} | ||
|
||
int main() { | ||
C = ( long int** )malloc( ( N + 1 ) * sizeof( long int* ) ); | ||
for ( i = 0; i <= N; ++i ) { | ||
C[ i ] = ( long int* )malloc( ( N + 1 ) * sizeof( long int ) ); | ||
|
||
for ( j = 0; j <= N; ++j ) { | ||
C[ i ][ j ] = -1; | ||
} | ||
} | ||
|
||
printf( "Cost: %li\n", CountRoutes( N, N ) ); | ||
return 0; | ||
} |