Robots Walk
/***
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.
IDEA: start from (X,Y), recursive until get (0, 0). each move has two options, (X-1, Y) or (X, Y-1).
***/
#include<iostream>
#include<vector>
#include <algorithm>
#include <climits>
using namespace std;
class point {
public:
int x;
int y;
point(int x, int y) {
this->x = x;
this->y = y;
}
};
int waysback(int x, int y) {
if(x == 0 && y==0) return 1;
if(x < 0 || y < 0) return 0;
return waysback(x-1, y) + waysback(x, y-1);
}
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.
IDEA: start from (X,Y), recursive until get (0, 0). each move has two options, (X-1, Y) or (X, Y-1).
***/
#include<iostream>
#include<vector>
#include <algorithm>
#include <climits>
using namespace std;
class point {
public:
int x;
int y;
point(int x, int y) {
this->x = x;
this->y = y;
}
};
int waysback(int x, int y) {
if(x == 0 && y==0) return 1;
if(x < 0 || y < 0) return 0;
return waysback(x-1, y) + waysback(x, y-1);
}
Comments
Post a Comment