[LeetCode] 01 Matrix 题解
题意
思路
我一开始的时候想的是最简单的方法,就是遍历所有的值为1的元素,再根据其为起点进行BFS,计算层数,但是这个方法超时了;
其实,可以不用从1开始遍历,从0开始遍历,找到和值为1相邻的0,将其的层数设置为1就行了,为什么可以不用从1开始,因为并没有要求从规定的起点到指定的位置,计算最小距离,而是计算一整个周围,只要周围存在1,则将其加入到队列,计算相应的距离(又可能存在别多个1包围的1的情况),注意的是,在访问过1的结点后下次不可以再进行计算。
实现
//
//
#include "../PreLoad.h"
class Solution {
public:
/**
* 三重循环,最外层为所有1的结点,里面两层是实现BFS
* 导致时间复杂度过高,待优化
* @param matrix
* @return
*/
vector< vector<int>> layouts = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
vector<vector<int>> result(matrix);
if (matrix.size() == 0) {
return result;
}
size_t row = matrix.size();
size_t col = matrix[0].size();
deque<pair<int, int>> queues;
vector<vector<int>> visited(row, vector<int>(col , 0));
bool isHaveOne = false;
for (size_t i = 0; i < row; i++) {
for (size_t j = 0; j < col; j++) {
if (matrix[i][j]) {
queues.push_back({i, j});
isHaveOne = true;
}
}
}
if (!isHaveOne) {
return result;
}
while (!queues.empty()) {
auto content = queues.front();
queues.pop_front();
bool found = false;
int level = 0;
deque<pair<int, int>> tqueue;
tqueue.push_back(content);
vector<vector<int>> tvisited(visited);
tvisited[content.first][content.second] = 1;
while (!found && !tqueue.empty()) {
level++;
int queue_len = tqueue.size();
// 保证队列中的每个数都能加上基本的平方数
for (int i = 0; i < queue_len; i++) {
auto tcontent = tqueue.front();
tqueue.pop_front();
for (auto temp : layouts) {
int newx = tcontent.first + temp[0];
int newy = tcontent.second + temp[1];
if (newx < 0 || newy < 0 || newx >= row || newy >= col || tvisited[newx][newy]) {
continue;
}
if (!matrix[newx][newy]) {
found = true;
break;
}
tvisited[newx][newy] = 1;
tqueue.push_back({newx, newy});
}
}
}
tvisited = visited;
result[content.first][content.second] = level;
}
return result;
}
// 做法错误
vector<vector<int>> updateMatrix2(vector<vector<int>>& matrix) {
vector<vector<int>> result(matrix);
if (matrix.size() == 0) {
return result;
}
size_t row = matrix.size();
size_t col = matrix[0].size();
deque<pair<int, int>> queues;
bool isHaveOne = false;
for (size_t i = 0; i < row; i++) {
for (size_t j = 0; j < col; j++) {
if (matrix[i][j]) {
queues.push_back({i, j});
isHaveOne = true;
}
}
}
if (!isHaveOne) {
return result;
}
vector<vector<int>> visited(row, vector<int>(col , 0));
vector<vector<int>> tvisited(visited);
while (!queues.empty()) {
auto content = queues.front();
queues.pop_front();
int level = 0;
bool found = false;
DFSHelper(matrix, result, visited, level, content.first, content.second, row, col, found);
tvisited = visited;
//result[content.first][content.second] = level;
}
return result;
}
// 无法保证取得的路径是最短的,因为是深度递归,所以有可能找的那条路径全都是1的,所以这样使用DFS是错误的
void DFSHelper(vector<vector<int>>& matrix, vector<vector<int>>& result, vector<vector<int>>& visited,
int& dis, int x, int y, int row, int col, bool& found) {
if (found) {
return ;
}
dis++;
visited[x][y] = 1;
if (!matrix[x][y]) {
result[x][y] = dis;
found = true;
return ;
}
else {
for (auto temp : layouts) {
int newx = x + temp[0];
int newy = y + temp[1];
if (found) {
return ;
}
if (newx < 0 || newy < 0 || newx >= row || newy >= col || visited[newx][newy]) {
continue;
}
DFSHelper(matrix, result, visited, dis, newx, newy, row, col, found);
if (found) {
return ;
}
}
}
dis--;
visited[x][y] = 0;
}
/**
* 将二维数组中为0的加入到队列中,1的则置为-1
* 因为必然存在和1的元素相邻的元素0,所以当找到这样的周围是1的0时
* 则把这个1的元素同样加入到队列中,因为可能会存在被1包围的1
* 同时设置其距离,这个则需要将其值设为初始元素的值(同样是1的元素)+1,
* 这个时候其的值不再是-1,同时起到了纪录其已经访问过了的作用
*
* 可以理解为如果上一个(初始位置)如果是0,则说明其在周围,自然为1
* 但是也会碰到被1包围的1,同样根据上面计算出来的1去计算后面的1的元素
* 有些dp的思想
*
* @param matrix
* @return
*/
vector<vector<int>> updateMatrix3(vector<vector<int>>& matrix) {
vector<vector<int>> result(matrix);
if (matrix.size() == 0) {
return result;
}
size_t row = matrix.size();
size_t col = matrix[0].size();
typedef pair<int, int> tp;
deque<pair<int, int>> queues;
for (size_t i = 0; i < row; i++) {
for (size_t j = 0; j < col; j++) {
if (matrix[i][j] == 0) {
queues.push_back(tp(i, j));
}
else {
result[i][j] = -1;
}
}
}
while (!queues.empty()) {
auto content = queues.front();
queues.pop_front();
for (auto temp : layouts) {
int newx = content.first + temp[0];
int newy = content.second + temp[1];
if (newx >= 0 && newx < row && newy >= 0 && newy < col && result[newx][newy] == -1) {
result[newx][newy] = result[content.first][content.second] + 1; //注意不是自增
queues.push_back({newx, newy});
}
}
}
return result;
}
// 计算层数,并将其设置为负数,作为访问过的标记
vector<vector<int>> updateMatrix4(vector<vector<int>>& matrix) {
if (matrix.size() == 0) {
return matrix;
}
size_t row = matrix.size();
size_t col = matrix[0].size();
typedef pair<int, int> tp;
deque<pair<int, int>> queues;
for (size_t i = 0; i < row; i++) {
for (size_t j = 0; j < col; j++) {
if (matrix[i][j] == 0) {
queues.push_back(tp(i, j));
}
}
}
int dis = 0;
while (!queues.empty()) {
dis++;
int queue_len = queues.size();
// 保证队列中的每个数都能加上基本的平方数
for (int i = 0; i < queue_len; i++) {
auto content = queues.front();
queues.pop_front();
for (auto temp : layouts) {
int newx = content.first + temp[0];
int newy = content.second + temp[1];
if (newx >= 0 && newx < row && newy >= 0 && newy < col && matrix[newx][newy] == 1) {
matrix[newx][newy] = -dis; //做标记
queues.push_back({newx, newy});
}
}
}
}
for(int i = 0; i < row; ++i){
for(int j = 0; j < col; ++j)
if(matrix[i][j] < 0) matrix[i][j] = -matrix[i][j];
}
return matrix;
}
void test() {
vector< vector<int>> water = {
{0, 0, 0},
{0, 1, 0},
{0, 0, 0},
};
vector<vector<int>> result = this->updateMatrix4(water);
for (auto i = 0; i < result.size(); i++) {
for (auto j = 0; j < result[0].size(); j++) {
cout << result[i][j] << ", ";
}
cout << endl;
}
}
};
[LeetCode] 01 Matrix 题解的更多相关文章
- [LeetCode] 01 Matrix 零一矩阵
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance b ...
- [Leetcode] 01 Matrix
问题: https://leetcode.com/problems/01-matrix/#/description 基本思路:广度优先遍历,根据所有最短距离为N的格找到所有距离为N+1的格,直到所有的 ...
- [Leetcode Week10]01 Matrix
01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- [leetcode] 542. 01 Matrix (Medium)
给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 计算机学院大学生程序设计竞赛(2015’12)01 Matrix
01 Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 01 Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
随机推荐
- 远程控制TOMCAT启动
远程控制TOMCAT启动 1.在tomcat/conf/tomcat-users.xml中配置好管理员帐号和密码. 2.在浏览器中输入:http://你的ip/manager/list. ...
- [html5]学习笔记一 新增的主题结构元素
html5 新增的主体结构元素有:article,section,nav,aside,time,pubdate元素. 1. article元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引 ...
- IIS7出现“HTTP 错误 404.17 - Not Found 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。”错误-Windows-
Errore HTTP 404.2 - Not Found" IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理 如果你用了安全狗,可能只看到404错误,请打开狗,资源 ...
- block之---内存管理
首先简单说下MRC和ARC MRC: 手动管理内存,需要自己去释放内存, 如果对象的引用计数器为0时对象就会被释放. 属性修饰策略:assign, retain, copy ARC: ARC是编译器特 ...
- Python学习的个人笔记(基础语法)
Python学习的个人笔记 题外话: 我是一个大二的计算机系的学生,这份python学习个人笔记是趁寒假这一周在慕课网,w3cschool,还有借鉴了一些博客,资料整理出来的,用于自己方便的时候查阅, ...
- FZU 2167 大王叫我来巡山呐
Problem 2167 大王叫我来巡山呐 Accept: 931 Submit: 1405Time Limit: 1000 mSec Memory Limit : 32768 KB Pr ...
- 遍历hashMap对效率的影响
测试环境:jdk1.7.0_79\Processor 1.7 GHz Intel Core i5 遍历Map的方式有很多,通常场景下我们需要的是遍历Map中的Key和Value. 写了两个方法: pu ...
- spring入门--Spring框架底层原理
上一篇的博客,我们可以看出来,spring可以维护各个bean (对象),并向其中注入属性值.那么,如果们要把一个对象的引用注入另外一个对象呢?应该怎么处理呢? 我们知道,对于对象中的属性来说,我们注 ...
- html中如何修改选中 用input做的搜索框 的边框颜色
html中如何修改选中 用input做的搜索框 的边框颜色 如图,当我鼠标选中输入框时,内边框会变成蓝色 我的问题是: 1.如何把蓝色去掉? 2.如何改成别的颜色? 首先感谢 UI设计师提出的需求,解 ...
- 【译文】用Spring Cloud和Docker搭建微服务平台
by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...