标签:blog io ar os sp for 数据 div on
传说HMH大沙漠中有一个M*N迷宫,里面藏有许多宝物。某天,Dr.Kong找到了迷宫的地图,他发现迷宫内处处有宝物,最珍贵的宝物就藏在右下角,迷宫的进出口在左上角。当然,迷宫中的通路不是平坦的,到处都是陷阱。Dr.Kong决定让他的机器人卡多去探险。
但机器人卡多从左上角走到右下角时,只会向下走或者向右走。从右下角往回走到左上角时,只会向上走或者向左走,而且卡多不走回头路。(即:一个点最多经过一次)。当然卡多顺手也拿走沿路的每个宝物。
Dr.Kong希望他的机器人卡多尽量多地带出宝物。请你编写程序,帮助Dr.Kong计算一下,卡多最多能带出多少宝物。2 2 3 0 10 10 10 10 80 3 3 0 3 9 2 8 5 5 7 100
120 134
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
const int M = 52;
int map[M][M];
int dp[M+M][M][M]; // dp[k][i][j]: 第k步时,双线里 一线在i位置(横坐标为i,纵坐标为k-i) 另一线在j位置(横坐标为j,纵坐标为k-j)的最大和
int main()
{
int T;cin >> T;
while(T--){
int row , col;
cin >> row >> col;
memset(dp,0,sizeof(dp));
for(int i = 1 ; i <= row ; i++)
for(int j = 1 ; j <= col ; j++)
scanf("%d" , &map[i][j]);
int beg = map[1][1];
int end = map[row][col];
map[1][1] = map[row][col] = 0;
int bound = col + row;
for(int k = 1 ; k <= bound ; k++){ // 起点和终点为唯一交叉处,特判
for(int x1 = 1 ; x1 <= row ; x1++){
for(int x2 = 1 ; x2 <= row ; x2++){
if (x1 == x2 && !(k==bound && x1==row))continue;
int y1 = k - x1;
int y2 = k - x2;
if ( !(1<=y1 && y1<=col) || !(1<=y2 && y2<=col))continue;
dp[k][x1][x2] = max(max(dp[k-1][x1-1][x2-1],
dp[k-1][ x1 ][x2-1]),
max(dp[k-1][x1-1][ x2 ],
dp[k-1][ x1 ][ x2 ]));
dp[k][x1][x2] += map[x1][y1] + map[x2][y2];
}
}
}
cout << beg + dp[bound][row][row] + end << endl;
}
return 0;
}
标签:blog io ar os sp for 数据 div on
原文地址:http://www.cnblogs.com/a972290869/p/4099944.html