heatherhb

Problem 15

Jun 5th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. long long calc [21][21] = {
  5.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  6.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  7.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  8.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  9.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  10.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  11.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  12.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  13.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  14.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  15.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  16.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  17.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  18.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  19.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  20.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  21.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  22.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  23.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  24.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
  25.     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
  26.     };
  27.  
  28. int dist(int rows, int cols) {
  29.     long long routes = 0;
  30.     long long left = -1;
  31.     long long right = -1;
  32.     if (rows == 0 && cols == 0) {       //distance (0,0)
  33.         routes = 0;
  34.         calc[rows][cols] = routes;
  35.     } else if (rows == 0 || cols == 0) {//distance (0,n) or (n,0)
  36.         routes = 1;
  37.         calc[rows][cols] = routes;
  38.     } else {                            //distance (n,n)
  39.         if (calc[rows][cols] == -1) {   //not calculated
  40.             left = calc[rows-1][cols];
  41.             right = calc[rows][cols-1];
  42.             if(left == -1) {//not calculated
  43.                 left = dist(rows-1, cols);
  44.                 calc[rows-1][cols] = left;
  45.             }
  46.             if(right == -1) {//not calculated
  47.                 right = dist(rows, cols-1);
  48.                 calc[rows][cols-1] = right;
  49.             }
  50.             routes = left + right;
  51.             calc[rows][cols] = routes;
  52.         } else {                        //calculated
  53.             routes = calc[rows][cols];
  54.         }
  55.     }
  56.     return routes;
  57. }
  58.  
  59. int main() {
  60.     cout << dist(20, 20) << endl;
  61.     cout << calc << endl;
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment