当前位置:网站首页>2022.7.18-----leetcode.749
2022.7.18-----leetcode.749
2022-07-26 22:24:00 【路Lu727】
//模拟
int[][] g;
int n, m, ans;
int[][] dirs = new int[][]{
{1,0},{-1,0},{0,1},{0,-1}};
boolean[][] vis;
int search(int _x, int _y, Set<Integer> s1, Set<Integer> s2) {
int ans = 0;
Deque<int[]> d = new ArrayDeque<>();
vis[_x][_y] = true;
d.addLast(new int[]{_x, _y});
s1.add(_x * m + _y);
while (!d.isEmpty()) {
int[] info = d.pollFirst();
int x = info[0], y = info[1];
for (int[] di : dirs) {
int nx = x + di[0], ny = y + di[1], loc = nx * m + ny;
if (nx < 0 || nx >= n || ny < 0 || ny >= m || vis[nx][ny]) continue;
if (g[nx][ny] == 1) {
s1.add(loc);
vis[nx][ny] = true;
d.addLast(new int[]{nx, ny});
} else if (g[nx][ny] == 0) {
s2.add(loc);
ans++;
}
}
}
return ans;
}
int getCnt() {
vis = new boolean[n][m];
int max = 0, ans = 0;
// l1: 每个连通块的点集 s2: 每个连通块的候选感染点集
List<Set<Integer>> l1 = new ArrayList<>(), l2 = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] == 1 && !vis[i][j]) {
// s1: 当前连通块的点集 s2: 当前连通块的候选感染点集
Set<Integer> s1 = new HashSet<>(), s2 = new HashSet<>();
int b = search(i, j, s1, s2), a = s2.size();
if (a > max) {
max = a; ans = b;
}
l1.add(s1); l2.add(s2);
}
}
}
for (int i = 0; i < l2.size(); i++) {
for (int loc : l2.get(i).size() == max ? l1.get(i) : l2.get(i)) {
int x = loc / m, y = loc % m;
g[x][y] = l2.get(i).size() == max ? -1 : 1;
}
}
return ans;
}
public int containVirus(int[][] _g) {
g = _g;
n = g.length; m = g[0].length;
while (true) {
int cnt = getCnt();
if (cnt == 0) break;
ans += cnt;
}
return ans;
}
边栏推荐
- Use Arthas to locate online problems
- Download win10 system image and create virtual machine on VMware virtual machine
- Eureka basic use
- 第二部分—C语言提高篇_6. 多维数组
- P5469 [noi2019] robot (Lagrange interpolation, interval DP)
- Part II - C language improvement_ 12. Packaging and use of dynamic / precision Library
- [shader realizes swaying effect _shader effect Chapter 4]
- C language dynamic memory management
- Eureka基本使用
- Several inventory terms often used in communication
猜你喜欢
![[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code](/img/11/27d354a411358bfb39ae7126f33a37.png)
[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code

Easily implement seckill system with redis! (including code)

How to recover the original data when the U disk is damaged, and how to recover the damaged data when the U disk is damaged

研究阿尔茨海默病最经典的Nature论文涉嫌造假

Eureka基本使用

App information reconnaissance & night God simulator burp packet capture configuration

SQL 基础知识

np.transpose & np.expand_dims

面试官问:JS的this指向

DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
随机推荐
About statefulwidget, you have to know the principle and main points!
Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)
DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
Cloud native microservices Chapter 1 server environment description
Part II - C language improvement_ 13. Recursive function
Part II - C language improvement_ 10. Function pointer and callback function
Professor Ashe, a Chinese scientist, made a positive response to the suspected fake Nature paper
[shaders realize distorted outline effect _shader effect Chapter 2]
Write golang simple C2 remote control based on grpc
实战项目:Boost搜索引擎
How to use data pipeline to realize test modernization
Interview questions of Bank of Hangzhou [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
SQL 基础知识
Download win10 system image and create virtual machine on VMware virtual machine
[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version
[shader realizes shine effect _shader effect Chapter 3]
[Luogu] p1395 meeting
Eureka基本使用
How to recover the original data when the U disk is damaged, and how to recover the damaged data when the U disk is damaged
Kt6368a Bluetooth chip development precautions and problem collection - long term update