zoukankan      html  css  js  c++  java
  • 最大联通子数组

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

    要求:

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

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

     

    队员:王楗  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:

  • 相关阅读:
    互质与欧拉函数学习笔记
    Luogu P4588 [TJOI2018]数学计算 题解
    Luogu P1072 Hankson 的趣味题 题解
    Luogu [POI2002][HAOI2007]反素数 题解
    Luogu P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题解
    JavaScript 实现简易版贪吃蛇(Day_13)
    IDEA 配置 Tomcat(详细)(Day_12)
    常用数据库连接池配置及使用(Day_11)
    大对象数据LOB的应用(Day_10)
    IDEA中配置maven 全解析教程(Day_08)
  • 原文地址:https://www.cnblogs.com/X-knight/p/5360777.html
Copyright © 2011-2022 走看看