• 最大联通子数组


    返回一个二维整数数组中最大联通子数组的和。

    要求:

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

    求所有子数组的和的最大值。下面是给的示例图:

     

    队员:王楗  http://home.cnblogs.com/u/wangjianly/

    #include <iostream>
    #include<fstream>
    #include <time.h>
    using namespace std;
    
    #define m 100
    #define n 100
    
    void main()
    {
        int a[m][n] = { 0 }, b[m][n] = { 0 };            //判断联通性,0为未选中,1为选中,2为连通
        bool flg = 0;                              //判断是否有1存在,存在为O。
        int sum = 0;                               //最后和
        int M, N;
        ofstream outfile;
        outfile.open("intput.txt");
        if (!outfile)
            {
             cerr << "OPEN ERROR!" << endl;
             exit(0);
            }
        cout << "输入数组的行数和列数:" << endl;
        cin >> M;
        outfile << M << endl;
        cin >> N;
        outfile << N << endl;
        srand(unsigned((int)time(0)));
    
        for (int i = 0; i < M; i++)
        {
            for (int j = 0; j < N; j++)
            {
                a[i][j] = rand() % 50 - 20;
                cout << a[i][j] << "	";
                outfile << a[i][j] << "	";
                if (a[i][j] >= 0)
                {
                    b[i][j] = 1;
                }
            }
            cout << endl;
            outfile <<endl;
        }
        cout << endl;
    
        for (int i = 0; i < M; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (b[i][j] == 1)
                {
                    if (a[i + 1][j] + a[i][j] > 0 && b[i + 1][j] == 0)
                    {
                        b[i + 1][j] = 2;
                    }
                    if (a[i - 1][j] + a[i][j] > 0 && b[i - 1][j] == 0)
                    {
                        b[i - 1][j] = 2;
                    }
                    if (a[i][j - 1] + a[i][j] > 0 && b[i][j - 1] == 0)
                    {
                        b[i][j - 1] = 2;
                    }
                    if (a[i][j + 1] + a[i][j] > 0 && b[i][j + 1] == 0)
                    {
                        b[i][j + 1] = 2;
                    }
                }
            }
        }
    
        for (int i = 0; i < M; i++)
        {
            for (int j = 0; j < N; j++)
            {
                flg = 0;
                if (b[i][j] != 0 && a[i][j] < 0)
                {
                    b[i][j] = 0;
                    for (int k = 0; k < M; k++)
                    {
                        for (int l = 0; l < N; l++)
                        {
                            if (b[k][l] != 0)
                            {
                                if ((b[k + 1][l] <= 0 || b[k + 1][l] > 2) && (b[k - 1][l] <= 0 || b[k - 1][l] > 2) && (b[k][l + 1] <= 0 || b[k][l + 1] > 2) && (b[k][l - 1] <= 0 || b[k][l - 1] > 2))
                                {
                                    flg = 1;
                                }
                            }
                        }
                    }
                    if (flg)
                    {
                        b[i][j] = 2;
                    }
                }
            }
        }
    
        for (int i = 0; i < M; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (b[i][j] != 0)
                {
                    cout << a[i][j] << "	";
                    sum += a[i][j];
                }
                else
                {
                    cout << "**" << "	";
                }
            }
            cout << endl;
        }
        cout << "sum = " << sum << endl;
        outfile << "sum = " << sum << endl;
        outfile.close();
    }

    运行结果:

    测试数据:3*4

    测试数据4*4:

  • 相关阅读:
    <转>反调试技巧总结原理和实现
    MFC CListCtrl 表格
    <转>汇编指令
    c++ builder 简单读、分析网页数据
    <转>CProcessData : A template class to ease up SendMessage calls across processes
    <转>Running console applications silently
    遍历电脑打印机、设置默认打印机、EnumPrinters ,SetDefaultPrinter,GetDefaultPrinter
    <转>运算符巧妙原理解析
    遍历 进程
    Enterprise Library5.0 Unity 试用.
  • 原文地址:https://www.cnblogs.com/X-knight/p/5360777.html
Copyright © 2020-2023  润新知