PAT甲级 1091 Acute Stroke
使用DFS的话会报错,可能是递归的层数太多导致栈出问题了,使用BFS才可以AC
//BFS 使用DFS的话会报错,可能是递归的层数太多导致栈出问题了,使用BFS才可以AC
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
bool arr[66][1300][130]={false}, status[66][1300][130]={false};
int num, allnum = 0, K, N, M, L, T;
struct nod {int k, i, j;};
int width_firstly_search(int k, int i, int j) {queue<nod>q;nod nd = {k, i, j};q.push(nd);int ww = 0;while(!q.empty()) {nd = q.front();q.pop();bool kk = true;if(nd.k < 0 || nd.k >=L || nd.i < 0 || nd.i >= M || nd.j < 0 || nd.j >= N || status[nd.k][nd.i][nd.j]==true || arr[nd.k][nd.i][nd.j]==false) continue;status[nd.k][nd.i][nd.j] = true;q.push(nod{nd.k - 1, nd.i, nd.j});q.push(nod{nd.k + 1, nd.i, nd.j});q.push(nod{nd.k, nd.i - 1, nd.j});q.push(nod{nd.k, nd.i + 1, nd.j});q.push(nod{nd.k, nd.i, nd.j - 1});q.push(nod{nd.k, nd.i, nd.j + 1});ww++;}return ww;
}
int main(void) {int i, j, k, m, n, kk;cin>>M>>N>>L>>T;for(k = 0; k < L; k++) {for(i = 0; i < M; i++) {for(j = 0; j < N; j++) {scanf("%d", &kk);arr[k][i][j] = kk==1;}}}for(k = 0; k < L; k++) {for(i = 0; i < M; i++) {for(j = 0; j < N; j++) {if(status[k][i][j]==false && arr[k][i][j]==true) {num = width_firstly_search(k, i, j);if(num >= T) allnum += num;}}}}printf("%d\n", allnum);return 0;
}
这里也贴出相应DFS的codes
//DFS
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
bool arr[66][1300][130]={false}, status[66][1300][130]={false};
int num, allnum = 0, K, N, M, L, T;
void recursion(int k, int i, int j) {if(k < 0 || k >=L || i < 0 || i >= M || j < 0 || j >= N || status[k][i][j]==true || arr[k][i][j]==false) return;status[k][i][j] = true;num++;if(k-1 >= 0 || k-1 < L) recursion(k - 1, i, j);if(k+1 >= 0 || k+1 < L) recursion(k + 1, i, j);if(i-1 >= 0 || i-1 < M) recursion(k, i - 1, j);if(i+1 >= 0 || i+1 < M) recursion(k, i + 1, j);if(j-1 >= 0 || j-1 < N) recursion(k, i, j - 1);if(j+1 >= 0 || j+1 < N) recursion(k, i, j + 1);
}
int main(void) {int i, j, k, m, n, kk;cin>>M>>N>>L>>T;for(k = 0; k < L; k++) {for(i = 0; i < M; i++) {for(j = 0; j < N; j++) {scanf("%d", &kk);arr[k][i][j] = kk==1;}}}for(k = 0; k < L; k++) {for(i = 0; i < M; i++) {for(j = 0; j < N; j++) {if(status[k][i][j]==false && arr[k][i][j]==true) {num = 0;recursion(k, i, j);if(num >= T) allnum += num;}}}}printf("%d\n", allnum);return 0;
}
PAT甲级 1091 Acute Stroke相关推荐
- PAT甲级 1091 Acute Stroke(30) (Flood Fill)
题目 One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the ...
- 【PAT】1091 Acute Stroke (30 分)
三维搜索,按照6个邻接理论,总会有重合的部分区域, 因此根据搜索顺序,先访问到的就是一个区域的[不能较真] #include <bits/stdc++.h> using namespace ...
- 1091. Acute Stroke (30)-PAT甲级真题(广度优先搜索)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...
- 1091 Acute Stroke (PAT甲级)
这道题用dfs做的话,因为递归太多层,堆栈溢出,有两个测试点过不了:所以用bfs. 根据他人代码修改后的结果: #include <cstdio> #include <vector& ...
- 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)-BFS
题意:给定三维数组,0表示正常,1表示有肿瘤块,肿瘤块的区域>=t才算是肿瘤,求所有肿瘤块的体积和 这道题一开始就想到了dfs或者bfs,但当时看数据量挺大的,以为会导致栈溢出,所以并没有立刻写 ...
最新文章
- 独家 | 手把手教你用Python构建你的第一个多标签图像分类模型(附案例)
- Binary Tree Nodes(单表多实例查询)
- 13 种 JavaScript 代码技巧
- PIC单片机精通_串口通讯与串口调试实例
- css3背景、边框、和补丁相关属性
- OpenCV:OpenCV目标检测Boost方法单独训练
- Hystrix 简介和使用
- java ee自学路线
- 电脑速度太慢 重装系统不如换个帐户
- 红橙Darren视频笔记 自定义RatingBar touch事件学习 dp转px listener监听
- ES6中对象新增方法
- windows下安装python的包管理工具pip,scikit-learn
- Xcode 6 的新增特性
- 使用ActiveSync同步WinCE设备,并在局域网中调试网络程序
- PHP:使用pecl安装 swoole
- 关于keil-C51中code、idata以及xdata
- 2021年私域流量的力量会更强劲!
- 零基础学Arcgis(七)|空间数据采集与管理(4)数据检查
- C++设计模式之二(设计模式六大原则)
- 微信小程序面试题(个人学习)