美团校招 拜访

题目描述
现在有一个城市销售经理,需要从公司出发,去拜访市内的商家,已知他的位置以及商家的位置,但是由于城市道路交通的原因,他只能在左右中选择一个方向,在上下中选择一个方向,现在问他有多少种方案到达商家地址。
给定一个地图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 (idx,j) or (i,jdy), so ps[i][j]=ps[idx][j]+ps[i][jdy].

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)

先展示下效果 https://pan.quark.cn/s/5061241daffd 在使用Apache HttpClient库发起HTTP请求的过程中,有可能遇到`HttpClient`返回`response`为`null`的现象,这通常暗示着请求未能成功执行或部分资源未能得到妥善处理。 在本文中,我们将详细研究该问题的成因以及应对策略。 我们需要掌握`HttpClient`的运作机制。 `HttpClient`是一个功能强大的Java库,用于发送HTTP请求并接收响应。 它提供了丰富的API,能够处理多种HTTP方法(例如GET、POST等),支持重试机制、连接池管理以及自定义请求头等特性。 然而,一旦`response`对象为`null`,可能涉及以下几种情形:1. **连接故障**:网络连接未成功建立或在请求期间中断。 需要检查网络配置,确保服务器地址准确且可访问。 2. **超时配置**:若请求超时,`HttpClient`可能不会返回`response`。 应检查连接和读取超时设置,并根据实际需求进行适当调整。 3. **服务器故障**:服务器可能返回了错误状态码(如500内部服务器错误),`HttpClient`无法解析该响应。 建议查看服务器日志以获取更多详细信息。 4. **资源管理**:在某些情况下,如果请求的响应实体未被正确关闭,可能导致连接被提前释放,进而使后续的`response`对象为`null`。 在使用`HttpClient 3.x`版本时,必须手动调用`HttpMethod.releaseConnection()`来释放连接。 而在`HttpClient 4.x`及以上版本中,推荐采用`EntityUtils.consumeQuietly(respons...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值