PAT甲题题解-1091. Acute Stroke (30)-BFS
题意:给定三维数组,0表示正常,1表示有肿瘤块,肿瘤块的区域>=t才算是肿瘤,求所有肿瘤块的体积和
这道题一开始就想到了dfs或者bfs,但当时看数据量挺大的,以为会导致栈溢出,所以并没有立刻写,想有没有别的办法。
然而结果是,实在想不出别的办法了,所以还是尝试写写dfs、bfs。
一开始先用了dfs,最后两个样例段错误,估计是栈溢出了。
之所以dfs栈溢出,因为dfs的时候每个状态都会存储在堆栈里,就好比dfs的第一个状态,一直保存到最后整个dfs结束。
而bfs是存储在队列中,每次队列都会有状态取出来,所以栈存储就会少很多。
对于每次bfs,计算肿瘤块体积,如果>=t,才算入总和sum中。
用vis[i][j][k]标记结点是否被访问过,被访问过的就不会再次访问,即不会再加入到队列中去。
PS:这里slice和vis数组开成了62*1288*130,即每个维度都比原来多2,是为了处理数组越界
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; int m,n,l,t; bool slice[62][1288][130]; //即输入 bool vis[62][1288][130]; //标记对应的点是否访问过 int cnt=0; struct Node{int i,j,k; }; int BFS(int a,int b,int c){if(vis[a][b][c] || slice[a][b][c]==false)return 0;Node t;int i,j,k;int res=0;queue<Node>q;t.i=a;t.j=b;t.k=c;q.push(t);vis[a][b][c]=true;while(!q.empty()){t=q.front();q.pop();i=t.i;j=t.j;k=t.k;if(i==0 || i>l || j==0 || j>m || k==0 || k>n)continue;res++;//六个方向if(slice[i][j-1][k] && !vis[i][j-1][k]){t.i=i;t.j=j-1;t.k=k;q.push(t);vis[i][j-1][k]=true;}if(slice[i][j+1][k] && !vis[i][j+1][k]){t.i=i;t.j=j+1;t.k=k;q.push(t);vis[i][j+1][k]=true;}if(slice[i][j][k-1] && !vis[i][j][k-1]){t.i=i;t.j=j;t.k=k-1;q.push(t);vis[i][j][k-1]=true;}if(slice[i][j][k+1] && !vis[i][j][k+1]){t.i=i;t.j=j;t.k=k+1;q.push(t);vis[i][j][k+1]=true;}if(slice[i+1][j][k] && !vis[i+1][j][k]){t.i=i+1;t.j=j;t.k=k;q.push(t);vis[i+1][j][k]=true;}if(slice[i-1][j][k] && !vis[i-1][j][k]){t.i=i-1;t.j=j;t.k=k;q.push(t);vis[i-1][j][k]=true;}}return res; } int main() {memset(vis,false,sizeof(vis));memset(slice,false,sizeof(slice));int a;scanf("%d %d %d %d",&m,&n,&l,&t);for(int i=1;i<=l;i++){for(int j=1;j<=m;j++){for(int k=1;k<=n;k++){scanf("%d",&a);if(a==1)slice[i][j][k]=true;elseslice[i][j][k]=false;}}}int sum=0;for(int i=1;i<=l;i++){for(int j=1;j<=m;j++){for(int k=1;k<=n;k++){cnt=BFS(i,j,k);if(cnt>=t)sum+=cnt;}}}printf("%d\n",sum);return 0; }
View Code
转载于:https://www.cnblogs.com/chenxiwenruo/p/6786453.html
PAT甲题题解-1091. Acute Stroke (30)-BFS相关推荐
- 1091. Acute Stroke (30)-PAT甲级真题(广度优先搜索)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...
- PAT甲级 1091 Acute Stroke(30) (Flood Fill)
题目 One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the ...
- 1091 Acute Stroke (30)(30 分)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...
- 1091. Acute Stroke (30)
题目如下: One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given ...
- 【PTA-A】1091 Acute Stroke (30 分)(BFS、队列)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...
- 【PAT甲级题解】1091 Acute Stroke (30分) BFS
本题考BFS应用,题目大意是给出一个三维0,1矩阵,你需要对任意一个元素的上下左右前后进行判断枚举,如果当前元素为1且满足要求,则入队,当前枚举结束后如果该'1'矩阵不小于一个'l'代表的阈值则返回其 ...
- 【PAT】1091 Acute Stroke (30 分)
三维搜索,按照6个邻接理论,总会有重合的部分区域, 因此根据搜索顺序,先访问到的就是一个区域的[不能较真] #include <bits/stdc++.h> using namespace ...
- 1091 Acute Stroke (30 分)【难度: 一般 / bfs】
https://pintia.cn/problem-sets/994805342720868352/problems/994805375457411072 很常见的题,就是求每一个连通块的大小. #i ...
- PAT甲题题解-1050. String Subtraction (20)-水题
#include <iostream> #include <cstdio> #include <string.h> #include <algorithm&g ...
最新文章
- 移动三国己然鼎立,普​通开发者如何选择开发​平台?
- Video4Linux
- js学习总结----案例之多级菜单js版本
- 财税、管理知识,离您那么远吗?
- World Wind Java开发之十二——加载粗制三维模型(ExtrudedPolygon)(转)
- 一本内行人写给行内人的安全经典--全面揭秘曾经热炒一时的“入侵腾讯事件”...
- 让一个从未接触过电脑的人测试浏览器
- iOS:面试八大陷阱
- msm8996平台的 camera 框架笔记
- 何钦铭c语言第三版第3章答案,何钦铭版C语言第3章答案精选.pdf
- 区块链供应链金融有着怎样的意义?
- windows下安装zookeeper以及监控中心
- 做自媒体如何快速实现财务自由
- 上课签到 php,福建一高校学生上课需刷脸签到 被赞高大上
- 谈谈我当韭菜的投机经历
- 《网络媒体教程》后记
- 助攻歌神演唱会喜提“第八杀”,AI应用创新浪潮来啦?
- arduino nano原理图_三种常用的Arduino开发板
- Android是虚拟机运行后总是显示:Unfortunately XXX has stopped.
- SAP SD CMD_EI_API=>MAINTAIN 创建客户主数据
热门文章
- Git初学札记(零)————EGIT完成Eclipse到GitHub一条龙
- android nfc ndef mifareclassic,Android NFC开发-实践篇
- linux dev sda1 满了,/dev/sda1把磁盘几乎都满了,我该怎么办呢??
- linux基础服务,Linux基础
- oracle 减少回表,減少oracle sql回表次數 提高SQL查詢性能 | 學步園
- adminer.php下载,Adminer.php
- android radiogroup 获取点击位置_屏幕连点器,解放双手[Android]
- Java经典课程设计--在线蛋糕商城销售网站项目【SpringMvc+mybatis+bootstrap等实现】
- android 菜单 功能键,Android交互体验必知:功能按键事件
- 仪表盘怎么调 铃木uy125摩托车_平时市区骑行,摩托车链条多久保养一次?