zoukankan      html  css  js  c++  java
  • 490. The Maze 迷宫踢足球

    There is a ball in a maze with empty spaces (represented as 0) and walls (represented as 1). The ball can go through the empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.

    Given the m x n maze, the ball's start position and the destination, where start = [startrow, startcol] and destination = [destinationrow, destinationcol], return true if the ball can stop at the destination, otherwise return false.

    You may assume that the borders of the maze are all walls (see examples).

     

    Example 1:

    Input: maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4]
    Output: true
    Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
    

    Example 2:

    Input: maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [3,2]
    Output: false
    Explanation: There is no way for the ball to stop at the destination. Notice that you can pass through the destination but you cannot stop there.
    

    Example 3:

    Input: maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], start = [4,3], destination = [0,1]
    Output: false

    忘记这种dfs怎么写了。一句话:边界感!

    https://leetcode.com/problems/the-maze/discuss/132857/Java-DFS-solution-beats-98

    int[][] steps = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        
        public boolean hasPath(int[][] maze, int[] start, int[] destination) {
            //DFS + cache
            int rows = maze.length;
            int cols = maze[0].length;
            boolean[][] visited = new boolean[rows][cols];
            
            return dfs(maze, start, destination, visited, rows, cols);
        }
        
        private boolean dfs(int[][] maze, int[] start, int[] des, boolean[][] visited, int rows, int cols) {
            if (visited[start[0]][start[1]]) {
                return false;
            }
            if (start[0] == des[0] && start[1] == des[1]) {
                return true;
            }
            
            visited[start[0]][start[1]] = true;
            for (int[] step : steps) {
                int dx = start[0];
                int dy = start[1];
                while (dx + step[0] >= 0 && dx + step[0] < rows && dy + step[1] >= 0 && dy + step[1] < cols && maze[dx + step[0]][dy + step[1]] != 1) {
                    dx += step[0];
                    dy += step[1];
                } 
                if (dfs(maze, new int[]{dx, dy}, des, visited, rows, cols)) {
                    return true;
                }
            }
            return false;
        }



  • 相关阅读:
    NND,优酷效果实在太差了
    技术人员创业的短板
    【上架通知】天轰穿.NET4趣味编程视频教程VS2010轻松学习C#零基础
    最新课程信息 课堂风格视频教程,中规中矩的教学思路设计和插诨打科的讲解方式
    Visual Studio 2010 Ultimate 中文旗舰版——提供下载地址和KEY
    整整半个月了
    学云网五一特惠活动,很喜感的图
    学员就业>我心纠结
    委托的定义和使用入门天轰穿
    第八 讲 : 流程控制循环语句 【天轰穿.Net4趣味编程系列视频教程vs2010轻松学习C#】
  • 原文地址:https://www.cnblogs.com/immiao0319/p/15195979.html
Copyright © 2011-2022 走看看