机器人走方格I

题目描述

有一个XxY的网格,一个机器人只能走格点且只能向右或向下走,要从左上角走到右下角。请设计一个算法,计算机器人有多少种走法。

给定两个正整数int x,int y,请返回机器人的走法数目。保证x+y小于等于12。

测试样例:
2,2
返回:2
class Robot {
public:
    int countWays(int x, int y) {
        // write code here
        if(x <= 0 || y <= 0) return 0;
        if(x == 1 || y == 1) return 1;
        return countWays(x-1,y)+countWays(x,y-1);
    }
};

 

posted on 2017-04-20 21:39  123_123  阅读(84)  评论(0编辑  收藏  举报