CodeForces 173C Spiral Maximum

题意:给你一个n*m的矩阵,然后问你螺旋线能够覆盖的最大和是多少

思路

暴力记忆化搜索,滚动数组优化

dp[i][j][len]表示以i,j为起点,正方形边长为len的覆盖的值是多少

dp[i][j][len]显然等于len所在的正方形覆盖的和 - mp[i+1][j] - dp[i+1][j+1][len-2]


#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
int n,m;
int mapp[510][510];
int sum[510][510];
int vis[510][510];
int dp[510][510][2];
int cal(int x1,int y1,int x2,int y2)
{
	return sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
}
int solve(int x,int y,int len)
{
	if (len==0)
	{
		dp[x][y][0]=mapp[x][y];
		return dp[x][y][0];
	}
	else
	{
		dp[x][y][0]=cal(x,y,x+len,y+len);
		dp[x][y][0]-=mapp[x+1][y];
		dp[x][y][0]-=dp[x+1][y+1][1];
		return dp[x][y][0];
	}
}
int main()
{
    scanf("%d%d",&n,&m);
	for (int i = 1;i<=n;i++)
		for (int j = 1;j<=m;j++)
			scanf("%d",&mapp[i][j]);
	for (int i = 1;i<=n;i++)
		for (int j = 1;j<=m;j++)
			sum[i][j]=mapp[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
	int ans = -INF;
	for (int k = 0;k<=min(n,m);k+=2)
	{
		memset(vis,0,sizeof(vis));
		for (int i = n;i>=1;i--)
			for (int j = m;j>=1;j--)
				dp[i][j][1]=dp[i][j][0];
		for (int i = n;i>=1;i--)
		{
			if (i+k>n)
				continue;
			for (int j = m;j>=1;j--)
			{
				if (j+k>m)
					continue;
				int p = solve(i,j,k);
				if (k>0)
					ans = max(ans,p);
			}
		}
	}
	printf("%d\n",ans);
}

Description

Let's consider a k × k square, divided into unit squares. Please note that k ≥ 3 and is odd. We'll paint squares starting from the upper left square in the following order: first we move to the right, then down, then to the left, then up, then to the right again and so on. We finish moving in some direction in one of two cases: either we've reached the square's border or the square following after the next square is already painted. We finish painting at the moment when we cannot move in any direction and paint a square. The figure that consists of the painted squares is a spiral.

 The figure shows examples of spirals for k = 3, 5, 7, 9.

You have an n × m table, each of its cells contains a number. Let's consider all possible spirals, formed by the table cells. It means that we consider all spirals of any size that don't go beyond the borders of the table. Let's find the sum of the numbers of the cells that form the spiral. You have to find the maximum of those values among all spirals.

Input

The first line contains two integers n and m (3 ≤ n, m ≤ 500) — the sizes of the table.

Each of the next n lines contains m space-separated integers: the j-th number in the i-th line aij ( - 1000 ≤ aij ≤ 1000) is the number recorded in the j-th cell of the i-th row of the table.

Output

Print a single number — the maximum sum of numbers among all spirals.

Sample Input

Input
6 5
0 0 0 0 0
1 1 1 1 1
0 0 0 0 1
1 1 1 0 1
1 0 0 0 1
1 1 1 1 1
Output
17
Input
3 3
1 1 1
1 0 0
1 1 1
Output
6
Input
6 6
-3 2 0 1 5 -1
4 -1 2 -3 0 1
-5 1 2 4 1 -2
0 -2 1 3 -1 2
3 1 4 -3 -2 0
-1 2 -1 3 1 2
Output
13

Hint

In the first sample the spiral with maximum sum will cover all 1's of the table.

In the second sample the spiral may cover only six 1's.



转载于:https://www.cnblogs.com/q934098774/p/5388696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值