• 返回一个最大联通子数组的和


    #include<iostream>
    #include<ctime>
    using namespace std;
    #define N 100
    
    typedef struct
    {
        int dian[N];
        int xian[N][N];
        int dianx, xianx;
    }A;
    
    void set(A &shu, int x, int y)
    {
        shu.dianx = x*y;
        srand((unsigned)time(NULL));
        for (int i = 1; i <= shu.dianx; i++)
        {
            shu.dian[i] = rand() % 10;
            if (rand() % 2 == 1)
                shu.dian[i] = shu.dian[i] * (-1);
        }
        for (int i = 1; i <= shu.dianx; i += y)
        {
            for (int j = i; j <= i + y - 2; j++)
            {
                shu.xian[j][j + 1] = 1;
                shu.xian[j + 1][j] = 1;
            }
        }
        for (int i = 1 + y; i<shu.dianx; i += y)
        {
            for (int j = i; j <= i + x - 1; j++)
            {
                shu.xian[j][j - y] = 1;
                shu.xian[j - y][j] = 1;
            }
        }
    }
    void output(A shu)
    {
        for (int i = 1; i <= shu.dianx; i++)
        {
            cout << shu.dian[i] ;
            if (shu.xian[i][i + 1] == 1)
                cout << "  ";
            else
                cout << endl;
        }
    }
    void bianli(A &shu, int v, int visit[], int &b, int &max, int x)
    {
        visit[v] = 1;
    
        max += shu.dian[v];
        if (max >= b)
            b = max;
    
        int a = 0, bo = 0;
        for (int w = 1; w <= shu.dianx; w++)
        {
            for (int c = 1; c <= shu.dianx; c++)
            {
                if ((visit[w] == 0) && (shu.xian[c][w] == 1) && (visit[c] == 1))
                {
                    a = w; bo = 1; break;
                }
            }
            if (bo == 1)
                break;
        }
        for (int w = 1; w <= shu.dianx; w++)
        {
            for (int c = 1; c <= shu.dianx; c++)
            {
                if ((visit[w] == 0) && (shu.xian[c][w] == 1) && (visit[c] == 1))
                {
                    if (shu.dian[a]<shu.dian[w])
                        a = w;
                }
            }
        }
        if (b + shu.dian[a]<0)
        {
            shu.xian[v][a] = 0;
        }
        else
            bianli(shu, a, visit, b, max, x);
    }
    
    int NoVisit(int visit[], A shu)
    {
        int k = 0, i;
        for (i = 1; i <= shu.dianx; i++)
        {
            if (visit[i] == 0)
            {
                k = i;
                break;
            }
        }
        return k;
    }
    
    int main()
    {
        cout << "请输入数组行列数:" << endl;
        int x, y;
        cin >> x >> y;
        A shu;
        set(shu, x, y);
        output(shu);
    
        int v = 1, b[N] = { 0 }, h = 0;
        for (int i = 1; i <= shu.dianx; i++)
        {
            if (shu.dian[i]<0)
            {
                b[i] = shu.dian[i];
            }
            else
            {
                int visit[N] = { 0 };
                int max = 0;
                bianli(shu, i, visit, b[i], max, x);
            }
        }
    
        int max = b[1];
        for (int i = 2; i <= shu.dianx; i++)
        {
            if (b[i]>max)
                max = b[i];
        }
        cout << "最大联通子数组的和为:" << max << endl;
    }

    题目要求:

    输入一个二维整形数组,数组里有正数也有负数。

    求所有子数组的和的最大值。

     

    设计思想:这道题目感觉很难,第一次设想是求出每一行最大子数组的和,同时求出他们的坐标,比较他们是否在同一行,后来发现很难实现

    通过图的遍历可以查找

    结果:并不是自己所做,题目还是偏难。

  • 相关阅读:
    十二月十学习报告
    十二月八学习报告
    十二月七学习报告
    学习笔记187—在线会议共享PPT时,设置让观众看不到备注,而自己能看到【腾讯会议】
    学习笔记186—打印机可以打印测试页,但是通过WPS或Word无法打印文档?
    《程序员的自我修养》读书有感 其一
    Linux下的单向ping通问题
    做一个Pandas专家,教你如何用它高效处理大量数据
    grpc python 源码分析(1):server 的创建和启动
    grpc python 源码分析(2):server 处理请求
  • 原文地址:https://www.cnblogs.com/SanShaoS/p/4594480.html
Copyright © 2020-2023  润新知