题意:给出l个n*m大小的肿瘤切片。1处代表有肿瘤,0处代表没问题。每处的体积设为1。求三维连通的不小于t的肿瘤的总体积。
思路:这题很狗。一眼dfs,结果拉了裤了。可能是递归的层数太深了,RE了。
改成BFS就可了。这题值三十分不会是因为DFS会拉裤吧
DFS拉了裤的代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <iomanip>
#include <string>
#include <list>
#include <cstdlib>
#include <memory>
#include <cstring>
#include <sstream>
#include <list>
#include <deque>
#include <bitset>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define FRER() freopen("input.txt" , "r" , stdin);
#define FREW() freopen("output.txt" , "w" , stdout);
#define QIO ios::sync_with_stdio(false)
#define mem(a, b) memset(a , b , sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-3;
const int MOD = 1e9;
const int maxn = 1e5+100;
int maps[1300+10][130+10][60+10];
bool vis[1300+10][130+10][60+10];
int m , n , l ,t;
bool check(int i , int j ,int k){
return (i>=0&&i<m&&j>=0&&j<n&&k>=0&&k<l&&!vis[i][j][k]);
}
int dfs(int i , int j , int k){
if(maps[i][j][k] == 0) return 0;
vis[i][j][k] = true;
int size = 1;
if(check(i+1,j,k)) size += dfs(i+1,j,k);
if(check(i-1,j,k)) size += dfs(i-1,j,k);
if(check(i,j+1,k)) size += dfs(i,j+1,k);
if(check(i,j-1,k)) size += dfs(i,j-1,k);
if(check(i,j,k+1)) size += dfs(i,j,k+1);
if(check(i,j,k-1)) size += dfs(i,j,k-1);
return size;
}
int main()
{
if (fopen("input.txt", "r") != NULL)
{
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
}
scanf("%d%d%d%d" , &m,&n,&l,&t);
for(int k = 0 ; k < l ; k++)
for(int i = 0 ; i < m ; i++)
for(int j = 0 ; j < n ; j++)
scanf("%d" , &maps[i][j][k]);
int sum = 0;
for(int k = 0 ; k < l ; k++){
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
if(maps[i][j][k] == 1 && !vis[i][j][k]){
int size = dfs(i , j , k);
if(size >= t)
sum += size;
}
}
}
}
printf("%d" , sum);
return 0;
}
BSF冲冲冲:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <iomanip>
#include <string>
#include <list>
#include <cstdlib>
#include <memory>
#include <cstring>
#include <sstream>
#include <list>
#include <deque>
#include <bitset>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define FRER() freopen("input.txt" , "r" , stdin);
#define FREW() freopen("output.txt" , "w" , stdout);
#define QIO ios::sync_with_stdio(false)
#define mem(a, b) memset(a , b , sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-3;
const int MOD = 1e9;
const int maxn = 1e5+100;
int maps[1300+10][130+10][60+10];
bool vis[1300+10][130+10][60+10];
int m , n , l ,t;
bool check(int i , int j ,int k){
return (i>=0&&i<m&&j>=0&&j<n&&k>=0&&k<l&&!vis[i][j][k]&&maps[i][j][k] == 1);
}
struct Node{
int x , y , z;
};
int bfs(int i , int j , int k){
int size = 0;
queue<Node> q;
q.push((Node){i,j,k});
vis[i][j][k] = true;
while(!q.empty()){
Node now = q.front();
size++;
q.pop();
i = now.x;
j = now.y;
k = now.z;
if(check(i+1,j,k)) vis[i+1][j][k] = true,q.push((Node){i+1,j,k});
if(check(i-1,j,k)) vis[i-1][j][k] = true,q.push((Node){i-1,j,k});
if(check(i,j+1,k)) vis[i][j+1][k] = true,q.push((Node){i,j+1,k});
if(check(i,j-1,k)) vis[i][j-1][k] = true,q.push((Node){i,j-1,k});
if(check(i,j,k+1)) vis[i][j][k+1] = true,q.push((Node){i,j,k+1});
if(check(i,j,k-1)) vis[i][j][k-1] = true,q.push((Node){i,j,k-1});
}
return size;
}
int main()
{
if (fopen("input.txt", "r") != NULL)
{
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
}
scanf("%d%d%d%d" , &m,&n,&l,&t);
for(int k = 0 ; k < l ; k++)
for(int i = 0 ; i < m ; i++)
for(int j = 0 ; j < n ; j++)
scanf("%d" , &maps[i][j][k]);
int sum = 0;
for(int k = 0 ; k < l ; k++){
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
if(maps[i][j][k] == 1 && !vis[i][j][k]){
int size = bfs(i , j , k);
if(size >= t)
sum += size;
}
}
}
}
printf("%d" , sum);
return 0;
}