zoukankan      html  css  js  c++  java
  • 华为OJ_1

    package com.cm.algorithm;
    
    /*迷宫问题*/
    
    import com.cm.utils.LogUtils;
    
    import java.util.Stack;
    
    public class Maze {
        private static final String TAG = "Maze";
        private static final boolean DEBUG = false;
    
        public static class Point {
            int x;
            int y;
            public int getX() {
                return this.x;
            }
            public int getY() {
                return this.y;
            }
            public Point(int x, int y) {
                this.x = x;
                this.y = y;
            }
            public boolean equal (Point p) {
                return this.x == p.x && this.y == p.y;
            }
        }
    
        /**
         * 获取x和y最长公共子序列
         * @param maze 地图 0 可以走 1不能走
         * @return 左上角到左下角的路线(数据保证唯一解)
         */
        public static Stack<Point> go(int[][] maze) {
            if(maze == null) {
                return new Stack<>();
            }
    
            int n = maze.length;
            int m = maze[0].length;
            int minL = n * m + 1; //记录当前最短值
            Stack<Point> nowRoute = new Stack<Point>();  //记录当前路线
            Stack<Point> result = new Stack<Point>();  //记录结果
            boolean[][] visited = new boolean[n][m]; //标记是否访问
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (maze[i][j] == 0) {
                        visited[i][j] = false;
                    } else {
                        visited[i][j] = true;
                    }
                }
            }
            int[][] dir = {{1, 0}, {0, 1}}; //定义四个方向
    
            Point out = new Point(n - 1, m - 1); //入口
            Point in = new Point(0, 0);  //出口
            visited[in.x][in.y] = true;
            nowRoute.push(in);
    
            while (!nowRoute.isEmpty()) {
                Point up = nowRoute.peek();
                if (up.equal(out)) {
                    if(DEBUG) {
                        LogUtils.beginDebugLog(TAG);
                        System.out.println("find result!");
                        LogUtils.endDebugLog(TAG);
                    }
                    if (nowRoute.size() < minL) {
                        writeResult(nowRoute, result);
                        minL = nowRoute.size();
                    }
                    nowRoute.pop();
                    if (!nowRoute.empty()) {
                        nowRoute.pop();
                    }
                    continue;
                }
                int dirNum = dir.length;
                boolean flag = true; //标注这个点是否需要弹出
                for(int i = 0; i < dirNum; i++) {
                    Point nextPoint = new Point(up.x + dir[i][0], up.y + dir[i][1]);
                    if (nextPoint.x >= 0 && nextPoint.x < n
                            && nextPoint.y >=0 && nextPoint.y < m
                            && !visited[nextPoint.x][nextPoint.y]) {
                        nowRoute.push(nextPoint);
                        visited[nextPoint.x][nextPoint.y] = true;
                        flag = false;
                        break;
                    }
                }
                if(flag) {
                    nowRoute.pop();
                }
             }
            return reverseStack(result);
        }
    
        /**
         * @param data
         * @param result
         */
        private static void writeResult(Stack<Point> data, Stack<Point> result) {
            result.clear();
            if (data != null) {
                for(Point point : data) {
                    result.push(new Point(point.x, point.y));
                }
            }
        }
    
        private static Stack<Point> reverseStack(Stack<Point> stack) {
            Stack<Point> reverseStack = new Stack<>();
            while (!stack.isEmpty()) {
                reverseStack.push(stack.pop());
            }
            return reverseStack;
        }
    }
  • 相关阅读:
    潭州课堂25班:Ph201805201 第十二课 new方法,定制属性访问,描述符与装饰器 (课堂笔记)
    潭州课堂25班:Ph201805201 第十一课 继承,多继承和魔术方法,属性和方法 (课堂笔记)
    Storm笔记
    java代码。继承。。。很戳我的心啊。。不太懂。super的真正用法
    java代码。从来没想过java里的继承是多么的难懂。如哲学
    java代码继承疑惑,请有心人解答
    java冒泡排序
    java数组复制
    java继承。顾不了
    java继承初级
  • 原文地址:https://www.cnblogs.com/xmu-chenming/p/9462789.html
Copyright © 2011-2022 走看看