记忆化搜索,fatmouseandcheese

树的种子      2022-02-08     566

关键词:

1、从gird[0][0]出发,每次的方向搜索一下,每次步数搜索一下

for(i=0; i<4; i++)
{
    for(j=1; j<=k; j++)
    {
        int tx=x+d[i][0]*j;
        int ty=y+d[i][1]*j;
        if(tx>=0&&tx<n&&ty>=0&&ty<n&&grid[x][y]<grid[tx][ty])
        {
            int temp=memSearch(tx,ty);
            if(max<temp) max=temp;
        }
    }
}

2、temp不断更新四个方向和每一步走多远的最优值

#include <cstdio>
#include <string.h>

using namespace std;

int n;///网格大小
int k;///每次最多移动的步数
int grid[105][105];///奶酪
int cheese[105][105];///记忆化搜索

///方向
int d[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};

///记忆化搜
int memSearch(int x,int y)
{
    int i,j;
    int max=0;
    if(cheese[x][y]>0) return cheese[x][y];
    for(i=0; i<4; i++)
    {
        for(j=1; j<=k; j++)
        {
            int tx=x+d[i][0]*j;
            int ty=y+d[i][1]*j;
            if(tx>=0&&tx<n&&ty>=0&&ty<n&&grid[x][y]<grid[tx][ty])
            {
                int temp=memSearch(tx,ty);
                if(max<temp) max=temp;
            }
        }
    }
    return cheese[x][y]=max+grid[x][y];
}

int main()
{
    while(scanf("%d%d",&n,&k)&&n!=-1&&k!=-1)
    {
        memset(cheese,0,sizeof(cheese));
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
                scanf("%d",&grid[i][j]);
        }
        printf("%d
",memSearch(0,0));
    }
    return 0;
}

 

hdu1078fatmouseandcheese——记忆化搜索

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1078 FatMouseandCheeseTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):11361& 查看详情

hdu1078fatmouseandcheese(记忆化搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078题目大意:题目中的k表示横向或者竖直最多可曾经进的距离,不可以拐弯。老鼠的出发点是(1,1)。对于老鼠从当前点可以到达的点。筛选出从这些点到达当前点所能获得的cheese的最... 查看详情

hdu1078fatmouseandcheese(简单记忆化搜索)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1078题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物一道简单的记忆化搜索题,从起点开始搜索即可没什么问题,可以拿来练练手。&nbs... 查看详情

hdu1078fatmouseandcheese(记忆化搜索)

题意:给定一个n*n的矩阵,问从(0,0)开始走,每次最多水平或者垂直走k个格子,且要保证每次到达的格子要大于前一个,问最大和是多少。析:一个很简单的记忆搜索,dp[i][j],表示到达(i,j)的最大和是多少,我们可以... 查看详情

hdu1078fatmouseandcheese(记忆化搜索)(代码片段)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1078FatMouseandCheeseTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):13356  &n 查看详情

hdu1078fatmouseandcheese简单记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078题目大意:在一个N*N的网格中每个网格都有一些奶酪,小明从(0,0)出发拿奶酪吃。但是小明只能往比当前网格中奶酪量多的网格走,并且小明一次最多可以一下子走k不。问小明能拿... 查看详情

hdu1078fatmouseandcheese记忆化搜索(代码片段)

<题目链接> 题目大意:给你一个n*n的矩阵,每个矩阵上有相应数量的奶酪,老鼠一次最多走K步,且每次只能横着走或者竖着走,并且每一次停留位置上的奶酪数一定要多余它刚才的奶酪数,求这只老鼠所能得到的最多奶... 查看详情

hdu1078fatmouseandcheese(记忆化搜索dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078题目大意:一个n*n的图,每个点都有奶酪,老鼠从(0,0)开始走,每次最多只能走k步就要停下来,停下的这个位置的奶酪数只能比上一个停留的位置大,并获取其奶酪,每次只能水平或... 查看详情

hdu-1078-fatmouseandcheese(记忆化搜索)(代码片段)

链接:https://vjudge.net/problem/HDU-1078#author=0题意:FatMousehasstoredsomecheeseinacity.Thecitycanbeconsideredasasquaregridofdimensionn:eachgridlocationislabelled(p,q)where0<=p<nand0<=q<n.AteachgridlocationFatmousehashidbetween0and100blocksofcheeseinahole.Nowhe‘sgoingtoenjoyhisfav... 查看详情

hdu1078zoj1107fatmouseandcheese记忆化搜索(代码片段)

FatMouseandCheeseTimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):15207AcceptedSubmission(s):6443ProblemDescriptionFatMousehasstoredsomecheeseinacity.Thecitycanbeconsideredasasquaregridofdimensionn:eachgridlocationislabelled(p,q)where0<=p<nand0<=q<... 查看详情

hdu1078fatmouseandcheese

记忆化搜索,$dp$。每一个点走到的最长距离是固定的,也就是只会算一次,那么记忆化一下即可,也可以按值从小到大排序之后进行$dp$。记忆化搜索:#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;intT,n,k;int... 查看详情

hdu-1078fatmouseandcheese(记忆化+dfs)(代码片段)

FatMouseandCheese FatMousehasstoredsomecheeseinacity.Thecitycanbeconsideredasasquaregridofdimensionn:eachgridlocationislabelled(p,q)where0<=p<nand0<=q<n.AteachgridlocationFatmousehash 查看详情

记忆化搜索

记忆化搜素是DP的一种形式记忆化搜素是DP的一种形式 查看详情

搜索分析(dfsbfs递归记忆化搜索)

搜索分析(DFS、BFS、递归、记忆化搜索)1、线性查找在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素。 (1)普通搜索方法,一个循环从0到10搜索,这里略。 (2)递归(从中间向两边)1//递归一定要写成记忆化递归2#include&... 查看详情

记忆化搜索(代码片段)

记忆化搜索什么是记忆化搜索?百度百科:算法上依然是搜索的流程,但是搜索到的一些解用动态规划的那种思想和模式作一些保存。个人理解:就是每求到一个状态就保存下来,下次再遇到这个状态直接调用即可它有什么好处... 查看详情

wenbao与记忆化搜索(代码片段)

记忆化搜索:通俗地讲就是搜索的形式,dp的思想 一些搜索难以完成,dp的动态转移方程又不好写的题,就会用到记忆化搜索,利用dp记录路径(相当于为dfs剪枝)用dfs进行模拟。。啦啦啦啦啦啦,,,,,,,,,好厉害!... 查看详情

coj1686:记忆化搜索

看了N遍才看懂题意。。。题意:给N个区间,每次能向左或向右走区间长度这么多,问能不能每次都在[0,m]这个范围内思路:爆搜是不行的。。这里把状态记录一下能剪枝很多定义:s[pos][now]=-1 查看详情

记忆化搜索游荡的奶牛

[luogu1535]游荡的奶牛题目描述Searchingfortheverybestgrass,thecowsaretravellingaboutthepasturewhichisrepresentedasagridwithNrowsandMcolumns(2<=N<=100;2<=M<=100).KeenobserverFarmerJohnhasrecordedBess 查看详情