当前位置:网站首页>Sword finger offer II 107 Distance in matrix
Sword finger offer II 107 Distance in matrix
2022-07-07 09:48:00 【PI Qiliang】
The finger of the sword Offer II 107. The distance in the matrix 【 Medium question 】
Ideas :
use BFS Sequence traversal , Find all the elements 0, From element 0 Start sequence traversal in four directions , set up ans The array stores the latest 0 distance , be ans in ,mat Matrix elements 0 The position value is 0, Encountered during traversal of each layer 1 Will ans Value is updated to element 0 Of ans value +1
Code :
class Solution {
static int[][] ans;
static int[][] dir = {
{
0,-1},{
0,1},{
-1,0},{
1,0}};
static int m,n;
public static int[][] updateMatrix(int[][] mat) {
m = mat.length;
n = mat[0].length;
// Mark mat matrix (i,j) Whether the location element has been searched
boolean[][] seen = new boolean[m][n];
// Storage mat In the matrix 0 The position coordinates of
Queue<int[]> queue = new LinkedList<>();
// Storage mat matrix (i,j) The nearest location element 0 Distance of
ans = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0){
// take mat The elements in the matrix 0 The position coordinates of are pressed into the queue
queue.offer(new int[]{
i,j});
// The current location of the tag has been searched
seen[i][j] = true;
}
}
}
//BFS Traverse , When the element in the stack is empty , The loop ends
while(!queue.isEmpty()){
// Take out the top element of the queue 0 The location of
int[] cell = queue.poll();
// Take out the elements 0 Coordinates of
int i = cell[0],j = cell[1];
for (int k = 0; k < 4; k++) {
// The element 0 Search up, down, left and right , The location coordinates searched are (x,y)
int x = i + dir[k][0],y = j + dir[k][1];
// If (x,y) Does not exceed the matrix boundary and (x,y) The location has not been searched
if (x >= 0 && x < m && y >= 0 && y < n && !seen[x][y]){
//ans Of (x,y) Position value be equal to ans Of (i,j) Position value + 1
ans[x][y] = ans[i][j] + 1;
// take (x,y) Position push into queue
queue.offer(new int[]{
x,y});
// take (x,y) The location is marked as searched
seen[x][y] = true;
}
}
}
return ans;
}
}
边栏推荐
- Information Security Experiment 4: implementation of IP packet monitoring program
- Selenium+bs4 parsing +mysql capturing BiliBili Tarot data
- Liunx command
- 2016 CCPC Hangzhou Onsite
- Switching value signal anti shake FB of PLC signal processing series
- 根据热门面试题分析Android事件分发机制(一)
- 2020浙江省赛
- Binary tree high frequency question type
- 请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
- 如何成为一名高级数字 IC 设计工程师(5-3)理论篇:ULP 低功耗设计技术精讲(下)
猜你喜欢

Impression notes finally support the default markdown preview mode

H5网页播放器EasyPlayer.js如何实现直播视频实时录像?

In fact, it's very simple. It teaches you to easily realize the cool data visualization big screen

使用BigDecimal的坑

第一讲:包含min函数的栈

How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data

Over 100000 words_ Ultra detailed SSM integration practice_ Manually implement permission management

Information Security Experiment 3: the use of PGP email encryption software

EXT2 file system

JS reverse tutorial second issue - Ape anthropology first question
随机推荐
战略合作|SubQuery 成为章鱼网络浏览器的秘密武器
AI从感知走向智能认知
How to become a senior digital IC Design Engineer (1-6) Verilog coding Grammar: Classic Digital IC Design
Oracle安装增强功能出错
Can flycdc use SqlClient to specify mysqlbinlog ID to execute tasks
ViewPager2和VIewPager的区别以及ViewPager2实现轮播图
进程和线程的区别
高斯消元
IIS redirection redirection appears eurl axd
数据库多表关联查询问题
NATAPP内网穿透
**grafana安装**
Unity shader (basic concept)
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
基础篇:带你从头到尾玩转注解
软件建模与分析
liunx命令
Unity shader (data type in cghlsl)
thinkphp3.2信息泄露
Addition, deletion, modification and query of ThinkPHP database