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


    #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;
    }

    题目要求:

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

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

     

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

    通过图的遍历可以查找

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

  • 相关阅读:
    Python易忽略要点记录二
    Python使用的几个小技巧
    MySQL内容整理二
    C++面向对象知识总结
    AssemblyInfo 的作用
    中国新年第一篇
    function函数的运用
    C语言1博客作业03
    C语言1博客作业02
    .C# 拷贝一个图片到指定文件夹下(IO文件操作实例)
  • 原文地址:https://www.cnblogs.com/SanShaoS/p/4594480.html
Copyright © 2020-2023  润新知