Maximum sum on a torus UVA

Maximum sum on a torus
Input: 
Standard Input

Output: Standard Output

 

A grid that wraps both horizontally and vertically is called a torus. Given a torus where each cell contains an integer, determine the sub-rectangle with the largest sum. The sum of a sub-rectangle is the sum of all the elements in that rectangle. The grid below shows a torus where the maximum sub-rectangle has been shaded.

 

1

-1

0

0

-4

2

3

-2

-3

2

4

1

-1

5

0

3

-2

1

-3

2

-3

2

4

1

-4

Input

The first line in the input contains the number of test cases (at most 18). Each case starts with an integer N (1≤N≤75) specifying the size of the torus (always square). Then follows N lines describing the torus, each line containing N integers between -100 and 100, inclusive.

 

Output

For each test case, output a line containing a single integer: the maximum sum of a sub-rectangle within the torus.

 

Sample Input                                  Output for Sample Input

2
5
1 -1 0 0 -4
2 3 -2 -3 2
4 1 -1 5 0
3 -2 1 -3 2
-3 2 4 1 -4
3
1 2 3
4 5 6
7 8 9
15

45



也可参考:http://blog.csdn.net/vinci_lau/article/details/7078968(利用动态规划解决的问题)


 以下是自己利用前缀和解决的问题;

关键是:

因为torus存在一种“可包围循环wrap”的情况

所以必须把给定的NXN的矩阵变成2NX2N的矩阵

然后利用前缀和,在枚举的时候注意枚举的矩阵必须包含在

NXN的矩阵中(即行列不超过N)

代码如下

max(0,j-n)//防止j-n<0

#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
int grid[160][160];
int sum[160][160];
int main()
{
    int t;
    scanf("%d",&t);
    for(int i = 0;i<t;++i)
    {
        int n;
        scanf("%d",&n);
        for(int j = 1;j<=n;++j)
          for(int k = 1;k<=n;++k)
             {
                 scanf("%d",&grid[j][k]);
                 grid[j+n][k] = grid[j][k];
                 grid[j][k+n] = grid[j][k];
                 grid[j+n][k+n] = grid[j][k];
             }
        for(int j = 0;j<=2*n;++j)
        {
            sum[0][j] = 0;
            sum[j][0] = 0;
        }

        for(int j = 1;j<=2*n;++j)
            for(int k = 1;k<=2*n;++k)
                sum[j][k] = sum[j][k-1] + sum[j-1][k] - sum[j-1][k-1] + grid[j][k];

        int maxn = grid[1][1];
       for(int j = 2*n;j>=1;--j)
         for(int k = 2*n;k>=1;--k)
           for(int a =max(0,j-n) ;a<j;++a)
             for(int b =max(k-n,0);b<k;++b)
             {
                 int temp = sum[j][k] - sum[j][b] - sum[a][k] + sum[a][b];
                 maxn = max(maxn,temp);
             }

          printf("%d\n",maxn);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
extern UFUNEXPORT int UF_MODL_ask_face_data( tag_t face ,/* <I> Face obj_id / int * type ,/ <O> Face type is NX surface type code 16 = cylinder 17 = cone 18 = sphere 19 = revolved (toroidal) 20 = extruded 22 = bounded plane 23 = fillet (blend) 43 = b-surface 65 = offset surface 66 = foreign surface / double point[] ,/ <O,len:3> Point information is returned according to the value of type as follows. Plane = Position in plane Cylinder= Position on axis Cone = Position on axis Sphere = Center position Torus = Center position Revolved = Position on axis / double dir[] ,/ <O,len:3> Direction information is returned according to the value of type as follows. Plane = Normal direction Cylinder= Axis direction Cone = Axis direction Torus = Axis direction Revolved = Axis direction / double box[] ,/ <O,len:6> Face boundary. The coordinates of the opposite corners of a rectangular box with sides parallel to X, Y, and Z axes (Absolute Coordinate System) are returned. The box contains the specified face and is usually close to the minimum possible size, but this is not guaranteed. box[0] = Xmin box[1] = Ymin box[2] = Zmin box[3] = Xmax box[4] = Ymax box[5] = Zmax / double * radius ,/ <O> Face major radius: For a cone, the radius is taken at the point[3] position on the axis. For a torus, the radius is taken at the major axis. / double * rad_data ,/ <O> Face minor radius: only a torus or cone has rad_data as a minor radius. For a cone, rad_data is the half angle in radians. For a torus, rad_data is taken at the minor axis. / int * norm_dir / <O> Face normal direction: +1 if the face normal is in the same direction as the surface normal (cross product of the U- and V-derivative vectors), -1 if reversed. */ ); 详解
最新发布
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值