输出二维数组连续二维子数组的最大和

package lianxu1;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ShowSub {

        public static void main(String[] args) throws IOException {
            Integer c[][]= {
                {1,2,-3,-1,2,2},
                {-3,4,5,1,-1,3},
                {-2,-3,4,1,4,3}
            };
            //求和
            List<List<Integer>> main=new ArrayList<List<Integer>>();
            for(int i=0;i<c.length;i++)
            {
                List<Integer> heng=new ArrayList<Integer>();
                for(int j=0;j<c[0].length;j++)
                {
                    if(j!=0)
                        heng.add(c[i][j]+heng.get(j-1));
                    else heng.add(c[i][j]);
                }
                if(i!=0)
                    main.add(addList(heng,main.get(i-1)));
                else main.add(heng);
            }
            //求最大值
            
            int max=main.get(0).get(0);
            for(int z=0;z<main.size();z++)
            {
                int temp=Collections.max(main.get(z));
                if(max<temp)
                {
                    max=temp;
                }
            }
            //确定一位置
            for(int i1=0;i1<main.size();i1++)
            {
                for(int j1=0;j1<main.get(0).size();j1++)
                {
                    //确定二位置
                    for(int i2=i1+1;i2<main.size();i2++)
                    {
                        for(int j2=j1+1;j2<main.get(0).size();j2++)
                        {
                            int g_max=0;
                            if(i1!=0&&j1!=0)
                            {
                                g_max=(main.get(i2).get(j2)+main.get(i1-1).get(j1-1)-main.get(i2).get(j1-1)-main.get(i1-1).get(j2));
                                
                            }
                            else if(i1!=0)
                            {
                                g_max=(main.get(i2).get(j2)-main.get(i1-1).get(j2));
                            }
                            else if(j1!=0)
                            {
                                g_max=(main.get(i2).get(j2)-main.get(i2).get(j1-1));
                            }
                            if(max<g_max)
                            {
                                max=g_max;
                            }
                        }
                    }
                }
            }
            System.out.println("该二维数组整理区域和为:"+main);
            System.out.println("该二维数组最大块区域和为:"+max);
        }
        public static List<Integer> addList(List<Integer> a,List<Integer> b)
        {
            List<Integer> sum=new ArrayList<Integer>();
            for(int i=0;i<a.size();i++)
            {
                sum.add(a.get(i)+b.get(i));
            }
//            for(int i1=0;i1<a.size();i1++)
//            {
//                sum.set(i1,sum.get(i1)+b.get(i1));
//            }
            return sum;
        }
        
}

目标排列为:

结果:

原文地址:https://www.cnblogs.com/yishaui/p/10953350.html