BOJ-311. 图像识别-计算机二2014

在这里插入图片描述

在这里插入图片描述
思路
dfs某个点可以到达的区域同时做好标记。依次遍历矩阵上的点,若未标记,则对其dfs,区域数++;若已标记,说明该区域已被统计,跳过。
代码:

#include <iostream>
#include <math.h>

using namespace std;
 
const int NN = 110;
 
int Mat[NN][NN];
bool mark[NN][NN];

int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
int N, M, D;

// dfs 从某点能连通的区域并做好标记
void dfs(int x, int y) {
	for (int i = 0; i < 8; i ++ ) {
		int xx = x + dx[i];
		int yy = y + dy[i];
		if (xx < 0 || xx >= N || yy < 0 || yy >= M) continue;
		if (mark[xx][yy]) continue;
		if (abs(Mat[x][y] - Mat[xx][yy]) > D) continue;
		mark[xx][yy] = true;
		dfs(xx, yy);
	}
	return;
}
int main() {
	int T;
	scanf("%d", &T);
	for (int i = 0; i < T; i ++ ) {
	    int res = 0;
		scanf("%d%d%d", &N, &M, &D);
		for (int j = 0; j < N; j ++ ) {
			for (int k = 0; k < M; k ++ ) {
				scanf("%d", &Mat[j][k]);
				mark[j][k] = false;
			}
		}
		mark[0][0] = true;
		for (int j = 0; j < N; j ++ ) {
			for (int k = 0; k < M; k ++ ) {
				if (j + k > 0 && mark[j][k]) continue; // 统计起始点连接的区域,以及未被标记过的区域
				dfs(j, k);
				res ++;
			}
		}
		
		cout << res << endl;
	}
	
	return 0;
}
// 2
// 3 3 0
// 1 1 1
// 0 1 0
// 0 1 0
// 3 4 1
// 10 11 12 13
// 9 8 7 6
// 2 3 4 5
// 3
// 1
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值