当前位置:网站首页>1162. Map analysis - non recursive method
1162. Map analysis - non recursive method
2022-07-28 21:24:00 【Mr Gao】
1162. Map analysis
Now you have a copy of n x n Of grid grid, Each of the above Cell Use both 0 and 1 It's marked . among 0 On behalf of the ocean ,1 Representing land .
Please find an ocean cell , The ocean cell has the largest distance to the nearest land cell , And return the distance . If there's only land or ocean on the grid , Please return -1.
The distance we're talking about here is 「 Manhattan distance 」( Manhattan Distance):(x0, y0) and (x1, y1) The distance between these two cells is |x0 - x1| + |y0 - y1| .
Example 1:
Input :grid = [[1,0,1],[0,0,0],[1,0,1]]
Output :2
explain :
Ocean cell (1, 1) And the distance between all the land cells reaches the maximum , The maximum distance is 2.
Example 2:
Input :grid = [[1,0,0],[0,0,0],[0,0,0]]
Output :4
explain :
Ocean cell (2, 2) And the distance between all the land cells reaches the maximum , The maximum distance is 4.
For this question , If we don't use recursive methods , Then use two arrays to store 0 and 1 Coordinates of , Then calculate , It can reduce the computational cost , But this way , Not yet optimal , It can be further judged that the calculation stops , The solution code is as follows :
int maxDistance(int** grid, int gridSize, int* gridColSize){
int n=gridSize,m=gridColSize[0];
int store0[n*m][2];
int store1[n*m][2];
int i,j;
int size0=0,size1=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(grid[i][j]==0){
store0[size0][0]=i;
store0[size0][1]=j;
size0++;
}
else{
store1[size1][0]=i;
store1[size1][1]=j;
size1++;
}
}
}
if(size1==n*m||size0==n*m){
return -1;
}
int max=0;
for(i=0;i<size0;i++){
int x=store0[i][0];
int y=store0[i][1];
int min=2000;
for(j=0;j<size1;j++){
int d=abs(store1[j][0]-x)+abs(store1[j][1]-y);
if(d<min){
min=d;
}
}
if(min>max){
max=min;
}
}
return max;
}
边栏推荐
- (转)冒泡排序及优化详解
- 35 道 MySQL 面试必问题图解,这样也太好理解了吧
- SQL server中提示对象名无效
- 怎么理解数据网格(Data Mesh)
- Guo Mingxuan: meta contraction is conducive to the development of VR competitors, and apple XR headshow will change the industry rules
- Unity3d tutorial notes - unity initial 04
- 【Bluetooth蓝牙开发】八、BLE协议之传输层
- Introduction to blue team: efficiency tools
- The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......
- protobuf 中基础数据类型的读写
猜你喜欢

Nacos principle

Redis缓存雪崩、缓存穿透、缓存击穿

SSM use @async and create threadpooltaskexecutor thread pool

Mobilevit: challenge the end-to-side overlord of mobilenet

两款吾爱破解优秀软件,批量查找文本,图像视频画质增强

编码用这16个命名规则能让你少写一半以上的注释!

The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......

SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计

There have been two safety accidents in a month after listing. Is L9 ideal?

Maxwell 一款简单易上手的实时抓取Mysql数据的软件
随机推荐
职场高薪 |「中高级测试」面试题
Applet container technology improves mobile R & D efficiency by 500%
SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计
程序员最大的浪漫~
(PMIC) full and half bridge drive csd95481rwj PDF specification
Unity - Fundamentals of 3D mathematics
八、QOS队列调度与报文丢弃
编码用这16个命名规则能让你少写一半以上的注释!
Bug of Dom4j
Uncaught Error:Invalid geoJson format Cannot read property ‘length‘ of undefind
Nacos principle
The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......
SSM use @async and create threadpooltaskexecutor thread pool
Capture video by buffering
DLL decompile (decompile encrypted DLL)
What functions does MySQL have? Don't look everywhere. Just look at this.
Basic operations of unity3d scene production
What is low code? Which platforms are suitable for business personnel? Is it reliable to develop the system?
Two excellent software of my love cracking, batch search text, image and video image quality enhancement
Icml2022 | timing self-monitoring video transformer