hdu 1078 FatMouse and Cheese
来源:互联网 发布:python猜数字四位数 编辑:程序博客网 时间:2024/05/22 10:29
DP+记忆化搜索 dp[i][j]表示以map[i][j]为起点路径搜索得到的的和的最大值。所以最后的结果自然为dp[0][0]了。#include<iostream>using namespace std;int map[100][100],dp[100][100];int dirx[4]={1,-1,0,0};int diry[4]={0,0,-1,1};int n,k;int max(int x,int y){ if(x>y) return x; return y;}int dfs(int x,int y){ if(dp[x][y]>-1) return dp[x][y]; dp[x][y]=map[x][y]; for(int i=0;i<4;i++) { for(int j=1;j<=k;j++) { int xx=x+j*dirx[i]; int yy=y+j*diry[i]; if(0<=xx&&xx<=n&&0<=yy&&yy<n&&map[xx][yy]>map[x][y]) { dp[x][y]=max(dp[x][y],dfs(xx,yy)+map[x][y]); } } }return dp[x][y];;} int main(){ while(cin>>n>>k) { if(n==-1&&k==-1) break; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { cin>>map[i][j]; dp[i][j]=-1; } dfs(0,0); cout<<dp[0][0]<<endl; } return 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
- hdu 1054 Strategic Game
- activemq高级特性
- hdu 1058 Humble Numbers
- windows&linux 双系统启动问题。
- hdu 1069 Monkey and Banana
- hdu 1078 FatMouse and Cheese
- 【模拟】 UVALive 4168 Lampyridae Teleportae
- 系统场景需求分析思路模板
- [ExtNet]常见问题汇总--[持续更新]
- .net学习计划
- hdu 1081 To The Max
- hdu 1087 Super Jumping! Jumping! Jumping!
- 解析Android消息处理机制:Handler/Thread/Looper & MessageQueue
- java study 02day-关键字与标识符