码迷,mamicode.com
首页 > 其他好文 > 详细

杭电1078(记忆搜索)

时间:2014-06-03 14:14:28      阅读:466      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   a   

一道记忆搜索题,记忆搜索题就是搜索的形式+DP的思想!

题目:

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
 
思想:
一道记忆搜索题,记忆搜索题就是搜索的形式+DP的思想!
 
代码:
  超时代码:
  
bubuko.com,布布扣
 1 #include<stdio.h>
 2 #include<string.h>
 3 //#include<stdlib.h>
 4 /*#include<iostream>
 5 #include<algorithm>
 6 using namespace std;*/
 7 
 8 int getmax(int x, int y){
 9     return x > y ? x : y;
10 }
11 
12 typedef struct{
13     int x;
14     int y;
15 }List;
16 List list[10001];
17 
18 int dir[4][2]={0, -1, -1, 0, 0, 1, 1, 0};
19 
20 
21 int main(){
22     int n, k, block[101][101], i, j, ii, jj, dp[101][101], max, lastmax, startx, starty, start, num;
23     while(scanf("%d %d", &n, &k) && (n != -1 || k != -1)){
24         max = -1;
25         for(i = 0; i < n; i ++){
26             for(j = 0; j < n; j ++){
27                 scanf("%d", &block[i][j]);
28                 max = getmax(max, block[i][j]);
29             }
30         }
31         memset(dp, -1, sizeof(dp));
32         dp[0][0] = block[0][0];
33         start = dp[0][0];
34         startx = 0;
35         starty = 0;
36         num = 0;
37         lastmax = dp[0][0];
38         do{
39             for(i = 0; i < 4; i ++){
40                 if(block[startx][starty] == max){
41                     break;
42                 }
43                 for(j = 1; j <= k; j ++){
44                     ii = startx + dir[i][0] * j;
45                     jj = starty + dir[i][1] * j;
46                     if(ii < 0 || jj < 0 || ii >= n || jj >= n || block[ii][jj] <= block[startx][starty]){
47                         continue;
48                     }
49                     dp[ii][jj] = getmax(dp[ii][jj], start + block[ii][jj]);
50                     lastmax = getmax(dp[ii][jj], lastmax);
51                     list[num].x = ii;
52                     list[num ++].y = jj;
53                 }
54             }
55             if(num == 0){
56                 num --;
57                 continue;
58             }
59             startx = list[-- num].x;
60             starty = list[num].y;
61             start = dp[startx][starty];
62         }while(num != -1);
63         printf("%d\n", lastmax);
64     }
65     return 0;
66 }
奋进(超时代码)

望大神指点,个人觉得跟下面的正确代码时间复杂度差不多啊!

 

  正确代码:

  

bubuko.com,布布扣
 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int map[105][105];
 5 int value[105][105];
 6 int dir[4][2]={0,1,0,-1,1,0,-1,0};
 7 int n,k;
 8 
 9 int dfs(int sx,int sy)
10 {
11    int i,j,x,y,max=0,temp;
12    if(value[sx][sy]!=-1)       //该点已经计算过,直接返回值
13       return value[sx][sy];
14    for(i=0;i<4;i++)            //2个循环控制该点所能走的地方
15      for(j=1;j<=k;j++)
16      {
17         x=sx+dir[i][0]*j;
18         y=sy+dir[i][1]*j;
19         if(x<0||x>=n||y<0||y>=n||map[x][y]<=map[sx][sy])    //剪枝
20           continue;
21         temp=dfs(x,y);       
22         if(temp>max)        //max保存由该点所能到达之处能吃到的最大奶酪数
23 
24            max=temp;
25      }
26   value[sx][sy]=map[sx][sy]+max;      //得到由该点出发所能吃到的最大奶酪数,保存在value里
27   return value[sx][sy];
28 }
29 
30 int main()
31 {
32   int i,j;
33   while(scanf("%d%d",&n,&k)!=EOF)
34   {
35     if(n==-1&&k==-1)
36        break;
37     memset(value,-1,sizeof(value));    //初始化数组(不能为0)
38     for(i=0;i<n;i++)
39       for(j=0;j<n;j++)
40         scanf("%d",&map[i][j]);
41      printf("%d\n",dfs(0,0));
42   }
43   return 0;
44 }
网上大神代码

总结:

尽量不要用do....while();

杭电1078(记忆搜索),布布扣,bubuko.com

杭电1078(记忆搜索)

标签:c   style   class   blog   code   a   

原文地址:http://www.cnblogs.com/xiaoyeye/p/3759582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!