HDU1078(树形DP+DFS)
来源:互联网 发布:其他资本公积.知乎 编辑:程序博客网 时间:2024/05/21 19:38
FatMouse and Cheese
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3332 Accepted Submission(s): 1314
FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse -- after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.
Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.
a line containing two integers between 1 and 100: n and k
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) ... (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), ... (1,n-1), and so on.
The input ends with a pair of -1's.
3 11 2 510 11 612 12 7-1 -1
37
直接DFS即可,刚开始一直错,原来Move数组定义在外面了!dp[][]即存从该处开始最大搜索结果
#include<iostream>
#define MAX 100+10
using namespace std;
int max(int a,int b)
{
return a<b?b:a;
}
int g[MAX][MAX];
int dp[MAX][MAX];
//={{1,0},{-1,0},{0,1},{0,-1}};
int n,k;
int dfs(int x,int y)
{
int move[4][2];int i,j;
if(dp[x][y])
return dp[x][y];
dp[x][y]=g[x][y];
for(j=1;j<=k;j++)
{
move[0][0]=j;move[0][1]=0;
move[1][0]=0-j;move[1][1]=0;
move[2][0]=0;move[2][1]=j;
move[3][0]=0;move[3][1]=0-j;
for(i=0;i<4;i++)
{
int tx=x+move[i][0];
int ty=y+move[i][1];
if(tx>=0&&tx<n&&ty>=0&&ty<n&&g[tx][ty]>g[x][y])
dp[x][y]=max(dp[x][y],dfs(tx,ty)+g[x][y]);
}
}
return dp[x][y];
}
int main()
{
int i,j;
while(cin>>n>>k&&!(-1==n&&-1==k))
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&g[i][j]);
}
memset(dp,0,sizeof(dp));
cout<<dfs(0,0)<<endl;;
}
return 0;
}
- HDU1078(树形DP+DFS)
- HDU1078 dfs+dp
- hdu1078(dp+dfs)
- HDU1078 记忆化DFS+dp
- HDU1078:记忆搜索(dp+dfs)
- HDU1078 FatMouse and Cheese(DFS+DP)
- hdu1078 FatMouse and Cheese 搜索dfs&dp
- hdu1520(树形DP+DFS)
- HDU1011树形DP+DFS
- zoj3201 树形DP+DFS
- HDU1561 树形DFS+DP
- HDU 3848 DFS 树形DP
- hdu2196 computer dfs 树形dp
- hihocoder1104(Dfs预处理+树形DP)
- HDU1078(深搜+DP)
- hdu1078基础dp
- 树形DP <dfs+floyd> 【hlg】 1329 游乐园
- hdu 1561 树形dp+背包+dfs
- WP7固定到开始屏幕
- 从struts-default包透析redirectAction与chain用法
- python读取和自定义配置文件的教程
- 可信计算机的研究与发展
- Jquery表单插件ajaxForm用法详解
- HDU1078(树形DP+DFS)
- 什么是sap 统驭科目(reconciliation account)
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
- python装饰器
- 张焕国
- 沈昌祥
- WIFI、3G、2G网络连接判断深入分析
- VS2010中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法
- Linux下Web服务器应用之基础简介