Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int minimumDistance = Integer.MIN_VALUE;
- int rowNbr[]
- = new int[]{-1, 0, 1, 0};
- int colNbr[]
- = new int[]{0, -1, 0, 1};
- HashSet<String> monsterArea;
- HashMap<String, TreeSet<Integer>> map;
- int findbestpath(int n, int m, int sr, int sc, int er, int ec, List<Integer> mr, List<Integer> mc) {
- map = new HashMap<>();
- monsterArea = new HashSet<>();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- for (int k = 0; k < mr.size(); k++) {
- int mcr = mr.get(k);
- int mcc = mc.get(k);
- monsterArea.add(mcr + "-" + mcc);
- if (mcr == i && mcc == j) {
- continue;
- }
- String str = i + "-" + j;
- int distance = Math.abs(i - mcr) + Math.abs(mcc - j);
- map.putIfAbsent(str, new TreeSet<>());
- map.get(str).add(distance);
- }
- }
- }
- dfs(n, m, sr, sc, mr, mc, er, ec, new HashSet<String>(), map.get(sr + "-" + sc).first());
- return minimumDistance == Integer.MIN_VALUE ? 0 : minimumDistance;
- }
- private void dfs(int n, int m, int sr, int sc, List<Integer> mr, List<Integer> mc, int er, int ec, HashSet<String> visited, int maxValue) {
- visited.add(sr + "-" + sc);
- if (sr == er && sc == ec) {
- minimumDistance = Math.max(minimumDistance, maxValue);
- }
- for (int i = 0; i < rowNbr.length; i++) {
- int nextRow = sr + rowNbr[i];
- int nextCol = sc + colNbr[i];
- String str = nextRow + "-" + nextCol;
- if (!visited.contains(str) && isValid(nextRow, nextCol, n, m) && !monsterArea.contains(str)) {
- dfs(n, m, nextRow, nextCol, mr, mc, er, ec, visited, Math.min(maxValue, map.get(str).first()));
- }
- }
- visited.remove(sr + "-" + sc);
- }
- private boolean isValid(int nextRow, int nextCol, int n, int m) {
- return nextRow >= 0 && nextRow < n && nextCol >= 0 && nextCol < m;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement