博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FatMouse and Cheese 动态化搜索
阅读量:6207 次
发布时间:2019-06-21

本文共 2736 字,大约阅读时间需要 9 分钟。

FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7576    Accepted Submission(s): 3133

Problem Description
FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he's going to enjoy his favorite food.
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.
 

 

Input
There are several test cases. Each test case consists of
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.
 

 

Output
For each test case output in a line the single integer giving the number of blocks of cheese collected.
 

 

Sample Input
3 1 1 2 5 10 11 6 12 12 7 -1 -1
 

 

Sample Output
37
 动态化搜索,详见代码。
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 int n,k; 9 const int maxn = 105;10 int maze[maxn][maxn],dp[maxn][maxn];11 int dfs(int p,int q){12 if(dp[p][q]) return dp[p][q];13 int l,r,d,u;14 int ans = 0,maxx = 0;15 l = p - k;16 r = p + k;17 if(l<0) l = 0;18 if(r>=n) r = n-1;19 for(int i = l; i<=r; i++){20 if(maze[i][q]>maze[p][q]){21 ans = dfs(i,q);22 if(ans>maxx) maxx = ans;23 }24 }25 u = q + k;26 d = q - k;27 if(d<0) d = 0;28 if(u>=n) u = n-1;29 for(int i = d; i<=u; i++){30 if(maze[p][i]>maze[p][q]){31 ans = dfs(p,i);32 if(ans>maxx) maxx = ans;33 }34 }35 dp[p][q] = maxx + maze[p][q];36 return dp[p][q];37 }38 void input(){39 while(scanf("%d%d",&n,&k)!=EOF&&n != -1&&k != -1){40 for(int i = 0; i
卷珠帘

 

转载于:https://www.cnblogs.com/littlepear/p/5385164.html

你可能感兴趣的文章
quartz
查看>>
C语言基础学习7:返回指针值的函数
查看>>
fatal error LINK1123:failure during conversion to COFF:file invalid or corrupt
查看>>
IE6/7下Select控件Display属性无效解决办法
查看>>
Django之名称空间
查看>>
<<深入浅出nodeJS>>读书笔记--<二>
查看>>
回收ImageView占用的图像内存
查看>>
Linux Kconfig及Makefile学习
查看>>
java之jvm学习笔记六(实践写自己的安全管理器)
查看>>
【评分】第二次作业-数独-第一次测试成绩
查看>>
基础排序算法,java实现(快速,冒泡,选择,堆排序,插入)
查看>>
struts请求源码的跟踪
查看>>
在jquery的ajax中添加自定义的header信息
查看>>
echarts3.0之关系图详解
查看>>
一步步学Qt,第四天-Qt程序发布问题
查看>>
每天一个小算法(Shell sort5)
查看>>
Tomcat 部署项目的三种方法(转)
查看>>
Python3.x和Python2.x的区别
查看>>
Python列表
查看>>
cenOS-telnet refused问题
查看>>