public static SubarrayInfo LineMaxSub(int[] a, int rowi) { int tempsub,maxsub,start=0,tempstart=0,end=0; int i,n; SubarrayInfo sub=new SubarrayInfo(); n = a.Length; tempsub=0; maxsub=a[n-1]; for(i=0;i<n;i++) { if(tempsub+a[i]<a[i]) { tempstart=i; tempsub=a[i]; } else tempsub+=a[i]; if(tempsub>maxsub) { maxsub=tempsub; start=tempstart; end=i; } } sub.max = maxsub; sub.start = start; sub.end = end; return sub; }
public static void Step1_Rect() { //for(int i=0;i<matrix.Length;i++) //for(int j=0;j<matrix[i].Length;j++) //System.Console.Write(matrix[i][j]+","); int column=matrix[0].Length; int row = matrix.Length; int i,j,k,tempsub,maxsub,cstart=0,cend=0,rstart=0,rend=0; int[] temparray=new int[column]; record = new int[row][]; SubarrayInfo sub = new SubarrayInfo(); for(i=0;i<row;i++) { record[i] = new int[column]; for(j=0;j<column;j++) if(i==0) record[i][j]=matrix[i][j]; else record[i][j]=record[i-1][j]+matrix[i][j]; } for(i=0,maxsub=matrix[0][0];i<row;i++) { for(j=0;j<=i;j++) { if(j==0) { sub=LineMaxSub(record[i],i); tempsub=sub.max; } else { for(k=0;k<column;k++) temparray[k]=record[i][k]-record[j-1][k]; sub=LineMaxSub(temparray,i); tempsub=sub.max; } if(tempsub>maxsub) { maxsub=tempsub; cstart=sub.start; cend=sub.end; rstart=j; rend=i; } } } for (i = rstart; i <= rend; i++) for (j = cstart; j <= cend; j++) inout[i][j] = true; }
// public void byStep1_Rect() { int i, j, k, tempsub, cstart = 0, cend = 0, rstart = 0, rend = 0; int[] temparray = new int[columnNum]; SubarrayInfo sub = new SubarrayInfo(); if (aimSRow == 0&&aimERow==0) { maxsub = matrix[0][0]; record = new int[rowNum][]; for (i = 0; i < rowNum; i++) { record[i] = new int[columnNum]; for (j = 0; j < columnNum; j++) if (i == 0) record[i][j] = matrix[i][j]; else record[i][j] = record[i - 1][j] + matrix[i][j]; } } if (aimSRow == 0) { sub = lineMaxSub(record[aimERow]); tempsub = sub.max; } else { for (k = 0; k < columnNum; k++) temparray[k] = record[aimERow][k] - record[aimSRow - 1][k]; sub = lineMaxSub(temparray); tempsub = sub.max; } if (tempsub > maxsub) { maxsub = tempsub; cstart = sub.start; cend = sub.end; rstart = aimSRow; rend = aimERow; for (i = 0; i < rowNum; i++) for (j = 0; j < columnNum; j++) if (i >= rstart && i <= rend && j >= cstart && j <= cend) inResult[i][j] = true; else inResult[i][j] = false; } }