• HDU 1078 FatMouse and Cheese


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078

    FatMouse and Cheese

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


    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
     
    题目描述:在一个正方形方格中,有 n*n个格子,每个格子里都有一定量的食物,一个老鼠在里面走,要求下一次吃的食物一定大于上一次吃的食物的量,所以只能去下一步的食物量大于上一步的食物量的那一个格子,而且老鼠一次走的步数不超过K步。求最多能吃多少食物。
    输入 n k,然后输入n×n个数,每个数代表对应位置的食物量,老鼠从(0,0)位置开始吃。
     
    思路:用dfs从起始点不断向下搜索合适的格子,然后计算到那个位置时吃的最大数量,然后用dp数组存取到那个位置时的最大数量;最后输出dp存取的其实点数量即为答案。
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 const int maxn = 110;
     5 int n,k;
     6 int dp[maxn][maxn];  //到达某个位置时,最多吃的量
     7 int maze[maxn][maxn]; //记录图
     8 int go[4][2] = {1,0,-1,0,0,1,0,-1};  //方向数组
     9 //判断是否越界
    10 bool check(int x,int y)
    11 {
    12     if(x<0||y<0||x>=n||y>=n)
    13         return 1;
    14     return 0;
    15 }
    16 
    17 int max(int x,int y)
    18 {
    19     if(x<y)
    20         return y;
    21     return x;
    22 }
    23 
    24 int dfs(int x,int y)
    25 {
    26     int i,j,ans = 0;
    27     int xx,yy;
    28     if(!dp[x][y])  //还没到xy这个位置
    29     {
    30         for (i = 1; i <= k; i++)  //能走的步数
    31         {
    32             for (j = 0; j < 4; j++)
    33             {
    34                 xx = x + go[j][0]*i;
    35                 yy = y + go[j][1]*i;
    36                 if (check(xx, yy))
    37                     continue;
    38                 if(maze[xx][yy] > maze[x][y])
    39                     ans = max(ans, dfs(xx, yy)); //一直递归调用dfs寻找每一步的最大值,人后一层层返回,最终dp[1][1]就是能吃的最多数量
    40             }
    41         }
    42         dp[x][y] = ans+maze[x][y]; //记录在xy位置 为止 能吃的最大数量和
    43     }
    44     return dp[x][y];
    45 }
    46 
    47 int main()
    48 {
    49     while(scanf("%d%d",&n,&k)!=EOF&&(n!=-1||k!=-1))
    50     {
    51         memset(dp,0,sizeof(dp));
    52         memset(maze,0,sizeof(maze));
    53         for(int i=0;i<n;i++)
    54             for(int j=0;j<n;j++)
    55                 scanf("%d",&maze[i][j]);
    56         int ans = dfs(0,0);
    57         printf("%d
    ",ans);
    58     }
    59     return 0;
    60 }
    View Code
     

  • 相关阅读:
    eclipse中统计代码行数
    vmware ubuntu 最近遇到的一些问题
    Shows how to create and run composites dynamically
    程序员的艺术:排序算法舞蹈
    Apache Mina 网络通信
    OutOfMemoryError汇总
    c++/java/c# 几种编程语言的指针、引用比较
    Silverlight开发工具集合
    web.cong 详解
    ASP.NET中的web.config配置
  • 原文地址:https://www.cnblogs.com/cypblogs/p/10059674.html
Copyright © 2020-2023  润新知