当前位置:网站首页>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
边栏推荐
- 直播系统代码,自定义软键盘样式:字母、数字、标点三种切换
- Superfluid_ HQ hacked analysis
- MUX VLAN configuration
- 普通人下场全球贸易,新一轮结构性机会浮出水面
- Mlsys 2020 | fedprox: Federation optimization of heterogeneous networks
- Leetcode 剑指 Offer 59 - II. 队列的最大值
- Huawei converged VLAN principle and configuration
- Is chaozhaojin safe? Will it lose its principal
- A Cooperative Approach to Particle Swarm Optimization
- 一图看懂!为什么学校教了你Coding但还是不会的原因...
猜你喜欢
Some features of ECMAScript
Finding the nearest common ancestor of binary search tree by recursion
Leetcode study - day 35
BiShe - College Student Association Management System Based on SSM
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
Docker compose配置MySQL并实现远程连接
Pbootcms plug-in automatically collects fake original free plug-ins
Leetcode skimming questions_ Sum of squares
Superfluid_ HQ hacked analysis
随机推荐
WGet: command line download tool
有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
关于softmax函数的见解
Leetcode sword finger offer 59 - ii Maximum value of queue
Mlsys 2020 | fedprox: Federation optimization of heterogeneous networks
037 PHP login, registration, message, personal Center Design
Cglib dynamic agent -- example / principle
Leetcode 208. 实现 Trie (前缀树)
SPIR-V初窥
Leetcode daily question solution: 1189 Maximum number of "balloons"
How does Huawei enable debug and how to make an image port
Nmap: network detection tool and security / port scanner
Pbootcms plug-in automatically collects fake original free plug-ins
Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
Leetcode1961. 检查字符串是否为数组前缀
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
现货白银的一般操作方法
Xunrui CMS plug-in automatically collects fake original free plug-ins
leetcode刷题_平方数之和
Dedecms plug-in free SEO plug-in summary