网格,最左上角顶点的坐标是(1,1),机器人从(1,1)出发,一次走一格,只能往下或往右走,问到(x,y)有几种走法?
设f(x,y)是到(x,y)的走法总数,则
当x = 1时,f(x,y) = 1
当y = 1时,f(x,y) = 1
当x >1 且 y > 1时,f(x,y) = f(x -1,y) + f(x,y-1)
int Num(int x,int y) { int i,j; if(x < 1 || y < 1) return -1; int dp[x][y]; for(j = 0;j < y;++j) { dp[0][j] = 1; } for(i = 0;i < x;++i) { dp[i][0] = 1; } for(i = 1;i < x;++i) { for(j = 1;j < y; ++j) { dp[i][j] = dp[i-1][j]+dp[i][j-1]; } } return dp[x-1][y-1]; }