其他分享
首页 > 其他分享> > Fatmouse and cheese

Fatmouse and cheese

作者:互联网

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.(问题描述;肥鼠在一个城市里储存了一些奶酪。这个城市可以被认为是一个n维的正方形网格:每个网格位置都标有(p,q),其中0 <= p < n,0 <= q < n。在每个网格位置,肥鼠都在一个洞里藏了0到100块奶酪。现在他要享受他最喜欢的食物了。)

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.(最后一个鼠标从位置(0,0)开始。他在原地吃完奶酪,然后水平或垂直地跑向另一个地方。问题是有一只名叫托普·黑仔的超级猫坐在他的洞附近,所以每次他都可以在被托普·黑仔抓住之前跑到最多k个位置进入洞内。更糟糕的是——在一个地方吃完奶酪后,肥鼠变得更胖了。因此,为了获得足够的能量进行下一次跑步,他必须跑到一块奶酪块比当前洞多的地方。)

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.(给定n、k和每个网格位置的奶酪块数量,计算FatMouse在无法移动之前可以吃的最大奶酪量。)

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.(包含1和100之间的两个整数的线:n和k;n行,每一行有n个数字:第一行包含位置(0,0) (0,1)处奶酪块的数量…(0,n-1);下一行包含位置(1,0)、(1,1)处奶酪块的数量,…(1,n-1)等等。输入以一对-1结尾。)

Output
For each test case output in a line the single integer giving the number of blocks of cheese collected.(输出;对于一行中的每个测试用例输出,给出收集的奶酪块数量的单个整数)


#include <iostream>
#include <cstdio>
#include <cstring>
#define N 105
using namespace std;
int n,k;    //定义n,k 表示矩阵的长和宽。
int data[N][N],dp[N][N];    // 定义数据二维数组,
int tab[4][2]={1,0,-1,0,0,1,0,-1};    // 表示胖鼠的四个移动方向。
int DFS(int x,int y)    // 深度优先遍历,定义x,y 表示该点坐标。
{
    int maxnum=0,t,x1,y1;   // 定义下一个所以动到的坐标点x1,y1, maxnum=0为最初始值,t为
    if(dp[x][y]==0)     //  判断该点有没有被探索过,0为没有探索过。
    {
        for(int i=1;i<=k;i++)   // 定义i,i为此不走的步数,最小步数为1,最大步数为k。
        {
            for(int j=0;j<4;j++)  // 胖鼠走的方向。
            {
                x1=x+tab[j][0]*i;
                y1=y+tab[j][1]*i;   //判断下一个移动到的坐标点。

                if(x1>=0&&x1<n&&y1>=0&&y1<n&&data[x1][y1]>data[x][y])   // 判断x1,y1坐标点是否再定义范围内。
                    // 且判断下个要移动的的点的奶酪数有没有上一个坐标的奶酪数多。

                {
                    t=DFS(x1,y1);  //记录下此点。
                    maxnum=max(maxnum,t);    // 取最大值。
                }
            }
        }
        dp[x][y]=maxnum+data[x][y];    // 最后获得奶酪数最大的值为沿途的的奶酪数相加。
    }
    return dp[x][y];  // 返回此点。
}
int main()
{
    int i, j;   // 定义i,为此刻胖鼠所在的坐标 , j为移动的方向。
    while (scanf("%d%d", &n, &k))   // 输入n,k。
    {
        if (n <= 0) break;
        for (i = 0; i < n; i++)   // i在所定义的范围内。
            for (j = 0; j < n; j++)
                scanf("%d", &data[i][j]);   // 判断胖鼠在最初原点的奶酪数是否比四个位置的奶酪数大。
        memset(dp, 0, sizeof(dp));
        printf("%d\n", DFS(0, 0));   // 输出该点。
    }
    return 0;
}

标签:cheese,int,奶酪,Fatmouse,location,each,x1
来源: https://blog.csdn.net/weixin_54501632/article/details/116240698