hdu 1078 FatMouse and Cheese(记忆化搜索)

hdu 1078 fatmouse and cheese 记忆化搜索 FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2394 Accepted Submission(s): 913 Problem Description FatMouse has stor 阅读详情

FatMouse and Cheese

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


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


 

Source

 

思路1:用记忆化搜索,dp[i][j]=max(dp[x][y]+map[i][j],dp[i][j]);任意i,j走k步且符合map[i][j]<map[x][y];

           初始标记mark[0][0]=1;递归搜索i,j若i,j已标记就按上式,否则就继续递归。

思路2:先将所有的数从小到大排序,然后找到原先0,0点所在的数开始更新。往上下左右走k步,且符合条件的就加上。这样一边就搞定了。

3 2

7 2 5

10 11 6

12 12 8

那么拍完序后,搜的顺序为, 7 8 10 11 12 12

2 5 6不用搜,因为永远不符合条件。

思路1:代码过了。

思路2:代码没过,不过感觉思路没问题,代码不知道哪出问题,求赐教。

//思路1:代码过了
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int mm=1110;
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
int map[mm][mm];
bool mark[mm][mm];
int dp[mm][mm],ans;
bool vis[mm][mm];
int n,k;
void DP(int x,int y)
{ int xx,yy;
  mark[x][y]=1;vis[x][y]=1;
  for(int i=0;i<4;i++)
  for(int j=1;j<=k;j++)
  {
    xx=x+dx[i]*j;yy=y+dy[i]*j;
    if(xx>=0&&xx<n&&yy>=0&&yy<n&&!vis[xx][yy]&&map[xx][yy]>map[x][y])
    {
      if(mark[xx][yy])
      {
        dp[x][y]=max(dp[x][y],dp[xx][yy]+map[x][y]);
      }
      else
      {
        DP(xx,yy);
        dp[x][y]=max(dp[x][y],dp[xx][yy]+map[x][y]);
      }
    }
  }
  if(!dp[x][y])dp[x][y]=map[x][y];
  vis[x][y]=0;
}
int main()
{
  while(cin>>n>>k)
  { if(n==-1&&k==-1)break;
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
    cin>>map[i][j];
    memset(vis,0,sizeof(vis));
    memset(mark,0,sizeof(mark));
    memset(dp,0,sizeof(dp));
    mark[0][0]=1;
    DP(0,0);
    cout<<dp[0][0]<<"\n";
  }
}


 

//思路2:代码没过,感觉思路没问题,求赐教

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int mm=1002;
int map[mm][mm];
int cost[mm][mm];
bool vis[mm][mm];
class node
{ public:
  int x;
  int val;
}s[mm*mm];
int n,k,pos;
bool cmp(node a,node b)
{
  return a.val<b.val;
}
int main()
{
  while(cin>>n>>k)
  {
    if(n==-1&&k==-1)break;
    pos=0;
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
    {cin>>map[i][j];
     cost[i][j]=map[i][j];
     s[pos].val=map[i][j];
     s[pos++].x=i*n+j;
    }
    sort(s,s+pos,cmp);//排序
   // memset(cost,0,sizeof(cost));
   memset(vis,0,sizeof(vis));
    int beg,z;
    for(int i=0;i<pos;i++)//找0,0 点并标记不符合条件的点
    if(s[i].x==0){beg=i;break;}
    else {z=s[i].x;vis[z/n][z%n]=1;}
    int ans=0,x,y,xx,yy;
    int a,b;
    for(int i=beg;i<pos;i++)//从小到大搜一遍
    {
      x=s[i].x/n;y=s[i].x%n;
      a=x-k;b=x+k;//上下左右搜k个点
      if(a<0)a=0;if(b>=n)b=n-1;
      for(int j=a;j<=b;j++)
      { if(j==x||vis[j][y])continue;
        if(map[j][y]<map[x][y]&&cost[j][y]+map[x][y]>cost[x][y])
        cost[x][y]=cost[j][y]+map[x][y];
      }
      a=y-k;b=y+k;
      if(a<0)a=0;if(b>=n)b=n-1;
      for(int j=a;j<=b;j++)
      { if(j==y||vis[x][j])continue;
        if(map[x][j]<map[x][y]&&cost[x][j]+map[x][y]>cost[x][y])
        cost[x][y]=cost[x][j]+map[x][y];
      }
      if(cost[x][y]>ans)ans=cost[x][y];//记录最大值
    }
    cout<<ans<<"\n";
  }
}


 

 

