码迷,mamicode.com
首页 > 其他好文 > 详细

机器人走方格I

时间:2017-04-20 23:22:18      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:strong   item   style   网格   blog   color   pre   write   return   

题目描述

有一个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);
    }
};

 

机器人走方格I

标签:strong   item   style   网格   blog   color   pre   write   return   

原文地址:http://www.cnblogs.com/xiuxiu55/p/6740968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!