Filthy Rich(动态规划)
来源:互联网 发布:ubuntu文档 编辑:程序博客网 时间:2024/05/29 14:36
Filthy Rich
Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
They say that in Phrygia, the streets are paved with gold. You’re currently on vacation in Phrygia, and to your astonishment you discover that this is to be taken literally: small heaps of gold are distributed throughout the city. On a certain day, the Phrygians even allow all the tourists to collect as much gold as they can in a limited rectangular area. As it happens, this day is tomorrow, and you decide to become filthy rich on this day. All the other tourists decided the same however, so it’s going to get crowded. Thus, you only have one chance to cross the field. What is the best way to do so?
Given a rectangular map and amounts of gold on every field, determine the maximum amount of gold you can collect when starting in the upper left corner of the map and moving to the adjacent field in the east, south, or south-east in each step, until you end up in the lower right corner.
Input
The input starts with a line containing a single integer, the number of test cases.
Each test case starts with a line, containing the two integers r and c, separated by a space (1 <= r, c <= 1000). This line is followed by r rows, each containing c many integers, separated by a space. These integers tell you how much gold is on each field. The amount of gold never negative.
The maximum amount of gold will always fit in an int.
Output
For each test case, write a line containing “Scenario #i:”, where i is the number of the test case, followed by a line containing the maximum amount of gold you can collect in this test case. Finish each test case with an empty line.
Sample Input
1
3 4
1 10 8 8
0 0 1 8
0 27 0 4
Sample Output
Scenario #1:
42
题意:首行给出T,代表有T组数据。每组数据第一行给出R,C代表接着有R行C列的数据。起点在左上角,终点在右下角,每一步可以向下向右向右下走,输出途中最大数字和。
思路:动态规划,DP[i][j]代表到达(i,j)能获取的最大数字和
状态转移方程
DP[i][j]=max(DP[i][j],DP[i][j-1]+map[i][j]),j-1>=0;
DP[i][j]=max(DP[i][j],DP[i-1][j]+map[i][j]),i-1>=0;
DP[i][j]=max(DP[i][j],DP[i-1][j-1]+map[i][j]),i-1>=0,j-1>=0;
代码
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<math.h>#include<queue>using namespace std;const int maxn=1002;int map[maxn][maxn];int DP[maxn][maxn];int main(){ int T; scanf("%d",&T); for(int casen=1; casen<=T; casen++) { int R,C;//地图尺寸 scanf("%d%d",&R,&C); for(int i=0; i<R; i++) { getchar(); for(int j=0; j<C; j++) scanf("%d",&map[i][j]); } memset(DP,0,sizeof(DP)); DP[0][0]=map[0][0]; for(int i=0; i<R; i++) for(int j=0; j<C; j++) { if(j-1>=0&&DP[i][j]<DP[i][j-1]+map[i][j]) DP[i][j]=DP[i][j-1]+map[i][j]; if(i-1>=0&&DP[i][j]<DP[i-1][j]+map[i][j]) DP[i][j]=DP[i-1][j]+map[i][j]; if(i-1>=0&&j-1>=0&&DP[i][j]<DP[i-1][j-1]+map[i][j]) DP[i][j]=DP[i-1][j-1]+map[i][j]; } printf("Scenario #%d:\n",casen); printf("%d\n\n",DP[R-1][C-1]);//两次换行,此题有毒 } return 0;}
- Filthy Rich(动态规划)
- (动态规划)Filthy Rich
- Filthy Rich(动态规划)
- 杭电2391Filthy Rich(动态规划)
- hdoj 2391 Filthy Rich (动态规划)
- HDU 2391 Filthy Rich(动态规划)
- HDOJ 2391 Filthy Rich dp动态规划.....so easy......
- Filthy Rich
- Filthy Rich
- HDU 2391 Filthy Rich (简单DP)
- HDU 2391 Filthy Rich(dp)
- HDU 2391 Filthy Rich(dp)
- hdu 2391 Filthy Rich(DP)
- hdu2391Filthy Rich(动态规划基础)
- hdu 2391 Filthy Rich
- hdu 2391 Filthy Rich
- Filthy Rich+DP
- hdu 2391 Filthy Rich
- JS卡通图片切换
- 修改PHP 上传文件大小限制
- 火狐浏览器使用HttpRequester模拟发送http请求
- Lua游戏开发技能属性定义
- 2016夏季练习——dp
- Filthy Rich(动态规划)
- JS图片滚动代码(无缝、平滑)
- 获取当前城市
- WEB前端开发人员须知的常见浏览器兼容问题及解决技巧
- DBCP配置
- leetcode.181. Employees Earning More Than Their Managers
- HDU 1072 Nightmare
- saltstack学习汇总
- [李景山php]每天laravel-20160910|Filesystem-1