度小满笔试

我是Android岗哈 可能跟算法岗的各位题目有些不一样

我忘了题目是什么了
我A了第二题 就是最少要走多少步的题
比较典型的bfs,就是注意下坐标的转换就好了
import java.util.LinkedList;
import java.util.Scanner;

class Node{
    public int x;
    public int y;
    public int step;

    public Node(int x, int y, int step) {
        this.x = x;
        this.y = y;
        this.step = step;
    }
}

public class Main{
    private static final int MAX = 1005;
    private static final int BASE = 500;

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        boolean[][] visited = new boolean[MAX][MAX];
        int x = scanner.nextInt() + BASE;
        int y = scanner.nextInt() + BASE;
        int n = scanner.nextInt();
        for(int i = 0; i < n; i++){
            int px = scanner.nextInt() + BASE;
            int py = scanner.nextInt() + BASE;
            visited[px][py] = true;
        }
        scanner.close();

        System.out.println(bfs(visited, x, y));
    }

    private static int bfs(boolean[][] visited, int ex, int ey){
        int[][] dirs = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

        visited[BASE][BASE] = true;

        LinkedList<Node> queue = new LinkedList<>();
        queue.add(new Node(BASE, BASE, 0));

        while (!queue.isEmpty()){
            Node node = queue.pollFirst();

            for(int[] dir : dirs){
                int x = node.x + dir[0];
                int y = node.y + dir[1];

                if(x < 0 || x > MAX || y < 0 || y > MAX)
                    continue;

                if(visited[x][y])
                    continue;

                if(x == ex && y == ey)
                    return node.step + 1;

                visited[x][y] = true;
                queue.add(new Node(x, y, node.step + 1));
            }
        }

        return -1;
    }

}

第一题 就是问有多少走法的一道题
我暴力 0.36😂

#笔试题目##度小满#
全部评论
递归
点赞 回复
分享
发布于 2019-09-15 20:31
我也是bfs,也是用的boolean数组,然而只通过0.45,告诉我其他的内存超过。。。
点赞 回复
分享
发布于 2019-09-15 20:34
联易融
校招火热招聘中
官网直投
第二题,我dfs超时了,才过了18%
点赞 回复
分享
发布于 2019-09-15 20:35
我的直接栈溢出了,很多边界条件没判断
点赞 回复
分享
发布于 2019-09-15 20:36
Android选择题,以下不属于Android五大布局的是什么啊,感觉每个选项都属于啊😥😥😥
点赞 回复
分享
发布于 2019-09-15 21:10

相关推荐

投递腾讯等公司10个岗位
点赞 评论 收藏
转发
1 11 评论
分享
牛客网
牛客企业服务