永磁同步电机ADRC控制策略优化:从理论到Simulink仿真实践 本文深入探讨了永磁同步电机自抗扰控制(ADRC)策略的优化与实践。针对传统PID在动态响应、抗扰及噪声抑制方面的不足,详细解析了ADRC三大核心组件——跟踪微分器(TD)、扩张状态观测器(ESO)与非线性状态误差反馈(NLSEF)的工作原理。通过手把手的Simulink仿真教程,展示了从算法编写、模型搭建到参数整定的完整流程,并进阶实现了位置-速度双环ADRC协同控制,为高性能电机伺服系统设计提供了从理论到实践的清晰指南。 阅读详情

相关推荐

简单易懂的AXI_Lite 总线详解

简单易懂的AXI_Lite 总线详解 1、前言 AXI_LITE协议主要应用于Xilinx的ZYNQ芯片构架下的ARM和FPGA之间的数据读写,更偏向于单个寄存器的读写。 2、AXI总线与ZYNQ的关系 AXI(Advanced eXtensible Interface)本是由ARM公司提出的一种总线协议,Xilinx从6系列的FPGA开始对AXI总线提供支持,此时AXI已经发展到了AXI4这个版本,所以当你用到Xilinx的软件的时候看到的都是“AIX4”的IP,如Vivado打包一个AXI IP的时候,

m0_46642108的博客 8493

动态规划入门——FatMouse and Cheese

转载请注明出处:http://blog.youkuaiyun.com/a1dark 分析:DFS+DP、搜索下一个状态所有可能的值、取其中最大的、从后往前递推、 #include #include #include using namespace std; #define N 101 #define INF 0xfffffff int mpt[N][N]; int dp[N][N]; int n,k;

2726

cartographer运行错误集合

播放bag的时候错误 Check failed: sensor_to_tracking->translation().norm() < 1e-5 The IMU frame must be colocated with the tracking frame. Transforming linear acceleration into the tracking frame will otherwise be imprecise tracking frame不是imu的link 修改lua文件中的.

Studying…… 2020

HDU 1078 FatMouse and Cheese(动态规划)

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

风的记忆 355

RK3568平台开发系列讲解:Linux wifi的调试

查询结果中state为ENABLE,表示hostapd程序启用成功。首先查看设备是否存在。

内核笔记 165

DOTS 性能基石:为什么传统 Unity 代码难以满足现代游戏需求?(DOTS 系列教程 · 第1篇)

本文探讨了Unity游戏开发中的性能优化问题,指出性能问题不应在最后才考虑,而应贯穿整个开发过程。文章分析了性能问题的三大表现(渲染开销、物理模拟、游戏逻辑),并解释了依赖硬件升级的不可行性。重点剖析了传统Unity代码性能低下的六大根源:垃圾回收卡顿、编译器优化不足、多核利用不足、数据结构和代码缓存不友好、过度抽象等问题。最后指出Unity项目中常见的反模式,如主线程垄断、内存碎片化等,为后续学习DOTS高性能架构解决方案奠定基础。

分享知识,拥抱变化 151

hdu(1078)——FatMouse and Cheese(递推型动归)

题意: 现在有n*n的方块,然后每个方块中都有一个值,代表这个方块中含有奶酪的数量。 现在那个老鼠站在(0,0)点,并且它每次最多走k步,然后每次走到一个点,它都能获得那个点上含有的值,但是要保证它下次走到的点含有的奶酪数必须多于它现在在的那个位置上的奶酪数量。 思路: 这道题也是一道递推性动归,和poj 1088 滑雪那题的区别就是这题最多可以走k步。 这道题让我更加深刻的理解了递推性

ACM_hades的梦想之路 780

新手必看!手把手教你如何在飞牛NAS上玩转Docker容器!(飞牛Nas(fnOS)如何创建Docke容器以及Compose部署项目)

新手必看!手把手教你如何在飞牛NAS上玩转Docker容器!(飞牛Nas(fnOS)如何创建Docke容器以及Compose部署项目)

XiaoqiangClub的博客 1万+

hdu 1078 FatMouse and Cheese 记忆化搜索/动态规划

题意:n*n的矩阵,一只老鼠从(0,0)点出发,每次只能水平或竖直行走不超过k步,且停留位置奶酪的值必须大于前一位置,求老鼠最多能吃多少奶酪。 这题可以用记忆化搜索做,很容易。也可以根据奶酪多少排序,然后用动态规划。 记忆化搜索代码: #include #include #include #include #include #define N 110 using namespace std

弱菜zc 887

手把手教你学Simulink--双足机器人的反步控制方法仿真

