HDU 1078 FatMouse and Cheese(递推)
来源:互联网 发布:php英文外贸企业源码 编辑:程序博客网 时间:2024/06/10 16:08
题目:点击打开链接
题意:老鼠能从上下左右四个方向走1 至 k长度吃到奶酪,但只能往多了吃不能吃了多的再吃少的,问所能吃到奶酪最大值
解法:结构体排序+dp,条件中应有if (dp[i][j]) 表示该点走过 ,没走过不能往下执行
代码:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;struct sa{ int x,y,z;}data[10010];int cmp(sa a,sa b){ return a.z<b.z;}int main(){ int n,k,b,a[105][105],dp[105][105],ans,xx,yy; while(~scanf("%d%d",&n,&k)) { if(n==-1&&k==-1) break; b=0; memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { scanf("%d",&a[i][j]); b++; data[b].x=i; data[b].y=j; data[b].z=a[i][j]; } sort(data+1,data+1+n*n,cmp); dp[1][1]=a[1][1]; for(int i=1;i<=n*n;i++) { xx=data[i].x; yy=data[i].y; for(int j=-k;j<=k;j++) { if(j==0) continue; if(xx+j>=1&&xx+j<=n&&a[xx][yy]>a[xx+j][yy]&&dp[xx+j][yy]) dp[xx][yy]=max(dp[xx][yy],dp[xx+j][yy]+a[xx][yy]); if(yy+j>=1&&yy+j<=n&&a[xx][yy]>a[xx][yy+j]&&dp[xx][yy+j]) dp[xx][yy]=max(dp[xx][yy],dp[xx][yy+j]+a[xx][yy]); } } ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) ans=max(ans,dp[i][j]); printf("%d\n",ans); } return 0;}
1 0
- HDU 1078 FatMouse and Cheese 递推
- HDU 1078 FatMouse and Cheese(递推)
- HDU 1078 FatMouse and Cheese
- hdu 1078 FatMouse and Cheese
- hdu 1078 FatMouse and Cheese
- hdu 1078 FatMouse and Cheese
- hdu 1078 FatMouse and Cheese
- hdu-1078-FatMouse and Cheese
- HDU 1078 FatMouse and Cheese
- HDU 1078 FatMouse and Cheese
- Hdu 1078 FatMouse and Cheese
- HDU 1078 FatMouse and Cheese
- HDU 1078 FatMouse and Cheese
- hdu 1078 FatMouse and Cheese
- HDU 1078 FatMouse and Cheese
- HDU 1078 FatMouse and Cheese
- hdu 1078 FatMouse and Cheese
- HDU 1078 FatMouse and Cheese
- linux下编译Android内核,系统,镜像问题=
- POJ dp题目列表
- 算法总结(7)--leetcode上的递归,BFS,DFS思考
- IT-http-读书笔记--《http权威指南》
- eclipse(indigo)安装maven插件失败
- HDU 1078 FatMouse and Cheese(递推)
- 【bzoj 1854】[Scoi2010]游戏 二分图匹配
- 教你搭建你自己的Git服务器
- android studio 小技巧
- cunit软件应用介绍
- 堆排序原理及其实现(C++)
- LeetCode----count-and-say
- Matlab中两个矩阵相乘报错:MTIMES is not fully supported for integer classes. At least one input must be scalar
- ECharts