hdu 1078 FatMouse and Cheese (dp+dfs)
2017-08-18 15:10
453 查看
题意:
老鼠从(0,0)点出发,每次老鼠最多移动k步,且移动过去的下一点的奶酪要比当前点多。
思路:
第一次把dp和dfs相结合,刚开始忘了标记走过的路结果超时了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,k;
int ma[105][105],dp[105][105];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int dfs(int x,int y){
int mx=0,te,xx,yy;
if(!dp[x][y]){
for(int co=1;co<=k;co++){
for(int i=0;i<4;i++){
xx=x+dx[i]*co;
yy=y+dy[i]*co;
if(xx>=0&&yy>=0&&xx<n&&yy<n&&ma[xx][yy]>ma[x][y]){
te=dfs(xx,yy);
mx=max(mx,te);
}
}
}
dp[x][y]=mx+ma[x][y];
}
return dp[x][y];
}
int main(){
while(scanf("%d%d",&n,&k)){
if(n==-1&&k==-1) break;
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&ma[i][j]);
printf("%d\n",dfs(0,0));
}
return 0;
}
老鼠从(0,0)点出发,每次老鼠最多移动k步,且移动过去的下一点的奶酪要比当前点多。
思路:
第一次把dp和dfs相结合,刚开始忘了标记走过的路结果超时了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,k;
int ma[105][105],dp[105][105];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int dfs(int x,int y){
int mx=0,te,xx,yy;
if(!dp[x][y]){
for(int co=1;co<=k;co++){
for(int i=0;i<4;i++){
xx=x+dx[i]*co;
yy=y+dy[i]*co;
if(xx>=0&&yy>=0&&xx<n&&yy<n&&ma[xx][yy]>ma[x][y]){
te=dfs(xx,yy);
mx=max(mx,te);
}
}
}
dp[x][y]=mx+ma[x][y];
}
return dp[x][y];
}
int main(){
while(scanf("%d%d",&n,&k)){
if(n==-1&&k==-1) break;
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&ma[i][j]);
printf("%d\n",dfs(0,0));
}
return 0;
}
相关文章推荐
- hdu 1078(dfs+dp)FatMouse and Cheese
- hdu 1078 FatMouse and Cheese【dfs+dp】
- hdu 1078 FatMouse and Cheese 记忆化dp
- HDU 1078 FatMouse and Cheese(DP)
- HDU 1078 FatMouse and Cheese(记忆化搜索 Or DP)
- HDU 1078 FatMouse and Cheese 记忆化搜索模板 dp
- hdu 1078 FatMouse and Cheese【dp】
- HDU 1078 FatMouse and Cheese (记忆化dp)
- hdu 1078 FatMouse and Cheese dfs 记忆化搜素
- Hdu 1078 - FatMouse and Cheese (基础dp)
- HDU-1078___FatMouse and Cheese —— 解题报告 dp动态数组
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
- !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)
- hdu 1078 FatMouse and Cheese 记忆化dp
- HDU1078 FatMouse and Cheese DP(记忆化搜索)
- hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
- 1078 FatMouse and Cheese DFS+DP
- 【DFS(记忆化)】hdu 1078 FatMouse and Cheese(poj 1088 滑雪)
- hdu_1078_FatMouse and Cheese_神奇的贪心_动态规划_算不上是dp
- hdu-1078 FatMouse and Cheese (and) 滑雪问题(记忆化搜索+简单dp)