Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >递归查找矩阵连通域

递归查找矩阵连通域

作者头像
零式的天空
发布于 2022-03-22 02:54:15
发布于 2022-03-22 02:54:15
47600
代码可运行
举报
文章被收录于专栏:零域Blog零域Blog
运行总次数:0
代码可运行

题目的来源是给定一张图片,查找所有临近的像素点,并求出最大像素值。经过抽象后是:两个矩阵,一个只是包含0 1,另一个是每个位置具体的像素值,可以通过查找第一个矩阵来确定连通域的点,根据第二个矩阵得出最大的值。

矩阵1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# data
1 0 0 0 1 0 0 0 0 1
0 1 0 0 1 0 0 0 0 0
0 1 1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0
0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 1
1 0 0 0 1 0 0 0 0 1
0 1 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 1 0 0 1 0 0 0 0 0

矩阵2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# data2
1 0 0 0 1 0 0 0 0 1
0 2 0 0 3 0 0 0 0 0
0 1 1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 4 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0
0 0 0 0 0 0 5 1 1 0
0 0 0 0 0 0 0 0 0 1
1 0 0 0 1 0 0 0 0 1
0 6 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 7 0 0 8 0 0 0 0 0

C++代码为:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// recursionFindPoints.cpp

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef double    db;

long flag = 2;
long START = flag;

int recursion(ll i, ll j, vector<vector<ll> > &data, vector<pair<ll,ll> > &position);
int launch( vector<vector<ll> > &data, vector<vector<db> > &data2);

int main(int argc, char * argv[])
{
    if (argc != 3)
    {
        cout<<"Enter two file"<<endl;
        return -1;
    }
    // 1 read map and intensity
    ifstream fin(argv[1]);
    ifstream fin2(argv[2]);

    vector<vector<ll> > data;
    vector<vector<db> > data2;
    vector<ll> temp;
    ll v;
    vector<db> temp2;
    db d;

    string line;
    string value;
    while (getline(fin,line))
    {
        stringstream ss(line);
        while (getline(ss,value,' '))
        {
           v = atoi(value.c_str());  
           temp.push_back(v);
        }
        data.push_back(temp);
        temp.clear();
    }	

    while (getline(fin2,line))
    {
        stringstream ss(line);
        while (getline(ss,value,' '))
        {
           d = atoi(value.c_str());  
           temp2.push_back(d);
        }
        data2.push_back(temp2);
        temp2.clear();
    }	

    // 2 entrance
    launch(data,data2);

    // 3 output result
    for (vector<vector<ll> >::iterator it = data.begin(); it != data.end(); ++it)
    {
        for (vector<ll>::iterator i = (*it).begin(); i != (*it).end(); ++i)
            cout<<*i<<" ";
        cout<<endl;
    }

}

int launch(vector<vector<ll> > &data, vector<vector<db> > &data2)
{
    // 1. Find all points in connected domain and store these coordinate
    vector<vector<pair<ll,ll> > > result;
    vector<pair<ll,ll> >  temp;
    for (int i = 0; i < data.size(); ++i)
    {
        for (int j = 0; j < data[0].size(); ++j)
        {
            if (data[i][j] == 1)
            {
               temp.push_back(make_pair(i,j)); 
               data[i][j] = flag;
               recursion(i,j,data,temp);
               flag++;
               result.push_back(temp);
               temp.clear();
            }
        }
    }

    // 2. Filter
    pair<ll,ll> pos;
    db inten = 0;
    for (vector<vector<pair<ll,ll> > >::iterator it = result.begin(); it != result.end(); ++it)
    {
        for (vector<pair<ll, ll> >::iterator jt = (*it).begin(); jt != (*it).end(); ++jt)
        {
            // cout<<(*jt).first<<" "<<(*jt).second<<"    ";
            if (inten <= data2[(*jt).first][(*jt).second])
            {
                inten = data2[(*jt).first][(*jt).second];
                pos.first = (*jt).first;
                pos.second = (*jt).second;
            }
        }
        // cout<<endl;
        printf("The %d points's max intensity is: %d\n", int(START), int(inten));
        START ++;
        inten = 0;
    }

    return 0;
}

