A1091

开始没读懂题,由于没碰到过三维数组的题,思想只禁锢于二维数组.
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值