news 2026/7/7 7:33:27

leetcode 困难题 749. Contain Virus 隔离病毒

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
leetcode 困难题 749. Contain Virus 隔离病毒

Problem: 749. Contain Virus 隔离病毒

解题过程

拿到每个区域内影响cell最多的那个,cell=0的不能重复,不能用sum的最大值,而是te.size()的最大值,然后最大的区域置-1,继续感染的,继续统计最大的那个区域

Code

class Solution { public: int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int containVirus(vector<vector<int>>& isInfected) { int m = isInfected.size(), n = isInfected[0].size(); int x, y, count = 0; while(true) { vector<vector<bool>> status(m, vector<bool>(n, false)); vector<vector<pair<int, int>>> collect; int maxeffect = 0, id = INT_MIN; int mxmx = INT_MIN; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if( isInfected[i][j]==1 && status[i][j]==false ) { queue<pair<int, int>> qe; qe.push({i, j}); collect.push_back({}); status[i][j] = true; pair<int, int> pr; int sum = 0; unordered_set<int> te; while(!qe.empty()) { pr = qe.front(); collect.back().push_back(pr); qe.pop(); for(int k = 0; k < 4; k++) { x = pr.first + dir[k][0]; y = pr.second + dir[k][1]; if( x<0 || y<0 || x>=m || y>=n || status[x][y]==true || isInfected[x][y] == -1) continue; if(isInfected[x][y]==0) { sum++; te.insert((x<<20) + y); continue; } else if(isInfected[x][y]==1) { status[x][y] = true; qe.push({x, y}); } } } if(mxmx < (int)te.size()) { maxeffect = sum; mxmx = te.size(); id = collect.size() - 1; } if(sum == 0) { collect.pop_back(); } } } } if(collect.size() == 0) break; count += maxeffect; if(id != INT_MIN) { for(int j = 0; j < collect[id].size(); j++) { isInfected[collect[id][j].first][collect[id][j].second] = -1; } collect.erase(collect.begin() + id); } if(collect.size() == 0) break; for(int j = 0; j < collect.size(); j++) { for(int i = 0; i < collect[j].size(); i++) { for(int k = 0; k < 4; k++) { x = collect[j][i].first + dir[k][0]; y = collect[j][i].second + dir[k][1]; if( x<0 || y<0 || x>=m || y>=n) continue; if(isInfected[x][y]==0) { isInfected[x][y] = 1; } } } } } return count; } };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/7 18:55:41

语音合成 - 用 Python 合成藏语三大方言语音

通过使用藏语语音合成技术&#xff0c;可以把一段藏文文字&#xff0c;快速变成可用的音频&#xff0c;用在短视频、朗读、课件或字幕配音里。本文介绍一套 Python 脚本&#xff0c;可以直接合成藏语三大方言的语音&#xff1a; 卫藏方言&#xff1a;facebook/mms-tts-bod ([Hu…

作者头像 李华
网站建设 2026/7/7 14:52:33

跨平台资源嗅探工具:5分钟上手全网下载终极指南

还在为无法下载心仪的网络资源而烦恼吗&#xff1f;无论是微信视频号的精彩内容、抖音快手的无水印视频&#xff0c;还是酷狗音乐的高品质音频&#xff0c;Res-Downloader资源下载器都能帮你轻松搞定&#xff01;这款基于Go语言开发的跨平台工具&#xff0c;集成了强大的网络资…

作者头像 李华
网站建设 2026/7/7 13:29:36

Eclipse Debug 配置指南

Eclipse Debug 配置指南 引言 Eclipse 是一款功能强大的集成开发环境(IDE),广泛应用于 Java 开发领域。在进行 Java 程序调试时,Eclipse 提供了丰富的调试功能,以帮助开发者快速定位问题。本文将详细讲解 Eclipse Debug 配置的方法,使您能够轻松地在 Eclipse 中进行有效…

作者头像 李华