int recursion(ll i, ll j, vector<vector<ll> > &data, vector<pair<ll,ll> > &position)
{
    for (int x = i - 1; x <= i + 1; x++)
    {
        for (int y = j - 1; y <= j + 1; y++)
        {
            if (x == i && y == j)
                continue;
            if (x >= 0 && x < data.size() && y >= 0 && y < data[0].size() && data[x][y] == 1)
            {
                position.push_back(make_pair(x, y));
                data[x][y] = flag;
                recursion(x,y,data,position);
            }
        }
    }
    return 0;
}

运行程序及结果:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$ g++ recursionFindPoints.cpp -o rfp
$ ./rfp data data2
The 2 points's max intensity is: 2
The 3 points's max intensity is: 3
The 4 points's max intensity is: 1
The 5 points's max intensity is: 1
The 6 points's max intensity is: 4
The 7 points's max intensity is: 1
The 8 points's max intensity is: 5
The 9 points's max intensity is: 6
The 10 points's max intensity is: 1
The 11 points's max intensity is: 1
The 12 points's max intensity is: 7
The 13 points's max intensity is: 8
2 0 0 0 3 0 0 0 0 4 
0 2 0 0 3 0 0 0 0 0 
0 2 2 0 3 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 5 
0 0 0 0 6 0 0 0 0 0 
0 0 0 6 0 0 0 0 0 0 
7 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 8 8 0 0 0 
0 0 0 0 0 0 8 8 8 0 
0 0 0 0 0 0 0 0 0 8 
9 0 0 0 10 0 0 0 0 8 
0 9 0 0 10 0 0 0 0 0 
0 0 0 0 0 0 0 11 0 0 
0 12 0 0 13 0 0 0 0 0

运行结果分两部分,第一部分是找到的每个连通域中点的最大值,第二部分是在第一个矩阵的基础上对连通域进行标号区分之后的矩阵

