开始没读懂题,由于没碰到过三维数组的题,思想只禁锢于二维数组.
Two pixels are connected and hence belong to the same region if they share a common side, as shown by Figure 1 where all the 6 red pixels are connected to the blue one.
并对这句话的理解也出了问题,理解成了:若两个1中间隔着一个0,也可认为是属于同一区域.
而题目的本意是为了告知,一个1可以于上下左右前后6个方向的1连接.
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
struct node{
int row;
int col;
};
const int row_max=1300;
const int col_max=130;
int a[row_max][col_max],x[4]={-1,0,0,1},y[4]={0,-1,1,0},count_total=0,M,N,L,T;
bool flag[row_max][col_max]={false};
queue<node> q;
bool test1(int r,int c){
if(r<0||r>=M||c<0||c>=N)return false;
if(a[r][c]==0||flag[r][c])return false;
return true;
}
bool test2(int r,int c){
if(a[r][c]==1&&flag[r][c]==false)return true;
return false;
}
int dfs(){
int count=0;
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
if(a[i][j]==1&&flag[i][j]==false){
int now=1;
flag[i][j]=true;
bool isfirst=true;
node s;
s.row=i,s.col=j;
q.push(s);
while(!q.empty()){
node t=q.front();
q.pop();
for(int i=0;i<4;i++){
node temp;
temp.row=t.row+x[i];
temp.col=t.col+y[i];
if(test1(temp.row,temp.col)){ //没将特殊情况写在此
flag[temp.row][temp.col]=true;
q.push(temp);
now++;
isfirst=false;
}else{
if(a[temp.row][temp.col]==0){
if(test2(temp.row+x[i],temp.col+y[i])){
temp.row+=x[i];
temp.col+=y[i];
flag[temp.row][temp.col]=true;
q.push(temp);
now++;
isfirst=false;
}
}
}
}
}
if(isfirst)now--;
if(now>=T)count+=now;
}
}
}
return count;
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt","r",stdin);
#endif
scanf("%d%d%d%d",&M,&N,&L,&T);
for(int i=0;i<L;i++){
for(int p=0;p<M;p++){
for(int q=0;q<N;q++){
scanf("%d",&a[p][q]);
}
}
count_total+=dfs();
for(int m=0;m<M;m++){
for(int n=0;n<N;n++){
flag[m][n]=false;
}
}
}
printf("%d\n",count_total);
return 0;
}
看完算法笔记敲了一遍,用三维数组解决,思想跟配套用书中的二维数组一模一样.
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
const int row_max=1300;
const int col_max=130;
const int z_max=65;
int a[row_max][col_max][z_max],m,n,slice,t,total=0,X[6]={-1,1,0,0,0,0},Y[6]={0,0,-1,1,0,0},Z[6]={0,0,0,0,-1,1};
bool flag[row_max][col_max][z_max]={false};
struct node{
int x,y,z;
};
queue<node> q;
bool judge(int x,int y,int z){
if(x<0||x>=m||y<0||y>=n||z<0||z>=slice)return false;
if(a[x][y][z]==0||flag[x][y][z]==true)return false;
return true;
}
int bfs(int x,int y,int z){
int now=0;
node n;
n.x=x,n.y=y,n.z=z;
q.push(n);
flag[x][y][z]=true;
now++;
while(!q.empty()){
n=q.front();
q.pop();
for(int i=0;i<6;i++){
node temp;
temp.x=n.x+X[i],temp.y=n.y+Y[i],temp.z=n.z+Z[i];
if(judge(temp.x,temp.y,temp.z)){
q.push(temp);
now++;
flag[temp.x][temp.y][temp.z]=true;
}
}
}
if(now>=t)return now;
return 0;
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt","r",stdin);
#endif
scanf("%d%d%d%d",&m,&n,&slice,&t);
for(int z=0;z<slice;z++){
for(int x=0;x<m;x++){
for(int y=0;y<n;y++){
scanf("%d",&a[x][y][z]);
}
}
}
for(int z=0;z<slice;z++){
for(int x=0;x<m;x++){
for(int y=0;y<n;y++){
if(a[x][y][z]==1&&flag[x][y][z]==false){
total+=bfs(x,y,z);
}
}
}
}
printf("%d",total);
return 0;
}