本文介绍了在Simulink中基于反步控制方法进行双足机器人仿真研究的步骤。主要内容包括:1)准备工作,包括MATLAB/Simulink安装和相关工具箱配置;2)构建简化双足机器人模型;3)设计反步控制器,阐述其递归设计原理及Lyapunov函数构造方法;4)通过MATLAB Function块实现控制逻辑并连接模型;5)仿真参数配置和结果分析。文章强调反步控制适用于非线性系统,通过逐步构造虚拟控制律保证系统稳定性,并指出需要多次调试和查阅文献来优化控制效果。该教程为研究双足机器人运动控制提供了实用的仿真

MHD0815的博客 151

告别调参难!YOLO超参数优化指南(网格搜索+贝叶斯优化实战)

YOLO超参数优化的本质,不是“试遍所有参数”,而是“用工具化方法,在场景约束内找到最优平衡”——网格搜索稳、贝叶斯优化快,两者结合(先粗搜再精调)是工业场景的最优解。先定范围:基于工业场景(小目标/边缘设备/多类别)缩小参数搜索范围,避免无效探索。先调核心:优先优化数据增强、学习率、输入尺寸,这些参数对精度影响最大。工具赋能:用网格搜索+贝叶斯优化替代人工试错,再用TensorBoard监控,让调参“有据可依”。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 698

hdu1078 FatMouse and Cheese —— 记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 代码1: #include<stdio.h>//hdu 1078 记忆化搜索 #include<string.h> #define MAX(a,b) (a>b?a:b) int n,k,dp[105][105],...

alince20008的博客 152

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

HDU1078 ZOJ1107 FatMouse and Cheese【记忆化搜索】

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15207 Accepted Submission(s): 6443 Problem Description FatMouse has stor...

海岛Blog 717

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

HDU1078 FatMouse and Cheese 记忆化搜索

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16459 Accepted Submission(s): 7010 Problem Description FatMouse has stor...

DouglasConnor的博客 312

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

HDU 1078 FatMouse and Cheese (记忆化搜索)

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7730    Accepted Submission(s): 3202 Problem Description FatMou

AC_Dreameng 1452

hdu1078 FatMouse and Cheese 记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意:     给一个N*N的矩阵。矩阵的每个坐标上有一个权值,老鼠在该矩阵上跑,求老鼠跑过的位置的权值总和的最大值。 要求:老鼠每次从一个坐标(x1,y1)跑到下一坐标(x2,y2),map[x2][y2]>map[x1][y1](map为权值)。老鼠只能向上,向左,向下,向右跑,且每次

u013699019的博客 312

HDU 1078 FatMouse and Cheese(记忆化搜索)

题目链接:点击打开链接 题意:n * n的地图,每个位置有若干奶酪,FatMouse初始位置在原点,每次可以跑向上下左右四个方向,距离原位置不超过k,而且下一个位置要比之前的位置的奶酪要多,到达一个位置就吃光该位置的奶酪,该过程直到FatMouse不能再移动为止,问FatMouse最多能吃多少奶酪? 思路:记忆化搜索,dp[i][j]表示在(i,j)位置出发,最多能吃的奶酪数;由于FatMou

不忘初心 461

HDU1078 FatMouse and Cheese(记忆化搜索)

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...

邵光亮的博客 768

hdu1078 FatMouse and Cheese(记忆化搜索)

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2710Accepted Submission(s): 1041 Problem Description FatMouse ha...

angtongyou1893的博客 401

hdu 1078 FatMouse and Cheese 【记忆化搜索+dfs】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 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...

甜晨流年 289

HDU - 1078 FatMouse and Cheese (记忆化搜索)

HDU - 1078 FatMouse and Cheese 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 <

HHH_go_的博客 592

HDU - 1078 FatMouse and Cheese(记忆化搜索DP)

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

00后的博客 267

hdu 1078 FatMouse and Cheese (记忆化搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=1078 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:

大太阳 423

HDU - 1078 FatMouse and Cheese dp 记忆化搜索

题目链接:https://cn.vjudge.net/problem/HDU-1078 题意:n*n的图,从(1,1)出发,可以朝一个方向走1到k步,下一个位置的数要比当前位置的数大,求走到的所有位置的数的和最大是多少 题解:记忆化搜索,dp[x][y] 表示从x,y这个位置出发能得到最大的和是多少 #include<bits/stdc++.h> using namespace...

还我笑颜 216
上一篇: hdu 1212 Big Number(数论)
下一篇: hdu 1080 Human Gene Functions(dp)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值