地下迷宫 Java 题解

地下迷宫

http://www.nowcoder.com/questionTerminal/571cfbe764824f03b5c0bfd2eb0a8ddf

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // input n m and energy
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int energy = scanner.nextInt();
        // input nums
        int[][] nums = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                nums[i][j] = scanner.nextInt();
            }
        }
        Main main = new Main();
        main.search(nums, n, m, energy);
    }

    private void search(int[][] nums, int n, int m, int energy) {
        ArrayList<Pair> result = new ArrayList<>();
        int resultEnergy = search(nums, n, m, energy, 0, 0, result);
        if(resultEnergy == -1){
            System.out.println("Can not escape!");
        }else{
            for (int i = 0; i < result.size(); i++) {
                System.out.print(result.get(i).toString());
                if (i != result.size() - 1) {
                    System.out.print(",");
                }
            }
        }
    }

    private int search(int[][] nums, int n, int m, int energy, int x, int y, ArrayList<Pair> result) {
        if (energy < 0 || x < 0 || x > n - 1 || y < 0 || y > m - 1 || nums[x][y] == 0) {
            return -1;
        } else {
            result.add(new Pair(x, y));
            if (x == 0 && y == m - 1) {
                return energy;
            } else {
                int leftMaxEnergy = -1;
                List<Pair> resultList = new ArrayList<>();
                nums[x][y] = 0;
                // up
                ArrayList<Pair> upResult = new ArrayList<>();
                int afterUp = search(nums, n, m, energy - 3, x - 1, y, upResult);
                if (afterUp > leftMaxEnergy) {
                    resultList = upResult;
                    leftMaxEnergy = afterUp;
                }
                // down
                ArrayList<Pair> downResult = new ArrayList<>();
                int afterDown = search(nums, n, m, energy, x + 1, y, downResult);
                if (afterDown > leftMaxEnergy) {
                    resultList = downResult;
                    leftMaxEnergy = afterDown;
                }
                // left
                ArrayList<Pair> leftResult = new ArrayList<>();
                int afterLeft = search(nums, n, m, energy - 1, x, y - 1, leftResult);
                if (afterLeft > leftMaxEnergy) {
                    resultList = leftResult;
                    leftMaxEnergy = afterLeft;
                }
                // right
                ArrayList<Pair> rightResult = new ArrayList<>();
                int afterRight = search(nums, n, m, energy - 1, x, y + 1, rightResult);
                if (afterRight > leftMaxEnergy) {
                    resultList = rightResult;
                    leftMaxEnergy = afterRight;
                }
                nums[x][y] = 1;
                result.addAll(resultList);
                return leftMaxEnergy;
            }
        }
    }
}

class Pair {
    private Integer key;
    private Integer value;

    public Pair(Integer key, Integer value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public String toString() {
        return "[" + key + "," + value + "]";
    }
}
全部评论

相关推荐

HR_丸山彩同学:你的项目描述里,系统设计讲了很多:MemCube是什么、三级存储架构怎么设计、四种遗忘策略分别是什么。这些面试的时候讲没问题,但简历上不需要这么细。 简历要突出的是影响力,不是实现细节。面试官看简历的时候想知道的是「这个项目有多大价值」,不是「这个项目具体怎么实现的」。实现细节是面试时候聊的 怎么改:技术细节可以精简为一句「采用三级存储架构+四种遗忘策略」,把省出来的篇幅用来写影响力。比如:项目有没有开源?有没有写成技术博客?有没有被别人使用过? 校园经历没有任何信息量,任何人都可以写这句话,写了等于没写。更关键的是,你投的是技术岗,校园活动经历本来就不是加分项。如果非要写,必须写出具体的数字和成果。如果你没有这些数字,那就老老实实删掉 「端到端耗时缩减30-40%」要给出确切数字和绝对值。从1000ms降到600ms是降了40%,从100ms降到60ms也是降了40%,但这两个含义完全不一样。其他也是,涉及到数据,准备好证据,口径统一,面试会问 「熟练」「熟悉」「了解」混在一起用,读起来很乱。而且「了解前端需求」最好改成「具备前后端协作经验」
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务