542. 01 Matrix

https://www.cnblogs.com/grandyang/p/6602288.html

将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值。

最短距离肯定使用bfs。

每次更新了值的地方还要再加入队列中 。

class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(),n = matrix[].size();
queue<pair<int,int>> q;
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
if(matrix[i][j] == )
q.push(make_pair(i,j));
else
matrix[i][j] = INT_MAX;
}
}
while(!q.empty()){
auto coordinate = q.front();
q.pop();
int x = coordinate.first,y = coordinate.second;
for(auto dir : dirs){
int new_x = x + dir[];
int new_y = y + dir[];
if(new_x < || new_x >= matrix.size() || new_y < || new_y >= matrix[].size() || matrix[new_x][new_y] <= matrix[x][y] + )
continue;
matrix[new_x][new_y] = matrix[x][y] + ;
q.push(make_pair(new_x,new_y));
}
}
return matrix;
}
private:
vector<vector<int>> dirs{{-,},{,},{,-},{,}};
};

663. Walls and Gates

https://www.cnblogs.com/grandyang/p/5285868.html

这个题跟542. 01 Matrix很像,主要利用bfs。先把所有的0位置放入队列中,然后通过0更新周围的位置达到更新所有的位置。

class Solution {
public:
/**
* @param rooms: m x n 2D grid
* @return: nothing
*/
void wallsAndGates(vector<vector<int>> &rooms) {
// write your code here
queue<pair<int,int>> q;
for(int i = ;i < rooms.size();i++){
for(int j = ;j < rooms[].size();j++){
if(rooms[i][j] == )
q.push(make_pair(i,j));
}
}
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(auto dir : dirs){
int x_new = x + dir[];
int y_new = y + dir[];
if(x_new < || x_new >= rooms.size() || y_new < || y_new >= rooms[].size() || rooms[x_new][y_new] < rooms[x][y] + )
continue;
rooms[x_new][y_new] = rooms[x][y] + ;
q.push(make_pair(x_new,y_new));
}
}
return;
}
private:
vector<vector<int>> dirs{{-,},{,},{,-},{,}};
};

773. Sliding Puzzle

https://www.cnblogs.com/grandyang/p/8955735.html

queue里面存储的是每次变换后的board的样子和当前新的board中0所在的坐标。

将队列一次清空完了之后才能更新res,因为这就是一次变换。

用set相当于决定什么时候停止循环

class Solution {
public:
int slidingPuzzle(vector<vector<int>>& board) {
int res = ,m = board.size(),n = board[].size();
set<vector<vector<int>>> visited;
vector<vector<int>> correct{{,,},{,,}};
queue<pair<vector<vector<int>>,vector<int>>> q;
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
if(board[i][j] == ){
vector<int> tmp;
tmp.push_back(i);
tmp.push_back(j);
q.push(make_pair(board,tmp));
}
}
}
while(!q.empty()){
for(int i = q.size();i > ;i--){
int x = q.front().second[];
int y = q.front().second[];
vector<vector<int>> board_new = q.front().first;
if(board_new == correct)
return res;
visited.insert(board_new);
q.pop();
for(auto dir : dirs){
int new_x = x + dir[];
int new_y = y + dir[];
if(new_x < || new_x >= m || new_y < || new_y >= n)
continue;
vector<vector<int>> cand = board_new;
swap(cand[x][y],cand[new_x][new_y]);
if(visited.find(cand) != visited.end())
continue;
vector<int> tmp;
tmp.push_back(new_x);
tmp.push_back(new_y);
q.push(make_pair(cand,tmp));
}
}
res++;
}
return -;
}
private:
vector<vector<int>> dirs{{-,},{,},{,-},{,}};
};

注意犯的错误:

for(int i = q.size();i > 0;i--)不能写成for(int i = 0;i < q.size();i++),因为q的size在循环一次后,大小是缩小的,这样不能保证正确的次数。

803. Shortest Distance from All Buildings

https://www.cnblogs.com/grandyang/p/5297683.html

到所有建筑物的距离和最小,其实也就等于到每个建筑物最小距离的和。

以每个建筑物做bfs求最小的距离,然后用一个dis的vector保存,每当增加一个建筑物,在相应位置叠加当前建筑物的最小距离即可。

有可能有些点无法访问到某一些建筑物,所以通过一个count矩阵存储每个位置能访问到的建筑物的个数,最后再判断能否达到。

class Solution {
public:
/**
* @param grid: the 2D grid
* @return: the shortest distance
*/
int shortestDistance(vector<vector<int>> &grid) {
// write your code here
int m = grid.size(),n = grid[].size();
int res = INT_MAX,building = ;
vector<vector<int>> dist(m,vector<int>(n,));
vector<vector<int>> count = dist;
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
if(grid[i][j] == ){
building++;
queue<pair<int,int>> q;
vector<vector<bool>> visited(m,vector<bool>(n,false));
q.push(make_pair(i,j));
int pace = ;
while(!q.empty()){
pace++;
for(int i = q.size();i > ;i--){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(auto dir : dirs){
int new_x = x + dir[];
int new_y = y + dir[];
if(new_x < || new_x >= m || new_y < || new_y >= n || visited[new_x][new_y]== true || grid[new_x][new_y] != )
continue;
dist[new_x][new_y] += pace;
count[new_x][new_y] += ;
visited[new_x][new_y] = true;
q.push(make_pair(new_x,new_y));
}
}
}
}
}
}
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
if(grid[i][j] == && count[i][j] == building)
res = min(res,dist[i][j]);
}
}
return res == INT_MAX ? - : res;
}
private:
vector<vector<int>> dirs{{-,},{,},{,-},{,}};
};

