FatMouse and Cheese
来源:互联网 发布:网络暴力乔任梁 编辑:程序博客网 时间:2024/06/11 19:54
题意是:
老鼠从(0,0)出发吃奶酪,能水平或竖直方向在map[][]上移动,至多移动k步,并且移动到的下一个位置必须比当前位置的奶酪多,求最多吃到的奶酪是多少。
由于题目要求有四个方向可以走,所以想到搜索,而要求走得最优解。所以可以用记忆化搜索。动态规划基础题,不过相当经典。
题目链接:http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1003&cid=29505
代码:
#include <iostream>#include<cstdio>#include<string.h>using namespace std;int map[101][101];int dp[101][101];int xx[4]= {0,0,-1,1};int yy[4]= {1,-1,0,0};int Max,ans,n,k;/*int dfs(int x,int y){ Max=0; ans=0; for(int i=1; i<=k; i++) { for(int j=0; j<4; j++) { int dx=x+xx[j]*i; int dy=y+yy[j]*i; if(dx<n&&dx>=0&&dy<n&&dy>=0&&map[x][y]<map[dx][dy]) { ans=dfs(dx,dy); if(Max<ans) Max=ans; } } dp[x][y]=Max+map[x][y]; } return dp[x][y];}*/int DFS(int x,int y){ int ans = 0,MAX = 0; for(int i = 1; i <= k; i++) { for(int j = 0; j < 4; j++) { int dx = x+xx[j]*i; int dy = y+yy[j]*i; if(dx>=0 && dx<n && dy>=0 && dy<n && map[x][y]<map[dx][dy]) { ans = DFS(dx,dy);//每条路径能吃到的奶酪量 if(ans > MAX) MAX = ans; } } dp[x][y] = MAX+map[x][y];//从dp[x][y]开始能吃到的最大奶酪量 } return dp[x][y];}int main(){ while(~scanf("%d%d",&n,&k)) { if(n==-1&&k==-1) break; memset(map,0,sizeof(map)); memset(dp,0,sizeof(dp)); for(int i=0; i<n; i++) for(int j=0; j<n; j++) scanf("%d",&map[i][j]); int sum=DFS(0,0); cout<<sum<<endl; } return 0;}
0 0
- hdu1078 FatMouse and Cheese
- FatMouse and Cheese
- hdu1078 FatMouse and Cheese
- FatMouse and Cheese
- 1078FatMouse and Cheese
- HDU1078 FatMouse and Cheese
- FatMouse and Cheese
- zoj1107 fatmouse and cheese
- hdu1078 FatMouse and Cheese
- FatMouse and Cheese
- HDU1078 FatMouse and Cheese
- HDU1078-FatMouse and Cheese
- B - FatMouse and Cheese
- HDU1078-FatMouse and Cheese
- FatMouse and Cheese
- FatMouse and Cheese
- hdu1078 FatMouse and Cheese
- FatMouse and Cheese HDU
- fmt 国际化 日期 数字 本地环境 字符编码 本地资源
- Unity 射线检测碰撞
- vs2013更新了代码,但是运行没效果
- 安卓xml绘图
- Windows下Redis安装和使用
- FatMouse and Cheese
- Hibernate关联关系映射(双向篇)
- 用angularjs遇到的坑
- Python IO multiplex
- collectionView 的选中添加数组和再点击从数组中删除
- 使用子查询。所谓子查询就是嵌套在另外一个查询内部的查询。mysql
- 图像处理-cvCopy与cvCloneImage的区别
- cocos2dx 内存管理
- splinter 学习笔记