Commit 8064bff9 by Andrew Dahl

Solved Problem 15

parent 2c1d4756
Showing with 57 additions and 0 deletions
Question:
Starting in the top left corner of a 2x2 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 20x20 grid?
Answer: 137846528820
#include <iostream>
#include <map>
using namespace std;
int size = 21;
map<int,map<int,long int> > route_grid;
long int find_route(map<int,map<int,int> > grid, int x, int y, bool test=false)
{
if(x < 0 || x >= size || y < 0 || y >= size || grid[x][y] == 1)
return 0;
grid[x][y] = 1;
if(x == (size-1) && y == (size-1))
return 1;
if(route_grid[x][y] == 0)
return find_route(grid, x+1, y) + find_route(grid, x, y+1);
return route_grid[x][y];
}
int main() {
map<int,map<int,int> > grid;
for(int i=0; i < size; i++)
for(int j=0; j < size; j++)
{
grid[i][j] = 0;
route_grid[i][j] = 0;
}
for(int i = size - 2; i >= 0; i--)
for(int j = size - 2; j >=0; j--)
route_grid[i][j] = find_route(grid,i,j,true);
cout << route_grid[0][0] << endl;
/*for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
cerr << " " << route_grid[i][j];
cerr << endl;
}*/
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment