HDU 1078

FatMouse and Cheese

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


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  //记忆化深搜+dp 

 2  #include <iostream>

 3  #include <stdio.h>

 4  #include <string.h>

 5  using namespace std;

 6  

 7  int map[110][110];

 8  int dp[110][110];

 9  int n,m,k;

10  

11  int dfs(int p,int q)

12  {

13      int i,j;

14      if(dp[p][q]>0)

15           return dp[p][q];

16      else

17      {

18          int ans,maxn=0;

19          int left,right,down,up;

20          left=p-k;       

21           //每一个操作不能超过K步

22          right=p+k;

23          if(left<0) left=0;        

24          //防止越界

25          if(right>=n) right=n-1;

26          for(i=left;i<=right;i++)

27          {

28              if(map[i][q]>map[p][q])       

29              //条件要求下一步必须比前一步的值大

30              {

31                  ans=dfs(i,q);        

32                  if(maxn<ans)

33                      maxn=ans;

34              }

35          }

36          up=q-k;

37          down=q+k;

38          if(up<0) up=0;

39          if(down>=n) down=n-1;

40          for(i=up;i<=down;i++)

41          {

42              if(map[p][i]>map[p][q])

43              {

44                  ans=dfs(p,i);

45                  if(maxn<ans)

46                      maxn=ans;

47              }

48          }

49          dp[p][q]=maxn+map[p][q];        

50          //状态转移方程 DP[p][q]=max(dp[i][q],dp[p][j])+map[p][q]  其中i就是左右,j是上下

51      }

52      return dp[p][q];//必须加 

53  }

54  

55  int main()

56  {

57      int i,j;

58      while(cin>>n>>k,~n||~k)

59      {

60          //scanf("%d%d",&n,&k);

61          //if(n==-1 && k==-1) break;

62          for(i=0;i<n;i++)

63          {

64              for(j=0;j<n;j++)

65                  cin>>map[i][j];

66          }

67          memset(dp,0,sizeof(dp));

68          //printf("%d\n",dfs(0,0));

69          cout<<dfs(0,0)<<endl;

70      }

71      return 0;

72  }
 1 //看来记忆化深搜和bfs我分不清啊 ,关键是我想起了骑士周游问题 

 2 #include<iostream>

 3 using namespace std;

 4 

 5 int a[101][101];

 6 int dp[101][101];

 7 int dir[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};

 8 int n,k;

 9 

10 int dfs(int i,int j)//深搜方位

11 {

12     int p,q,s,t,temp,m = 0;

13     if(dp[i][j] > 0)

14         return dp[i][j];

15     else

16     {

17          for(p = 0;p < 4;p++)//搜四个方向

18              for(q = 1;q <= k;q++)//一次可以跳1-k步

19              {

20                  s = i + dir[p][0]*q;

21                  t = j + dir[p][1]*q;

22                  if(s>=1 && s<=n && t>=1 && t<= n && a[i][j] < a[s][t])

23                  {

24                      temp = dfs(s,t);

25                      if(m < temp)//从周围找到最大的数

26                      {

27                          m = temp;

28                      }

29                  }

30              } 

31          dp[i][j] = m + a[i][j];//本身也要加上

32      }

33     return dp[i][j];

34 }

35 

36 int main()

37 {

38     while(cin>>n>>k)

39     {

40         if(n == -1 && k == -1)

41             break;

42         int i,j;

43         for(i = 1;i <= n;i++)

44             for(j = 1; j <= n;j++)

45             {

46                 scanf("%d",&a[i][j]);

47                 dp[i][j] = 0;

48             }

49         dfs(1,1);

50         cout<<dp[1][1]<<endl;

51     }

52     return 0;

53 }

 

你可能感兴趣的:(HDU)