HDU1081 (最大连续子序列/矩阵)
生活随笔
收集整理的這篇文章主要介紹了
HDU1081 (最大连续子序列/矩阵)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
將二維轉化為一維。
第一個FOR循環用于枚舉 行,第二個和第三個FOR 用于求從當前行(J)開始的每一個矩陣方塊和,從而得到ANS。
View Code 1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 const int maxn = 105; 5 int a[ maxn ][ maxn ],temp[ maxn ]; 6 int n; 7 8 int dp( ){ 9 int ans=temp[ 0 ]; 10 int tmp_sum=0; 11 for( int i=0;i<n;i++ ){ 12 tmp_sum+=temp[ i ]; 13 if( ans<tmp_sum ) ans=tmp_sum; 14 if( tmp_sum<0 ) tmp_sum=0; 15 } 16 return ans; 17 } 18 19 int main(){ 20 while( scanf("%d",&n)!=EOF ){ 21 for( int i=0;i<n;i++) 22 for( int j=0;j<n;j++ ) 23 scanf("%d",&a[ i ][ j ]); 24 int ans=-9999999; 25 for( int i=0;i<n;i++ ){ 26 memset( temp,0,sizeof(temp) ); 27 for( int j=i;j<n;j++ ){ 28 for( int k=0;k<n;k++ ){ 29 temp[ k ]+=a[ j ][ k ]; 30 } 31 ans=max( ans,dp() ); 32 } 33 } 34 printf("%d\n",ans); 35 } 36 return 0; 37 }?
轉載于:https://www.cnblogs.com/xxx0624/archive/2013/01/28/2880509.html
總結
以上是生活随笔為你收集整理的HDU1081 (最大连续子序列/矩阵)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么QQ传文件比MSN快 揭密背后原因
- 下一篇: Jquery checkbox全选简单用