浏览器标题切换
浏览器标题切换end

ZOJ-1107-FatMouse and Cheese-dfs+记忆化搜索

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 Specification

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 Specification

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

Output for Sample Input

37

题意:给出一个n和k,代表一个n*n的矩阵,k代表老鼠每一次水平或者垂直走的最多步数,-1-1输入结束

没写出来的原因:

  1. 对于记忆化搜索理解不够透彻,这一题不需要对于走过的点进行标记。每次走到之前走过的那个位置,直接返回那个位置所对应的奶酪数,因为那个点所找的奶酪数肯定是之前找过的并且是最多的。
  2. 对于每次可以走1、2、...k步不知道可以在控制方向那边再用一层循环去进行控制。

 

复制代码
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<string.h>
 5 #include<iomanip>
 6 using namespace std;
 7 
 8 int n,k;
 9 int dir[4][2]= {{0,1},{0,-1},{-1,0},{1,0}};
10 int a[110][110];
11 int ss[110][110];//存每一步能够得到的最大奶酪数,之后要是访问过就直接返回
12 
13 int dfs(int x,int y)
14 {
15     if(ss[x][y])
16         return ss[x][y];
17     else
18     {
19         for(int i=0; i<4; i++)
20         {
21             for(int j=1; j<=k; j++)
22             {
23                 int tx=x+dir[i][0]*j;
24                 int ty=y+dir[i][1]*j;
25              //   if(tx>=0&&tx<n&&ty>=0&&ty<n&&ss[x][y]==0)//WA//之前这个点可能是已经记录过步数的,因为要通过两层循环去控制每次的步数和方向,然后去寻找这个点的最大值进行更新
26               if(tx>=0&&tx<n&&ty>=0&&ty<n)
27                 {
28                     if(a[tx][ty]>a[x][y])
29                     {
30                         int maxx=dfs(tx,ty);
31                         if(maxx>ss[x][y])
32                         {
33                             ss[x][y]=maxx;
34                         }
35                     }
36                 }
37             }
38         }
39         ss[x][y]=ss[x][y]+a[x][y];
40         return ss[x][y];
41     }
42 }
43 int main()
44 {
45     std::ios::sync_with_stdio(false);
46     cin.tie(0);
47     cout.tie(0);
48     while(cin>>n>>k)
49     {
50         memset(a,0,sizeof(a));
51         memset(ss,0,sizeof(ss));
52         if(n==-1&&k==-1)
53             break;
54         for(int i=0; i<n; i++)
55         {
56             for(int j=0; j<n; j++)
57             {
58                 cin>>a[i][j];
59             }
60         }
61         cout<<dfs(0,0)<<endl;
62     }
63     return 0;
64 }
复制代码

 

posted @   抓水母的派大星  阅读(155)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示