当前位置:网站首页>Force buckle 1020 Number of enclaves
Force buckle 1020 Number of enclaves
2022-07-06 01:25:00 【zjsru_ Beginner】

How small is Russia , Why is NATO afraid ?
It turns out that this is an enclave of Russia . Not all Russian territory .
Give you a size of m x n The binary matrix of grid , among 0 Represents an ocean cell 、1 Represents a land cell .
once Move It refers to walking from one land cell to another ( On 、 Next 、 Left 、 Right ) Land cells or across grid The boundary of the .
Return to grid unable The number of land cells that leave the grid boundary in any number of moves .

This is a routine bfs Search questions , It is obvious to start with four boundaries ,grid【】【】 by 1 Join the team , Then I use guangsearch , If the value of the lattice connected to the boundary is 1, Just put grid Change it to 0, Then traverse , Look, how many are there 1.
class Solution {
public:
int room[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };
int numEnclaves(vector<vector<int>>& grid) {
int x = grid.size(), y = grid[0].size();
if (x == 1)return 0;
queue<pair<int, int>>q;
for (int i = 0; i < y; i++)// Join the team around the border 1
if (grid[0][i] == 1)
q.emplace(0, i);
for (int i = 0; i < y; i++)
if (grid[x - 1][i] == 1)
q.emplace(x-1, i);
for (int i = 0; i < x; i++)
if (grid[i][y-1] == 1)
q.emplace(i, y-1);
for (int i = 0; i < x; i++)
if (grid[i][0] == 1)
q.emplace(i, 0);
while (!q.empty()) {//bfs Search element
int first_x = q.front().first;
int first_y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {//first Search around elements
int next_x = first_x + room[i][0];
int next_y = first_y + room[i][1];
if (next_x >= 0 && next_x < x && next_y >= 0 && next_y < y && grid[next_x][next_y] == 1) {
q.emplace(next_x, next_y);
grid[next_x][next_y] = 0;
}
}
}
int k = 0;
for (int i = 1; i < x-1; i++)
for (int j = 1; j < y-1; j++)
if (grid[i][j] == 1)
k++;
return k;
}
};I also wrote a lot bfs and dfs It's the topic of , Summarize the idea of this kind of chart topic :
1. Specify what to use ,bfs still dfs;
2. The first point we want to search ( The starting point ) What is it? ;
3. How can we judge whether we have searched this point ;
4. The answer is given according to the question .
jsl
边栏推荐
- 基于DVWA的文件上传漏洞测试
- Basic process and testing idea of interface automation
- Five challenges of ads-npu chip architecture design
- Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
- [机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
- 什么是弱引用?es6中有哪些弱引用数据类型?js中的弱引用是什么?
- Building core knowledge points
- Recommended areas - ways to explore users' future interests
- Leetcode daily question solution: 1189 Maximum number of "balloons"
- c#网页打开winform exe
猜你喜欢

c#网页打开winform exe

Basic process and testing idea of interface automation

A picture to understand! Why did the school teach you coding but still not

How to see the K-line chart of gold price trend?

False breakthroughs in the trend of London Silver

普通人下场全球贸易,新一轮结构性机会浮出水面

XSS learning XSS lab problem solution

Test de vulnérabilité de téléchargement de fichiers basé sur dvwa

Superfluid_ HQ hacked analysis

Four commonly used techniques for anti aliasing
随机推荐
PHP error what is an error?
After 95, the CV engineer posted the payroll and made up this. It's really fragrant
A picture to understand! Why did the school teach you coding but still not
MYSQL---查询成绩为前5名的学生
Gartner released the prediction of eight major network security trends from 2022 to 2023. Zero trust is the starting point and regulations cover a wider range
Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
Huawei Hrbrid interface and VLAN division based on IP
c#网页打开winform exe
一图看懂!为什么学校教了你Coding但还是不会的原因...
Leetcode sword finger offer 59 - ii Maximum value of queue
Convert binary search tree into cumulative tree (reverse middle order traversal)
Dede collection plug-in free collection release push plug-in
282. Stone consolidation (interval DP)
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
MUX VLAN configuration
ThreeDPoseTracker项目解析
什么是弱引用?es6中有哪些弱引用数据类型?js中的弱引用是什么?
有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
File upload vulnerability test based on DVWA