Kaka's Matrix Travels(减弱版) DP版

时间:2021-09-22 04:06:15

Kaka's Matrix Travels(减弱版)

Time Limit:5000MS  Memory Limit:65536K
Total Submit:15 Accepted:8

Description

On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUM Kaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his 2th travel. Note the SUM is accumulative during the 2 travels.

Input

The first line contains one integers N (1 ≤ N ≤ 50) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000.

Output

The maximum SUM Kaka can obtain after his 2th travel.

Sample Input

3
1 2 3
0 2 1
1 4 2

 

Sample Output

15

 

Source

TLE版本1:

TLE原因:

travel函数中每个点更新时只要比当前值大就更新一次,而事实上只需要从四种情况中那个最大的情况往下走就好了,所以每个点不只走一次!必然超时!

 

TLE版本2:

 

 

TLE原因:

现在改成4个for循环,复杂度 50*50*50*50*4;

Memory:26648K  Time:3546MS
是不是还可以减少状态呢?

我是同步的走,由于每次只能忘右或下走,所以坐标必然是递增的,所以……同步走道的两个点P1,P2必然能够保证P1.x+P1.y==P2.x+P2.y

 

所以……就出现了AC代码:

 

 

if (i+j==k+t) 多了这个限制条件,事情就好办多了!

Memory:26452K  Time:296MS

当然,这道题中当路的条数不确定时不能弄2*n维DP……通解是网络流,貌似最大费用流吧,等我做了再说吧!