码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-06-23 07:34:06      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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

题目要求:

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

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

 技术分享

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

通过图的遍历可以查找

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

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

标签:

原文地址:http://www.cnblogs.com/SanShaoS/p/4594480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!