程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 杭電1081——to the max(動態規劃和暴力法解)

杭電1081——to the max(動態規劃和暴力法解)

編輯:C++入門知識

To The Max


Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.

As an example, the maximal sub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

is in the lower left corner:

9 2
-4 1
-1 8

and has a sum of 15.
 

Input
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
 

Output
Output the sum of the maximal sub-rectangle.
 

Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2
 

Sample Output
15
 
題目我起先用暴力法來解答,不過,通不過,超時,後來參考了一下別人的思想吧!沒想到這道題還可以用動歸的方法解答,很吃驚啊!
代碼如下:
[cpp] 
//這個程序可以求出m*n的矩陣某一塊的最大值 
//這種搜索算法沒有錯誤!但是超時,因此還要進一步優化算法! 
//這道題的題意說的比較模糊,題目裡沒有說要多個測試數據,弄得我只處理了一次,導致老是錯誤,又找不到問題所在! 
/*
#include<iostream>
#include<cstdio>
using namespace std;
const int MAX=10010;
int arr[MAX];
 
int main()
{
    int s,max=-200;
    int we=0;
    int m,n;
    while(scanf("%d",&arr[0])!=EOF)
    {
    //cin>>arr[0];//輸入方陣的維數
    m=n=arr[0];
    for(int i=1;i<=arr[0]*arr[0];i++)
        cin>>arr[i];
 
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)//這裡是表示塊數的大小,i*j
            for(int p=1;m-p>=i-1 ;p++)
                for(int q=1;n-q>=j-1 ;q++)//搜索路線,自上而下,自左向右
                {
                    int cur=p+m*(q-1);
                    int count=0;
                    int temp=cur; 
                    int temp1=1;
                    s=0;
                    //cout<<"第"<<we<<"次搜索: "<<endl;
                    //cout<<"起點是 行數"<<p<<","<<"列數"<<q<<endl;
                    //cout<<"p="<<p<<",n="<<n<<endl;
                    for(int k=1;k<=i*j;k++)
                    {
                        //cout<<arr[temp]<<"  ";
                        s=s+arr[temp];
                        count++;
                        temp++;
                        if(count>=i)
                        {
                            temp=cur+temp1*m;
                            temp1++;
                            count=0;
                        }
                    }
                    //cout<<endl;
                    //we++;
                    if(s>max)
                    max=s;
                }
     cout<<max<<endl;
     //system("pause");
     }
    return 0;
}
*/ 
 
#include<iostream> 
#include<cstdio> 
using namespace std; 
 
int arr[101][101]; 
int sum[101],dp[101]; 
 
int main() 

     
    int i,j,k,p,n; 
    while(scanf("%d",&n)!=EOF) 
    {   
        int max=-200; 
        memset(sum,0,sizeof(sum)); 
        memset(dp,0,sizeof(dp)); 
        for(i=0;i<n;i++) 
            for(j=0;j<n;j++) 
            cin>>arr[i][j]; 
        //很難想到,這居然是一道動態規劃題 
        //1、最大子字段和 
    /*
    題目可以轉化為兩個子問題:
    1,給定n個整數(可能為負數)組成的序列a[1],a[2],a[3],…,a[n],求該序列如a+a+…+a[j]的子段和的最大值。當所給的整均為負數時定義子段和為0,依此定義,所求的最優值為Max{0,a+a+…+a[j]},1<=i<=j<=n
    我們把b[j]理解為從前面某項開始包含a[j](a[j]為最後一個元素)的連續的段的和最大值,易知,開始的a[i]必定為正數!
記b[j]=max(a+..+a[j-1]+a[j]),其中1<=i<=j,並且1<=j<=n,需要明確的一點是b[j]的值必須包含a[j]。則所求的最大子段和為max{b[j]},1<=j<=n。
    由b[j]的定義可易知,當b[j-1]>0時(即前面的段加上a[j]這一段值會更大,自然把a[j]這一段接上)b[j]=b[j-1]+a[j],否則(由於前面的段為負值,加上a[j],會使值變小,這樣的話,我們將前面的段去掉,a[j]即是最大值)b[j]=a[j]。故b[j]的動態規劃遞歸式為 b[j]=max(b[j-1]+a[j],a[j]),1<=j<=n。
    2、最大子矩陣和
 
    矩陣是二維的,將其壓縮成一維就可以用上面的方法求出最大子矩陣和了
    即將一層層的數相加,壓縮成一維的即可,我們遍歷所有的壓縮可能,就可以通過上面的方法,求出最大值!
    */  
        for(i=0;i<n;i++) 
        { 
            memset(sum,0,sizeof(sum)); 
            for(k=i;k<n;k++) 
            { 
                for(j=0;j<n;j++) 
                sum[j]=arr[k][j]+sum[j]; 
                    dp[0]=sum[0]; 
                    for(p=1;p<n;p++) 
                    { 
                        if (dp[p-1]>0) dp[p]=dp[p-1]+sum[p]; 
                        else dp[p]=sum[p]; 
                        if (dp[p]>max) max=dp[p]; 
                        /*
                        if(dp[p-1]+sum[p]>sum[p])
                            dp[p]=dp[p-1]+sum[p];
                        else
                            dp[p]=dp[p-1];
                        if(dp[p]>max) max=dp[p];
                        */ 
                    } 
                     
            } 
        } 
        cout<<max<<endl; 
        //system("pause"); 
    } 
    return 0; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved