网易牢笼问题 70%

2018-08-09  本文已影响0人  portability

网易大哥请告诉我哪里是出口?

import java.util.*;

public class MiGong {

    static class Step{
        public int dx;
        public int dy;

        public Step(int dx, int dy) {
            this.dx = dx;
            this.dy = dy;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Step step = (Step) o;
            return dx == step.dx &&
                    dy == step.dy;
        }

        @Override
        public int hashCode() {

            return Objects.hash(dx, dy);
        }
    }

    private static int m, n, sx, sy, tx, ty;
    private static List<Step> step_list = new ArrayList<>();
    private static List<String> room = new ArrayList<>();
    private static Map<String, Integer> stepIntegerMap = new HashMap<>();

    private static void getMaxStepsSolution(){
        Queue<Step> stepQueue = new LinkedList<>();
        stepQueue.offer(new Step(sx, sy));
        stepIntegerMap.put(sx + " " + sy, 0);
        stepIntegerMap.put((m - 1) + " " + (n - 1), Integer.MAX_VALUE);
        int pos_x, pos_y;
        while (!stepQueue.isEmpty()){
            Step step = stepQueue.poll();
            //到达终点
            if(step.dx == m - 1 && step.dy == n - 1){
                continue;
            }else{
                for (Step step1
                        :
                        step_list){
                    pos_x = step.dx + step1.dx;
                    pos_y = step.dy + step1.dy;
                    if(pos_x < 0 || pos_x > m - 1 || pos_y < 0 || pos_y > n -1){
                        continue;
                    }
                    if( room.get(pos_x).charAt(pos_y) == '.'){

                        if(stepIntegerMap.containsKey(pos_x + " " + pos_y)){
                            if (stepIntegerMap.get(pos_x + " " + pos_y) > stepIntegerMap.get(step.dx + " " + step.dy) + 1){
                                stepIntegerMap.put(pos_x + " " + pos_y, stepIntegerMap.get(step.dx + " " + step.dy) + 1);
                                stepQueue.offer(new Step(pos_x, pos_y));
                            }
                        }else{
                            stepIntegerMap.put(pos_x + " " + pos_y, stepIntegerMap.get(step.dx + " " + step.dy
                            ) + 1);
                            stepQueue.offer(new Step(pos_x, pos_y));
                        }

                    }
                }
            }
        }
    }
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        m = scanner.nextInt();
        n = scanner.nextInt();
        for(int i = 0; i < m; i++){
            String string = scanner.next();
            room.add(string);
            int j = string.indexOf('.');
            if(j != -1){
                stepIntegerMap.put(i + " " + j, Integer.MAX_VALUE);
            }
        }
        sx = scanner.nextInt();
        sy = scanner.nextInt();
        int steps = scanner.nextInt();
        int i = 0;
        int dx, dy;
        while(i < steps){
            dx = scanner.nextInt();
            dy = scanner.nextInt();
            Step step = new Step(dx,dy);
            step_list.add(step);
            i++;
        }
        getMaxStepsSolution();
        int result = 0;
        for (String key :
                stepIntegerMap.keySet()){
            if (stepIntegerMap.get(key) == Integer.MAX_VALUE){
                System.out.println(-1);
                return;
            }
            result = Math.max(stepIntegerMap.get(key), result);
        }
        System.out.println(result);
    }
}

上一篇下一篇

猜你喜欢

热点阅读