HDU 1078 FatMouse and Cheese
来源:互联网 发布:it618淘宝客导购源码 编辑:程序博客网 时间:2024/06/06 11:42
分析:题意就是,输入n,k然后输入n阶矩阵,从起点(0,0)开始走,可以往上/下/左/右,4个方向直走,一次最多可以走k步,要求走到的下一个点必须比当前的点大,输出走到的所有点的值之和最大为多少。这题用DFS暴力肯定超时,可以用记忆化搜索,用dp[i][j]表示以点(i,j)为起点,走到的所有点的值之和的最大值,要求dp[i][j],只要知道(i,j)一次能走到的所有点的dp[i'][j']中的最大值max就行了,然后dp[i][j]=map[i][j]+max;
# include <stdio.h># include <string.h> int n,k,map[105][105],dp[105][105]; int dx[4]={-1,0,1,0}; int dy[4]={0,1,0,-1}; int DFS(int x,int y) { int i,j,a,b,t,max=0; if(dp[x][y]>0) return dp[x][y]; for(i=0;i<4;i++) for(j=1;j<=k;j++) { a=x+j*dx[i]; b=y+j*dy[i]; if(a>=0&&a<n&&b>=0&&b<n&&map[a][b]>map[x][y]) { t=DFS(a,b); if(t>max) max=t; } } return dp[x][y]=map[x][y]+max; } int main() { int i,j; while(scanf("%d%d",&n,&k)!=EOF) { if(n==-1&&k==-1) break; for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&map[i][j]); memset(dp,0,sizeof(dp)); printf("%d\n",DFS(0,0)); } }
0 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
- “异或”处理数组的相关算法的面试题
- 欢迎使用CSDN-markdown编辑器
- [leetcode190]Reverse Bits
- hdu 1044 Collect More Jewels(BFS+DFS)
- Java List接口
- HDU 1078 FatMouse and Cheese
- 线程池详解
- 1 - 统计学习方法概论
- 数据库里写了很多存储过程,想用SQL语句查出哪些存储过程包含字段(如:user_id)
- android 最优绘制圆图方法
- 如何脱离tutorial使用igllib
- 形式系统(Formal System)-前言
- Android视频播放(一)
- [leetcode] House Robber