news 2026/6/23 20:13:23

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/6/23 18:50:31

三步学会百度网盘极速下载:告别龟速的终极方案

三步学会百度网盘极速下载&#xff1a;告别龟速的终极方案 【免费下载链接】baidu-wangpan-parse 获取百度网盘分享文件的下载地址 项目地址: https://gitcode.com/gh_mirrors/ba/baidu-wangpan-parse 还在为百度网盘的下载速度而烦恼吗&#xff1f;当你明明拥有高速网络…

作者头像 李华
网站建设 2026/6/23 18:51:02

5大实用技巧:用Calibre-Douban插件智能管理电子书元数据

5大实用技巧&#xff1a;用Calibre-Douban插件智能管理电子书元数据 【免费下载链接】calibre-douban Calibre new douban metadata source plugin. Douban no longer provides book APIs to the public, so it can only use web crawling to obtain data. This is a calibre D…

作者头像 李华
网站建设 2026/6/23 14:03:10

飞书文档批量导出终极指南:一键解决文档迁移难题

飞书文档批量导出终极指南&#xff1a;一键解决文档迁移难题 【免费下载链接】feishu-doc-export 项目地址: https://gitcode.com/gh_mirrors/fe/feishu-doc-export 你是否曾经为文档迁移而头疼不已&#xff1f;当公司决定更换办公平台&#xff0c;或是需要将飞书知识库…

作者头像 李华
网站建设 2026/6/23 18:51:01

Source Han Serif思源宋体:免费开源中文字体专业应用指南

Source Han Serif思源宋体&#xff1a;免费开源中文字体专业应用指南 【免费下载链接】source-han-serif-ttf Source Han Serif TTF 项目地址: https://gitcode.com/gh_mirrors/so/source-han-serif-ttf 还在为字体版权问题而烦恼&#xff1f;Source Han Serif思源宋体为…

作者头像 李华
网站建设 2026/6/23 18:52:03

DOM Element:深入理解与操作

DOM Element:深入理解与操作 引言 在Web开发领域,DOM(文档对象模型)是一个至关重要的概念。DOM允许开发者与网页内容进行交互,如HTML和XML文档。DOM Element是DOM模型中的核心组成部分,它代表了HTML或XML文档中的每一个元素。本文将深入探讨DOM Element的概念、属性、方…

作者头像 李华