[Leetcode] 490. The Maze 解题报告

来源:互联网 发布:mongodb js脚本 编辑:程序博客网 时间:2024/06/05 00:13

题目

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling updownleft or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.

Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination.

The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.

Example 1

Input 1: a maze represented by a 2D array0 0 1 0 00 0 0 0 00 0 0 1 01 1 0 1 10 0 0 0 0Input 2: start coordinate (rowStart, colStart) = (0, 4)Input 3: destination coordinate (rowDest, colDest) = (4, 4)Output: trueExplanation: One possible way is : left -> down -> left -> down -> right -> down -> right.

Example 2

Input 1: a maze represented by a 2D array0 0 1 0 00 0 0 0 00 0 0 1 01 1 0 1 10 0 0 0 0Input 2: start coordinate (rowStart, colStart) = (0, 4)Input 3: destination coordinate (rowDest, colDest) = (3, 2)Output: falseExplanation: There is no way for the ball to stop at the destination.

Note:

  1. There is only one ball and one destination in the maze.
  2. Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
  3. The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
  4. The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.

思路

我开始把题目的意思给理解错了:必须是球能够停下来的地方才算是能够到达。如果球仅仅经过该格子,但是停不下来,那么就不算能够到达。这道题目用DFS或者BFS都可以解决,也算是DFS和BFS的一个小变种而已。由于能够停下来的位置及其有限,所以我们改用set来记录已经访问过的格子(当然用二维数组记录也是可以的)。此外,我们设计一个go2End函数,来计算球朝某个方向滚动时的下一个停靠点。下面就看看DFS和BFS分别怎么来做了。

1、DFS:对于DFS而言,我们首先判断是否已经到达了destination,如果是则直接返回;否则就看该位置是否已经被访问过了,如果是则返回,否则就记当前位置已经被访问(如果没有这一步,将会导致DFS无法结束)。然后就分别用DFS尝试四个不同方向,有任何一个方向可以到达destination就直接返回true。

2、BFS:对于BFS而言,思路类似。我们首先建立一个队列,并且将start压入队列。每次从队列头部拿出当前位置。如果该位置是destination则直接返回,否则就看该位置是否还没有被访问,如果没有被访问,就置为已访问,并将其四个方向上移动可以停靠的点加入队列。如果队列都空了还没有发现可以到达的路径,那么就说明无法到达了。

代码

1、DFS:

class Solution {public:    bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {        set<vector<int>> visited;        return DFS(maze, start, destination, visited);    }private:    bool DFS(vector<vector<int>> &maze, vector<int> &start, vector<int> &destination, set<vector<int>> &visited) {        if(start == destination) {            return true;        }        if(visited.find(start) != visited.end()) {      // already visited            return false;        }        visited.insert(start);        vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};        for(int i = 0; i < 4; ++i) {            vector<int> res = go2End(maze, start, dirs[i]);            if(res == destination || DFS(maze, res, destination, visited)) {                return true;            }        }        return false;    }    vector<int> go2End(vector<vector<int>> &maze, vector<int> start, vector<int> &dir) {        vector<int> new_start = {start[0] + dir[0], start[1] + dir[1]};        int row_num = maze.size(), col_num = maze[0].size();        if(new_start[0] < 0 || new_start[0] >= row_num || new_start[1] < 0 || new_start[1] >= col_num ||           maze[new_start[0]][new_start[1]] == 1) {            return start;        }        return go2End(maze, new_start, dir);    }};

2:BFS:

class Solution {public:    bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {        set<vector<int>> visited;        queue<vector<int>> q;        q.push(start);        vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};        while (!q.empty()) {            vector<int> pos = q.front();            q.pop();            if (pos == destination) {                return true;            }            if (visited.find(pos) == visited.end()) {   // not visited yet                visited.insert(pos);                for(int i = 0; i < 4; ++i) {                    vector<int> res = go2End(maze, pos, dirs[i]);                    q.push(res);                }            }        }        return false;    }private:    vector<int> go2End(vector<vector<int>> &maze, vector<int> start, vector<int> &dir) {        vector<int> new_start = {start[0] + dir[0], start[1] + dir[1]};        int row_num = maze.size(), col_num = maze[0].size();        if(new_start[0] < 0 || new_start[0] >= row_num || new_start[1] < 0 || new_start[1] >= col_num ||           maze[new_start[0]][new_start[1]] == 1) {            return start;        }        return go2End(maze, new_start, dir);    }};

阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 铁路局的门面乱收房租怎么办 酒店夜审房费多过怎么办 夜审房价录多了怎么办 做工地拿不到钱怎么办 赢了官司拿不到钱怎么办 工地上拿不到钱怎么办 做了工拿不到钱怎么办 高速公路上车没油了怎么办 高铁乘务员年龄大了怎么办 总公司跑路了分公司怎么办 坐车久了耳朵懵怎么办 过完隧道耳朵疼怎么办 护照还在大使馆需要出国怎么办 护照在大使馆不返回怎么办 美国面签迟到了怎么办 成都美签迟到了怎么办 签证电调没人接怎么办 单位没有抬头纸怎么办在职证明 出国签证无银行流水怎么办 铁路职工得癌症后工作怎么办 去泰国不会泰语和英语怎么办 签证状态一直没有更新怎么办 简理财不能身份信息确认怎么办 德国领事馆没有收到预约邮件怎么办 父母一方带孩子英国签证怎么办 去韩国自由行签证怎么办 韩国自由行签证的该怎么办 法院离婚判决书没了怎么办 离婚判决书对方没收到怎么办 法院判离怎么办离婚证 判决书下来后没钱还怎么办 拿调解书怎么办离婚证 判决书生效后对方拒不履行怎么办 收到民事判决公告该怎么办 苹果手机gps信号弱怎么办 二审败诉后拿到判决书怎么办 农商行房贷逾期一天怎么办 农商行房贷逾期怎么办 农发行车改司机怎么办 混泥土地泵排量不稳怎么办 改嫁上海老公孩子的户口怎么办