设为首页 加入收藏

TOP

动态规划和暴力法解(一)
2013-01-09 13:41:44 来源: 作者: 【 】 浏览:510
Tags:动态 规划 暴力

  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的矩阵某一块的最大值

  //这种搜索算法没有错误!但是超时,因此还要进一步优化算法!

     

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++之static静态修饰符详解 下一篇C++中的位拷贝与值拷贝浅谈

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: