hdu 1078 FatMouse and Cheese

记忆化搜索

题目大意:老鼠出来后停在哪个格子就将那个格子里面的食物吃掉。每次出来都只能吃一方格的食物,

每次出来最多跳过k个有食物的格子去吃另外那个格子里面的食物。

思路:本题用记忆化搜索。其实就是记录在一个点上 在之前搜索过程中的最大值,如果后来搜到了这没有大于这个值就停止搜索, 如果大于这更新这个最大值并继续搜索。

题目源码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 110;
int map[maxn][maxn],mark[maxn][maxn];
int up[] = {0,0,1,-1};
int ri[] = {1,-1,0,0};
int n,k;
int dfs(int x,int y)
{
    int Max = 0;
    if( !mark[x][y])
    {
        for( int i = 1; i <= k; i++)
            for( int j = 0; j < 4; j++)
        {
            int xx = x + up[j]*i;
            int yy = y + ri[j]*i;

            if( xx >= 0 && xx < n && yy >= 0 && yy < n && map[xx][yy] > map[x][y])
            {
                int ans = dfs(xx,yy);
                Max = Max < ans ? ans : Max;
            }
        }
        mark[x][y] = Max + map[x][y];
    }
    return mark[x][y];
}
int main()
{
    while( scanf("%d%d",&n,&k) != EOF)
    {
        if( n == -1 && k == -1)break;
        for( int i = 0; i < n; i++)
            for( int j = 0; j < n; j ++)
            scanf("%d",&map[i][j]);
        memset(mark,0,sizeof(mark));
        printf("%d
",dfs(0,0));
    }
    return 0;
}


 

原文地址:https://www.cnblogs.com/LUO257316/p/3220801.html