[CareerCup] 9.2 Robot Moving 机器人移动

9.2 Imagine a robot sitting on the upper left corner of an X by Y grid. The robot can only move in two directions: right and down. How many possible paths are there for the robot to go from (0,0) to (X,Y)?
 FOLLOW UP
 Imagine certain spots are "off limits," such that the robot cannot step on them. Design an algorithm to find a path for the robot from the top left to the bottom right.

LeetCode上的原题,请参见我之前的博客Unique Paths 不同的路径Unique Paths II 不同的路径之二

解法一:

class Solution {
public:
    int getPath(int x, int y) {
        vector<int> dp(y + 1, 1);
        for (int i = 1; i <= x; ++i) {
            for (int j = 1; j <= y; ++j) {
                dp[j] += dp[j - 1];
            }
        }
        return dp[y];
    }
};

解法二:

class Solution {
public:
    int getPath(int x, int y) {
        double num = 1, denom = 1;
        int small = x < y ? x : y;
        for (int i = 1; i <= small; ++i) {
            num *= x + y - i + 1;
            denom *= i;
        }
        return (int)(num / denom);
    }
};
时间: 2024-10-14 14:17:02

[CareerCup] 9.2 Robot Moving 机器人移动的相关文章

UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)

UVA 1600 Patrol Robot Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The colu

java机器人API学习笔记

robocode 部分 API 中文参考 ahead 向前 public void ahead(double distance) Immediately moves your robot ahead (forward) by distance measured in pixels. 马上将你的机器人向前移动 以 distance 指定的 多少个像素 This call executes immediately, and does not return until it is complete,

寒假训练——搜索——C - Robot

The Robot Moving Institute is using a robot in their local store to transport different items. Of course the robot should spend only the minimum time necessary when travelling from one place in the store to another. The robot can move only along a st

微信接入机器人实现对别人消息和群at消息的自动回复

微信接入机器人实现对别人消息和群at消息的自动回复 有时候,我们想让我们的微信号对别人发出的各种消息做出回复.我们可以通过接入图灵机器人的方式实现. IDLE编写py文件并保存,命名为wxbot. #!/usr/bin/env python # coding: utf-8 import os import sys import webbrowser import pyqrcode import requests import json import xml.dom.minidom import

POJ 1736 Robot BFS

Robot Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7561   Accepted: 2444 Description The Robot Moving Institute is using a robot in their local store to transport different items. Of course the robot should spend only the minimum time

一个控制台程序,模拟机器人对话

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace RobotTest { class Program { static void Main(string[] args) { 机器人 robot = new 机器人(); r

POJ 1376 Robot

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7866   Accepted: 2586 Description The Robot Moving Institute is using a robot in their local store to transport different items. Of course the robot should spend only the minimum time neces

robocode 机器人编码

robocode robot code 机器人编码 battle 战斗 warning:the battle you are about to start is vrey large and will consume a lot of cpu and memory do you wish to proceed 警告:你即将开始的战斗是非常大的,会消耗大量的CPU和内存是否继续 Please wait while Robocode sets up a compiler for you... Set

CareerCup All in One 题目汇总

Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation String 1.4 Replace Spaces 1.5 Compress String 1.6 Rotate Image 1.7 Set Matrix Zeroes 1.8 String Rotation Chapter 2. Linked Lists 2.1 Remove Duplicates