博客
关于我
Leetcode每日随机2021/3/5
阅读量:357 次
发布时间:2019-03-04

本文共 2026 字,大约阅读时间需要 6 分钟。

在这里插入图片描述

在这里插入图片描述

一看就是BFS的题。

但是地图太大了,纯bfs的话会超时。
题目中正好给了blocked的大小限制,我们就让遍历足够多的节点后停下来就行了。
我也比较快的想到这点了,但是这题还是把我恶心到了。
恶心的地方就是我用红框框出的部分,去你码的,骗人!
害老子找了那么久问题,cnm!
以后用例能不能认真写一下啊leetcode。

代码

class Solution {       private int max = (int) Math.pow(10, 6);	private int maxArea = 20000;	public boolean isEscapePossible(int[][] blocked, int[] source, int[] target) {   		for (int i = 0; i < blocked.length; i++) {   			if ((blocked[i][0] == source[0] && blocked[i][1] == source[1])					|| (blocked[i][0] == target[0] && blocked[i][1] == target[1])) {   				blocked[i][0] = -1;				blocked[i][1] = -1;			}		}		Set
v1 = new HashSet
(); Set
v2 = new HashSet
(); Queue
q1 = new LinkedList
(); Queue
q2 = new LinkedList
(); if (bfs(q1, v1, source, target, blocked) || bfs(q2, v2, target, source, blocked)) { return true; } if (v1.size() > maxArea && v2.size() > maxArea) { return true; } return false; } private boolean bfs(Queue
queue, Set
visited, int[] source, int[] target, int[][] blocked) { queue.offer(source); while (!queue.isEmpty()) { int[] temp = queue.poll(); if (visited.contains(temp[0] + "," + temp[1])) { continue; } visited.add(temp[0] + "," + temp[1]); if (visited.size() > maxArea) { break; } if (temp[0] == target[0] && temp[1] == target[1]) { return true; } if (validChild(blocked, temp[0] - 1, temp[1], visited)) { queue.offer(new int[] { temp[0] - 1, temp[1] }); } if (validChild(blocked, temp[0] + 1, temp[1], visited)) { queue.offer(new int[] { temp[0] + 1, temp[1] }); } if (validChild(blocked, temp[0], temp[1] - 1, visited)) { queue.offer(new int[] { temp[0], temp[1] - 1 }); } if (validChild(blocked, temp[0], temp[1] + 1, visited)) { queue.offer(new int[] { temp[0], temp[1] + 1 }); } } return false; } private boolean validChild(int[][] blocked, int x, int y, Set
visited) { if (x < 0 || x >= max || y < 0 || y >= max || visited.contains(x + "," + y)) { return false; } for (int[] i : blocked) { if (i[0] == x && i[1] == y) { return false; } } return true; }}

转载地址:http://fuee.baihongyu.com/

你可能感兴趣的文章
Mysql 分页语句 Limit原理
查看>>
MySql 创建函数 Error Code : 1418
查看>>
MySQL 创建新用户及授予权限的完整流程
查看>>
mysql 创建表,不能包含关键字values 以及 表id自增问题
查看>>
mysql 删除日志文件详解
查看>>
mysql 判断表字段是否存在,然后修改
查看>>
MySQL 到底能不能放到 Docker 里跑?
查看>>
mysql 前缀索引 命令_11 | Mysql怎么给字符串字段加索引?
查看>>
MySQL 加锁处理分析
查看>>
mysql 协议的退出命令包及解析
查看>>
mysql 参数 innodb_flush_log_at_trx_commit
查看>>
mysql 取表中分组之后最新一条数据 分组最新数据 分组取最新数据 分组数据 获取每个分类的最新数据
查看>>
MySQL 命令和内置函数
查看>>
MySQL 和 PostgreSQL,我到底选择哪个?
查看>>
mysql 四种存储引擎
查看>>
MySQL 在并发场景下的问题及解决思路
查看>>
MySQL 在控制台插入数据时,中文乱码问题的解决
查看>>
MySQL 基础架构
查看>>
MySQL 基础模块的面试题总结
查看>>
MySQL 处理插入重主键唯一键重复值办法
查看>>