程序使用递归来查找一个九宫格的中心对周围八个点的关系,几行代码即可实现,可见递归的精妙,缺点是递归有最大层数,如果超过了会导致堆栈溢出,所以不能应用于太大的矩阵

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-04-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Codeforce #566 A B C(模拟) E(矩阵快速幂+欧拉降幂)
C.将数量相同但不是同一个结尾的放一个vector里,数量相同结尾相同放另一个vector里。
用户2965768
2019/07/04
5870
河工院首届工业设计大赛程序组(选拔赛)题解
本题为简单的01背包,不过是需要算一下超重多少,但是最多超重100%,那么代码就如下。
浪漫主义狗
2024/04/28
990
18级个人训练赛--2
B --Consecutive Integers AtCoder 5037 思路:水题,签到~
杨鹏伟
2020/09/11
3100
洛谷P1081 开车旅行(倍增)
\(da[i][j]\)同理表示\(a\)的距离,\(db[i][j]\)与\(da\)同理
attack
2019/01/30
3670
又是一个安静的周末,又是一个水水的周赛
给定一个表示正整数的字符串 s,输出字符串中的最大奇数子字符串,如果不存在奇数,返回空字符串
ACM算法日常
2021/07/16
2800
codeforce 266c Below the Diagonal 矩阵变换 (思维题)
You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix:
风骨散人Chiam
2020/10/28
3820
SDUT 2021 Spring Individual Contest – A
给你一个算式,问你这个算式中的数字在(1-36进制中)哪些进制下是合法的,输出所有合法的进制(10-35进制用a-z表示,36进制用0表示),否则输出invalid。只有满足所有数字的表示合法以及算式运算也合法才算合法。注意1进制此题规定只含有一个数字1,转换成十进制就是看有几个1(如1111表示十进制的4)。
Here_SDUT
2022/08/08
2820
AtCoder Beginner Contest 272(A~D)
A. Integer Sum ---- Origional Link 题目大意: N 个数求和。 ---- 思想: 签到题。 ---- 代码: #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <sstream> #include <vector> #include <queue> #include <stack> #include <map> #in
浪漫主义狗
2022/10/28
3680
SDUT编译原理上机测试
这题需要注意的是,对于句子:E \rightarrow TG 和 T \rightarrow FS 需要先判断一下 SELECT 集再进行递归,如果输入字符不属于 SELECT 集则直接输出 ERROR。
Here_SDUT
2022/09/19
9810
XMU oj Problem List
注意不要有不必要的输出,比如"请输入 a 和 b 的值: ",示例代码见隐藏部分。
glm233
2020/09/28
8290
LeetCode 867. 转置矩阵
1. 题目 给定一个矩阵 A, 返回 A 的转置矩阵。 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1,4,7],[2,5,8],[3,6,9]] 示例 2: 输入:[[1,2,3],[4,5,6]] 输出:[[1,4],[2,5],[3,6]] 提示: 1 <= A.length <= 1000 1 <= A[0].length <= 1000 来源:力扣(LeetCode) 链接:https:/
Michael阿明
2022/11/26
3090
Codeforces Global Round 15 (A-F)
有一个字符串,可以选择任意个字符任意调换他们的位置,求选择最少数量的字符调换他们的位置使得调换后字符串按字典序排列。
Here_SDUT
2022/08/11
3340
Codeforces Global Round 15 (A-F)
洛谷P4027 [NOI2007]货币兑换(dp 斜率优化 cdq 二分)
设\(f[i]\)表示到第\(i\)天所持有软妹币的最大数量,显然答案为\(max_{i = 1}^n f[i]\)
attack
2019/01/03
3820
《图》相关刷题
用户11039529
2024/04/10
860
河南工程学院2022级新生周赛(三)题解
A. 6男 ---- 原题链接 题目大意: 给定一个字符串 S,求最长的连续的 6 的字串的长度。 S 可能含有空格。 ---- 思想: 签到题。 读入时注意空格。 ---- 代码: #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <sstream> #include <vector> #include <queue> #include <stac
浪漫主义狗
2022/10/09
3000
牛客小白月赛22 A~~J
A.链接:https://ac.nowcoder.com/acm/contest/4462/A 来源:牛客网
杨鹏伟
2020/09/11
3900
opencv相机标定示例代码
https://blog.csdn.net/dcrmg/article/details/52939318
用户1148525
2019/05/27
1.9K0
码蹄集新手村100题答案
码蹄集是今年新上线的一个OJ平台,内含了100道基础题和一些百度之星的题目。由于很多题目有原创性,搜不到相关解答,因此我花了两天特将100道题目刷了一遍,目前位居榜二。 码蹄集传送门:https://www.matiji.net/exam/ojquestionlist 前言 所有题目均能AC,不一定是最佳方法,如有其它方法,可在评论区留言讨论。 1、程序设计入门 #include <iostream> using namespace std; int main( ) { cout <
zstar
2022/06/14
6230
码蹄集新手村100题答案
【算法竞赛】Namomo Winter 2023 Day 3 Div 2
Dashboard - 2017-2018 ACM-ICPC, NEERC, Northern Subregional Contest - Codeforces
Livinfly
2023/01/11
3470
IME++ Starters Try-outs 2019 题解
显然如果有多棵树,则一定会存在无法到达的点。否则直接暴力 b f s bfs bfs求每个点到其余点的距离, a n s ans ans取 m a x max max即可
dejavu1zz
2020/12/02
5860
IME++ Starters Try-outs 2019 题解
相关推荐
Codeforce #566 A B C(模拟) E(矩阵快速幂+欧拉降幂)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验