题目描述:设想有个机器人坐在X*Y网格的左上角,只能向右向下移动。机器人从(0,0)开始出发,到(X,Y)共有多少种方法。
思路:到i,j只和,(i-1,j)和(i,j-1)有关
递归的时候加备忘
1 #include <iostream> 2 #include <queue> 3 #include <climits> 4 #include <algorithm> 5 #include <memory.h> 6 #include <stdio.h> 7 #include <ostream> 8 #include <vector> 9 #include <list> 10 #include <cmath> 11 #include <string> 12 #include <stdexcept> 13 #include <stack> 14 using namespace std; 15 16 int gridPaths[1000][1000]; 17 18 int fun(int x,int y) 19 { 20 if(x == 0 || y == 0) 21 { 22 gridPaths[x][y] = 1; 23 return gridPaths[x][y]; 24 } 25 if(gridPaths[x][y] > 0) 26 return gridPaths[x][y]; 27 else 28 { 29 gridPaths[x][y] = fun(x-1,y)+fun(x,y-1); 30 return gridPaths[x][y]; 31 } 32 } 33 34 int main() 35 { 36 memset(gridPaths,-1,sizeof(gridPaths)); 37 int x = 10; 38 int y = 10; 39 cout<<fun(x,y); 40 return 0; 41 }
思考:如果加入禁区点,怎么找一条路径?