hdu 2666 Matrix

Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1332    Accepted Submission(s): 740

Problem Description

Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.

Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom
can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end.

Input

The input contains multiple test cases.

Each case first line given the integer n (2<n<30)

Than n lines,each line include n positive integers.(<100)

Output

For each test case output the maximal values yifenfei can get.

Sample Input

2
10 3
5 10
3
10 3 3
2 5 3
6 7 10
5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9

Sample Output

28
46
80

双线程DP

#include"stdio.h"
#include"string.h"
#define N 32
int num[N][N];
int f[N][N][N][N];
int Max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int i,j,k,l,n;
	while(scanf("%d",&n)!=-1)
	{
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				scanf("%d",&num[i][j]);
		memset(f,0,sizeof(f));
		for(i=1;i<=n;i++)      //一条路线从(1,1)点向右出发
		{
			for(j=1;j<=n;j++)
			{
				for(k=i+1;k<=n;k++)      //二路线从第二行开始,即从(1,1)点向下出发
				{
					l=i+j-k;              //因为两条路线的步数相等,则二路线的纵坐标容易求得
					if(l<=0)
						break;          //该点的值可以从四个路线得到,下下,右右,下右,右下
					f[i][j][k][l]=Max(Max(f[i-1][j][k-1][l],f[i][j-1][k][l-1]),Max(f[i-1][j][k][l-1],f[i][j-1][k-1][l]));
					f[i][j][k][l]+=(num[i][j]+num[k][l]);           //加上该路线的值
				}
			}
		}
		int t=Max(Max(f[n-1][n][n-1][n],f[n][n-1][n][n-1]),Max(f[n-1][n][n][n-1],f[n][n-1][n-1][n]));
		printf("%d\n",t+num[n][n]+num[1][1]);       //起点和终点的值
	}
	return 0;
}

hdu 2666 Matrix,码迷,mamicode.com

时间: 2024-11-13 07:44:58

hdu 2666 Matrix的相关文章

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

HDU 4902 Matrix multiplication

点击打开链接 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2113    Accepted Submission(s): 956 Problem Description Given two matrices A and B of size n×n, find the product o

HDU 2686 Matrix(最大费用最大流+拆点)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(因为有两个方向的选择)即可 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int ma

HDU多校赛第9场 HDU 4965Fast Matrix Calculation【矩阵运算+数学小知识】

难度上,,,确实,,,不算难 问题是有个矩阵运算的优化 题目是说给个N*K的矩阵A给个K*N的矩阵B(1<=N<=1000 && 1=<K<=6),先把他们乘起来乘为C矩阵,然后算C^(N*N) 相当于 ABABABABABABAB...=(AB)^(N*N) 不如 A(BA)^(N*N-1)B 因为BA乘得K*K的矩阵,K是比较小的 #include <cstdio> #include <cstdlib> #include <cstr

HDU 2830 Matrix Swapping II (DP,最大全1矩阵)

题意  给你一个n*m矩阵  每列都可以随便交换位置   求最优交换后最大的全1子矩阵 又是HDU 1505 1506的变种  但这个更容易了  因为每列都可以交换位置了   那么这一行中所有比i高的都可以与i相邻了  只需要统计这一行有多少个比i高就行了   可以在算出每一行后  把高度大的放前面去  用num[i]记录排序后的列原来的数  这样就有j列比h[i][num[j]]高了  最后的答案也就是max(j*h[i][num[j]]) #include<cstdio> #include

HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊.没想到是处理掉0的情况就可以过啊.一直以为会有极端数据.没想到竟然是这样的啊..在网上看到了一个AC的神奇的代码,经典的矩阵乘法,只不过把最内层的枚举,移到外面就过了啊...有点不理解啊,复杂度不是一样的吗.. Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 640 

hdu 4920 Matrix multiplication(矩阵乘法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 989    Accepted Submission(s): 396 Problem Description Given two matr

HDU 4920 Matrix multiplication

Problem Description Given two matrices A and B of size n×n, find the product of them. bobo hates big integers. So you are only asked to find the result modulo 3. Input The input consists of several tests. For each tests: The first line contains n (1≤

HDU 4920 Matrix multiplication(bitset)

HDU 4920 Matrix multiplication 题目链接 题意:给定两个矩阵,求这两个矩阵相乘mod 3 思路:没什么好的想法,就把0的位置不考虑,结果就过了.然后看了官方题解,上面是用了bitset这个东西,可以用来存大的二进制数,那么对于行列相乘,其实就几种情况,遇到0都是0了,1 1得1,2 1,1 2得2,2 2得1,所以只要存下行列1和2存不存在分别表示的二进制数,然后取且bitcount一下的个数,就可以计算出相应的数值了 代码: 暴力: #include <cstdi