HDU1078FatMouse and Cheese

来源:互联网 发布:客户无忧软件下载 编辑:程序博客网 时间:2024/06/04 23:39

FatMouse and Cheese

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7974 Accepted Submission(s): 3328


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 11 2 510 11 612 12 7-1 -1

Sample Output
37
题目要求找出老鼠最大的逃跑距离,每次逃跑都记忆当前点,然后搜索在该点获得的能量足够去的地方,找出里面能量最大的点到达它,然后继续走,直到被吃。
每次获得能量为a= x+dirx[[j]*i,判断a,b是否被吃 ;同时老鼠到达一个点会长胖,如果他到达的点以及下面所有的点能量小于他当前体重,依然输出0;

#include <iostream>#include <cstdio>#include <cstdio>#include <cstring>#define max(a,b) (a>b)?a:b#define M 1001using namespace std;int k,n;int map[M][M];int sum[M][M];int dirx[4]={1,-1,0,0};//搜索方向int diry[4]={0,0,1,-1};int bound(int i,int j){    if(i>=n||i<0||j>=n||j<0)return 0;//边界    else return 1;}int dfs(int x,int y){    if(sum[x][y]) return sum[x][y];//记忆化    int i,j;    int num=0;    for(i=0;i<=k;i++)    {        for(j=0;j<=3;j++)        {            int a=x+dirx[j]*i;            int b=y+diry[j]*i;            if(bound(a,b)&&map[x][y]<map[a][b])//从(0,0)开始寻找一条最长路,从(x1,y1) ->(x2,y2)             //要满足 x1==x2&& |y1-y2|<=k  或者 y1==y2&&|x1-x2|<=k                num=max(dfs(a,b),num);        }    }    return sum[x][y]=map[x][y]+num;}int main(){    while(scanf("%d%d",&n,&k)!=EOF&&n!=-1)    {        int i,j;        for(i=0;i<n;i++)            for(j=0;j<n;j++)        {            scanf("%d",&map[i][j]);        }        memset(sum,0,sizeof(sum));        printf("%d",dfs(0,0));    }    return 0;}


0 0
原创粉丝点击