您的位置:首页 > 其它

美团校招 拜访

2017-04-27 17:46 253 查看
题目描述

现在有一个城市销售经理,需要从公司出发,去拜访市内的商家,已知他的位置以及商家的位置,但是由于城市道路交通的原因,他只能在左右中选择一个方向,在上下中选择一个方向,现在问他有多少种方案到达商家地址。

给定一个地图map及它的长宽n和m,其中1代表经理位置,2代表商家位置,-1代表不能经过的地区,0代表可以经过的地区,请返回方案数,保证一定存在合法路径。保证矩阵的长宽都小于等于10。

测试样例:

[[0,1,0],[2,0,0]],2,3

返回:2

Method one

Because the sales manager can only choose one direction in horizontal and vertical separately, the distance walking through is identical, namely, it’s the Manhattan Distance.

Intuitively, I solve this problem by analogy with graph theory: Find the number of different acyclic paths connecting two points in a given graph G = < V, E >. Hence, Depth First Algorithm or Breadth First Algorithm may solve this problem. Specific to this problem, A set to record the visited or not visited vertices is unnecessary, since no vertex will be accessed repeatedly when visited along one direction.

Pseudo-code to solve the problem

stk: a stack to storage the position of the manager

origin: the original position of the manager

dest: the destination

cnt: the number of acyclic paths

# push original position into the stack
stk.push(origin)
# repeat when there is an element in the stack
while(!stk.empty())
# pop out the element at the top
top = stk.pop()
# if top is the destination, cnt increases by one
if(top == dest)
cnt++
# push into the position of the next step in horizontal and vertical,
# if its value is unequal to -1 and its position doesn't exceed the destination.
if(satisfy the above criterion)
stk.push(next step in horizontal and vertical)


Source-code to solve the problem in java

import java.util.*;

class Position{
int x;
int y;
static int[][] map;
static Position origin;
static Position dest;
static int dx;
static int dy;
//to determine the direction
static void init(){
dx = dy = 1;
if(origin.x > dest.x)
dx = -1;
if(origin.y > dest.y)
dy = -1;
}
//next step in horizontal
Position movex(){
int x_temp = x + dx;
if((x_temp - dest.x) * dx > 0)
return null;
if(map[x_temp][y] == -1)
return null;
return new Position(x_temp, y);
}
//next step in vertical
Position movey(){
int y_temp = y + dy;
if((y_temp - dest.y) * dy > 0)
return null;
if(map[x][y_temp] == -1)
return null;
return new Position(x, y_temp);
}
//to determine whether to reach the destination
boolean isArrived(){
if(x == dest.x && y == dest.y)
return true;
return false;
}
Position(int x, int y){
this.x = x;
this.y = y;
}
}
public class CountPath {
public static int count_Path(int[][] map, int n, int m) {
Position.map = map;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(map[i][j] == 1){
Position.origin = new Position(i, j);
}else if(map[i][j] == 2){
Position.dest = new Position(i, j);
}
Position.init();
Stack<Position> stk = new Stack<Position>();
stk.push(Position.origin);
int cnt = 0;
while(!stk.empty()){
Position top = stk.pop();
if(top.isArrived()){
cnt++;
continue;
}
Position mxp = top.movex();
Position myp = top.movey();
if(mxp != null)
stk.push(mxp);
if(myp != null)
stk.push(myp);
}
return cnt;
}
public static void main(String[] args) {
int[][] map = {{0, 1, 0}, {2, 0, 0}};
int n = 2, m = 3;
System.out.println(count_Path(map, n,m));
}
}


Complexity analysis

Δx denotes the horizontal distance between the manager and the merchant, while Δy denotes the vertical distance. Then, the time complexity is o(2Δx+Δy), and the space complexity is o(Δx∗Δy).

Method two

Obviously, the above method is time-consuming in time complexity, but it can record the path if you use an array to mark every pop-out element. Since there’s no request for the output of each path, we can apply dynamic programming to address this issue. dx,dy denote the distance that a single step moves in horizontal and vertical separately, and ps[i][j] denotes the number of paths from original position to the current position (i,j). The previous position before (i,j) is (i−dx,j) or (i,j−dy), so ps[i][j]=ps[i−dx][j]+ps[i][j−dy].

Source-code in java

public int countPath(int[][] map, int n, int m) {
int mX = 0, mY = 0;//position of manager
int sX = 0, sY = 0;//position of merchant
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == 1) {
mX = i;
mY = j;
continue;
}
if (map[i][j] == 2) {
sX = i;
sY = j;
}
}
}
//determine the direction
int diffX = mX < sX ? 1 : -1;
int diffY = mY < sY ? 1 : -1;
//ps[i][j] = ps[i-dx][j] + ps[i][j-dy]
for (int i = mX + diffX; i != sX + diffX; i += diffX) {
map[i][mY] = map[i][mY] == -1 ? 0 : map[i - diffX][mY];
}
for (int i = mY + diffY; i != sY + diffY; i += diffY) {
map[mX][i] = map[mX][i] == -1 ? 0 : map[mX][i-diffY];
}
for (int i = mX + diffX; i != sX+diffX ; i+=diffX) {
for (int j = mY + diffY; j != sY + diffY ; j+=diffY) {
map[i][j] = map[i][j] == -1 ? 0 : map[i-diffX][j] + map[i][j-diffY];
}
}
return map[sX][sY];
}


Complexity analysis

Time Complexity: o(Δx∗Δy)

Space Complexity: o(Δx∗Δy)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  美团 动态规划