另一种写法:

class Solution {
public:
/**
* @param grid: the 2D grid
* @return: the shortest distance
*/
int shortestDistance(vector<vector<int> > &grid) {
// write your code here
int m = grid.size(),n = grid[].size();
vector<vector<int> > path(m,vector<int>(n,));
vector<vector<int> > count(m,vector<int>(n,));
int building = ;
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
if(grid[i][j] == ){
building++;
vector<vector<bool> > visited(m,vector<bool>(n,false));
search(grid,i,j,path,count,visited);
}
}
}
int res = INT_MAX;
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
if(grid[i][j] == && count[i][j] == building)
res = min(res,path[i][j]);
}
}
return res == INT_MAX ? - : res;
}
void search(vector<vector<int> > grid,int x,int y,vector<vector<int> >& path,vector<vector<int> >& count,vector<vector<bool> > visited){
queue<pair<int,int> > q;
q.push(make_pair(x,y));
int pace = ;
while(!q.empty()){
pace++;
for(int i = q.size();i > ;i--){
int x0 = q.front().first;
int y0 = q.front().second;
q.pop();
//visited[x0][y0] = true;
for(int j = ;j < dirs.size();j++){
int new_x = x0 + dirs[j][];
int new_y = y0 + dirs[j][];
if(new_x < || new_x >= path.size() || new_y < || new_y >= path[].size() || visited[new_x][new_y] || grid[new_x][new_y] != )
continue;
path[new_x][new_y] += pace;
count[new_x][new_y]++;
visited[new_x][new_y] = true;
q.push(make_pair(new_x,new_y));
}
}
}
}
private:
vector<vector<int> > dirs{{-,},{,},{,-},{,}};
}; //visited[new_x][new_y] = true;只能放在新生成的坐标的位置

leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings的更多相关文章

  1. [leetcode] 542. 01 Matrix (Medium)

    给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...

  2. LeetCode 542. 01 Matrix

    输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出 ...

  3. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  4. Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)

    542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 ...

  5. [Leetcode Week10]01 Matrix

    01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...

  6. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  7. [LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  8. Leetcode 542.01矩阵

    01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...

  9. 542 01 Matrix 01 矩阵

    给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离.两个相邻元素间的距离为 1 .示例 1:输入:0 0 00 1 00 0 0输出:0 0 00 1 00 0 0 示例 2:输入: ...

随机推荐

  1. 2013.6.28 - KDD最后一天

    今天收到中秋的邮件.KDD结果出来了,Zhongqiu Wang & Jingwen Huang 15th/561.  

  2. 复习巩固:oracle如何实现去重和分页

    一:oracle实现去重: user数据表: 分两步:1.查询重复数据  2.删除重复数据 1.查询重复数据:在oracle中实现查询重复数据,可以借助于rowid这个伪列.oracle中每个表物理上 ...

  3. 小程序框架之视图层 View~基础组件

    框架为开发者提供了一系列基础组件,开发者可以通过组合这些基础组件进行快速开发.详细介绍请参考组件文档. 什么是组件: 组件是视图层的基本组成单元. 组件自带一些功能与微信风格一致的样式. 一个组件通常 ...

  4. noi 7827 质数的和与积

    描述 两个质数的和是S,它们的积最大是多少? 输入一个不大于10000的正整数S,为两个质数的和.输出一个整数,为两个质数的最大乘积.数据保证有解.样例输入 50 样例输出 589 欧拉筛+尺取.实际 ...

  5. Java集合之整体概述

    Java集合与数组是相似的,都用于保存一组对象,并提供一些操作来管理对象.然而,不同于数组的是,当添加或删除元素时集合的大小是可以自动变化的.Java集合不可以存放基本类型数据(比如int,long或 ...

  6. Centos7安装nginx后提示“Welcome to nginx on Fedora!”,conf.d目录下无default.conf文件

    问题描述 在腾讯云centos7上安装nginx sudo yum install nginx 打开默认网页显示 Welcome to nginx on Fedora! 且 /etc/nginx/co ...

  7. 行为型模式(七) 策略模式(Stragety)

    一.动机(Motivate) 在软件构建过程中,某些对象使用的算法可能多种多样,经常改变,如果将这些算法都编码到对象中,将会使对象变得异常复杂:而且有时候支持不使用的算法也是一个性能负担.如何在运行时 ...

  8. 远程连接Linux mysql报错:Access denied for user ‘root’@‘localhost’(using password: YES)的解决方法

    在新安装好的Centos7上刚安装好mysql,准备进去看看,但是登陆的时候,发现报错啦: ERROR 1045 (28000): Access denied for user 'root'@'loc ...

  9. memset()函数的使用

    1.概述 memset()函数,称为按字节赋值函数,使用时需要加头文件 #include<cstring>或者#include<string.h>.通常有两个用法: (1)用来 ...

  10. [Schematics] 0. Schematics "Hello World"

    1. Install schematics cli: npm i -g @angular-devkit/schematics-cli@latest 2. Then run schematics to ...