北邮机试 | bupt oj | 278. 图像识别-计算机二2014

版权声明:本人小白,有错误之处恳请指出,感激不尽;欢迎转载 https://blog.csdn.net/stone_fall/article/details/88764344

图像识别

题目描述

在图像识别中,我们经常需要分析特定图像中的些特征,而其中很重要的点就是识别出图像的多个区域。在这个问题中,我们将给定一幅N x M的图像,其中每个1 x 1的点都用一个[0, 255]的值来表示他的RGB颜色。如果两个相邻的像素点颜色差值不超过D.我们就认为这两个像素点属于豆一个区域。对于一个像素点(x,y),以下这8个点(如果存在)是与它相邻的: (x 一1,y一1),(x 一1,y),(x一1,y + 1),(x,y 一1),(x,y + 1),(x + 1,y- 1),(x + 1,y),(x +1,y+ 1)。

你的任务是写一个程序,分辨出给定图像中一共被分为多少个区域。

输入格式

输入数据包含多组测试数据。

输入的第一行是一个整数T (T ≤ 100),表示测试效据的组数,

每组测试数据的第一行是三个整数N,M,D(1≤N,M≤100, 0≤D≤255),意义知上所述。

接下来N行,每行M个整数,表示给定图像的每人像素点颜色。

输出格式

对于每组测试数据输出一行,即图像中的区域数量

输入样例

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

AC代码

参考

https://blog.csdn.net/TQCAI666/article/details/88358586#Problem_C_111

#include<bits/stdc++.h>
#define For(i,start,end) for(int i=start;i<end;i++)
#define MAXN 100
using namespace std;
typedef struct node{
    int x,y;
    node(int x,int y):x(x),y(y){}
}Node;
int Map[MAXN][MAXN];
int vis[MAXN][MAXN];
int main(){
    int t,n,m,d;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d",&n,&m,&d);
        memset(vis,0,sizeof vis);
        For(i,0,n){
            For(j,0,m){
                scanf("%d",&Map[i][j]);
            }
        }
        queue<Node> q;
        int cnt=0;
        For(x,0,n)For(y,0,m)if(vis[x][y]==0){
            cnt++;
            vis[x][y]=1;
            q.push(Node(x,y));
            while(!q.empty()){
                Node tmp=q.front();
                q.pop();
                int cx=tmp.x;
                int cy=tmp.y;
                int v = Map[cx][cy];
                for(int i=cx-1;i<=cx+1;i++){
                    for(int j=cy-1;j<=cy+1;j++){
                        if(!(cx==i && cy==j) && vis[i][j]==0){
                            if(i>=0 && i<n && j>=0 && j<m && abs(v-Map[i][j])<=d){
                                vis[i][j]=1;
                                q.push(Node(i,j));
                            }
                        }
                    }
                }



            }
        }
        printf("%d\n",cnt);
    }
    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

*/


 

猜你喜欢

转载自blog.csdn.net/stone_fall/article/details/88764344
今日推荐