最大聯通子數組

來源:互聯網
上載者:User

標籤:

題目:

    返回一個二維整數數組中最大聯通子數組的和。

要求:

   1. 輸入一個二維整形數組,數組裡有正數也有負數。 求所有子數組的和的最大值。要求時間複雜度為O(n)。    

   2.程式要使用的數組放在一個叫 input.txt 的檔案中, 檔案格式是: 數組的行數, 數組的列數, 每一行的元素, (用逗號分開) 每一個數字都是有符號32位整數,當然,行數和列數都是正整數。

設計思想:

      矩陣中的數是隨機產生的,可能會有正有負,所以要首先判斷二維數組中哪些位置上的數是正數,利用另一個二維數組記錄正數的位置,然後判斷哪些數是連通的,我的思路是先判斷兩個數的行數(列數)是否相同,如果相同,再判斷列數(行數)是否相鄰,如果相鄰即證明兩個數連通,計算每個連通塊的數值的和,然後再連通不同的塊,計算其和,最後比較其中的最大值,即為二維整數數組中最大聯通子數組的和。

來源程式代碼:

#include<iostream>#include<ctime>using namespace std;#define M 100#include<fstream>int Max(int n, int a[], int *sm, int *mm){    int b[100] = { 0 };    int i, Ssum = 0, Mmax= 0;    for (i = 0; i<n; i++)    {        if (Ssum<0)        {            Ssum = a[i];        }        else        {            Ssum = Ssum + a[i];        }        b[i] = Ssum;    }     Mmax = b[0];    for (i = 0; i<n; i++)    {        if ( Mmax<b[i])        {             Mmax = b[i];            *mm = i;        }    }    for (i = *mm; i >= 0; i--)    {        if (b[i] == a[i])        {            *sm = i;            break;        }    }    return Mmax;}void main(){    ofstream outfile;        outfile.open("a.txt");                      if(!outfile)    {        cerr<<"OPEN ERROR!"<<endl;        exit(0);    }    srand((_int32)time(NULL));    int m, n, i, j, sm, mm, max1;    int sum, max;    int line[M], list[M], t[M];    int STR[M][M], b[M];    cout << "二維數組的行:";    cin>>m ;    outfile<<m<<endl;    cout << "二維數組的列:";    cin>>n ;    outfile<<n<<endl;    for(i = 0; i <m; i++)    {        for (j = 0; j<n; j++)        {             STR[i][j]= rand() % 10;             if (rand() % 2 == 1)                 STR[i][j]= STR[i][j]*(-1);             cout<<STR[i][j]<<" ";             outfile<<STR[i][j]<<" ";        }        cout<<endl;        outfile<<endl;    }    for (i = 0; i<m; i++)    {        for (j = 0; j<n; j++)        {            b[j] = STR[i][j];        }        sum = Max(n, b, &sm, &mm);        line[i] = sm;        list[i] = mm;        t[i] = sum;    }    max1 = t[0];    for (i = 0; i + 1<m; i++)    {        if (line[i] <= list[i + 1] && list[i] >= line[i + 1])        {            max1 =max1+ t[i + 1];        }        for (j = line[i]; j<line[i + 1]; j++)        {            if (STR[i + 1][j]>0)                 max1 =max1+ STR[i + 1][j];                         }    }    cout <<"最大聯通子數組之和"<< max1 << endl;    outfile<<max1;}

 

運行結果:

 

 

 

 小組成員:楊濤  http://www.cnblogs.com/GloryYT/ 

       

 

最大聯通子數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.