题目描述

Bessie has wandered off the farm into the adjoining farmer's land. He raises delicious papaya fruit, which is a delicacy for cows. The papaya jungle is partitioned into a grid of squares with R rows and C columns (1 <= R <= 40, 1 <= C <= 40), as is popular in Wisconsin. Bessie can travel from a given square to any existing adjacent square whose route is parallel to the X or Y axis. So in the

following diagram, if Bessie is at the square labeled 'B', she can travel to any of the squares labeled 'T':

.T. TBT .T. Bessie always starts out by munching the papayas in square

(row=1,col=1). After she's done with one square, Bessie always uses her trusty binoculars to count the low-hanging fruit in each of the adjacent squares. She then always moves to the square with the most visible uneaten fruit (a square that happily is always unique).

Sooner or later, following this rule, Bessie always ends up in square (R,C) and eats the fruit there.

Given the dimensions of the papaya jungle and the amount of fruit F_ij in each square (1 <= F_ij <= 100), determine the total number of fruit Bessie consumes for a given papaya jungle.

POINTS: 80

Bessie不小心游荡出Farmer John的田地,而走进了相邻的农民的地里。她举起一个木瓜,木瓜对奶牛来说可是不可多得得美味。这个木瓜林像一般的威斯康星州的田地一样被分割成一个R行C列的网格(1 <= R <= 40, 1 <= C <= 40)。Bessie可以从一个格沿着一条跟X轴或Y轴平行的直线走到邻接的另一个格。Bessie发现一开始她自己在木瓜林的(1,1),也就是第一行第一列慢悠悠地咀嚼着木瓜。

Bessie总是用她最信赖地双筒望远镜去数每一个邻接的格里挂着的木瓜的数目。然后她就游荡到那个有最多没有被吃掉的木瓜的邻接的格子(保证这样的格子只有一个)。

按照这种移动方法,最终Bessie总是会在(R,C)停止然后吃掉那里的木瓜。

给定这个木瓜林的大小及每个格的木瓜数F_ij(1 <= F_ij <= 100), 要求Bessie一共吃了多少个木瓜。

输入输出格式

输入格式:

* Line 1: Two space-separated integers: R and C

* Lines 2..R+1: Line i+1 describes row i of the jungle with C

space-separated integers that tell how many fruit are in each square: F_i1, F_i2, ..., F_iC

输出格式:

* Line 1: A single integer that is the total number of papayas that Bessie eats by the time she finishes off the papayas at the barn in the lower right corner at coordinate (R,C).

输入输出样例

输入样例#1: 复制

3 4
3 3 4 5
4 5 3 2
1 7 4 2

输出样例#1: 复制

39

说明

Three rows; four columns. Bessie starts in upper left corner at the '3'.

Bessie eats the papayas in the order given by the letters next to the numbers below:

(1,1) ---> (1,C)

(1,1) 3a 3 4g 5h (1,C)

| 4b 5c 3f 2i |

(R,1) 1 7d 4e 2j (R,C)

(R,1) ---> (R,C)

She declines to eat 4 of the papayas but consumes 39 (visiting all but two squares of the grid).

题解:十分淳朴的dfs题目!好久之前在叶老师那里做过呢。

// luogu-judger-enable-o2
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
typedef long long ll;
using namespace std;
int r,c,a[41][41],jjj;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
void dfs(int x,int y){jjj+=a[x][y]; a[x][y]=0;if(x==r && y==c) return;int mx,my,ans=0;for(int i=0;i<4;i++){int tx=x+dx[i]; int ty=y+dy[i];if(tx>=1 && tx<=r && ty>=1 && ty<=c && a[tx][ty]>ans ){ans=a[tx][ty];mx=tx,my=ty;}   }dfs(mx,my);
}
int main(){scanf("%d %d",&r,&c);for(int i=1;i<=r;i++)for(int j=1;j<=c;j++)scanf("%d",&a[i][j]);dfs(1,1);printf("%d",jjj);return 0;
}

转载于:https://www.cnblogs.com/wuhu-JJJ/p/11182376.html

【洛谷 2958】木瓜的丛林相关推荐

  1. 洛谷 P2958 [USACO09OCT]木瓜的丛林Papaya Jungle

    P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer ...

  2. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  3. 洛谷 P1142 轰炸

    洛谷 P1142 轰炸 题目描述 "我该怎么办?"飞行员klux向你求助. 事实上,klux面对的是一个很简单的问题,但是他实在太菜了. klux要想轰炸某个区域内的一些地方,它们 ...

  4. 洛谷 P1387 最大正方形

    P1387 最大正方形 题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=10 ...

  5. 洛谷P2763 试题库问题

    题目:https://www.luogu.org/problemnew/show/P2763 题目描述 «问题描述: 假设一个试题库中有n道试题.每道试题都标明了所属类别.同一道题可能有多个类别属性. ...

  6. 动态规划——洛谷_P1057传球游戏

    题目: 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏.游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球, ...

  7. 洛谷P1417 烹调方案

    洛谷P1417 烹调方案 如果是一般的01背包的话 选的先后是没关系的 但是这题选的先后是有关系的,因为他的价值是随着时间而变化的, 而你的01背包是做不到先选2再选1的 那么我们就跟国王游戏一样 用 ...

  8. 记忆优化搜索(简单题)(洛谷P3183 [HAOI2016]食物链 )( P5635 【CSGRound1】天下第一 )

    昨天做了蓝桥杯的时候,发现自己对于记忆优化搜索甚是不熟悉,所以今天随便找了几个基础题做做,顺便写下两片题解,顺便用了一下devc++敲的代码,发现没有代码补全真的可以说是灰常难受了... 洛谷P318 ...

  9. 洛谷 - 试炼场(全部题目备份)

    整理的算法模板合集: ACM模板 目录 1.新手村 1 - 1 洛谷的第一个任务 1 - 2 顺序与分支 1 - 3 循环!循环!循环! 1 - 4 数组 1 - 5 简单字符串 1 - 6 过程函数 ...

最新文章

  1. Go 知识点(10) — 子协程能否使用主协程变量
  2. 马斯克来了!瞄准1万亿美元电信市场,星链「村村通」在美获突破
  3. AC日记——Power收集 洛谷 P3800
  4. express 源码阅读(全)
  5. 再见xx网盘!4 行命令搭建属于你的私人网盘!
  6. 20150204--JS巩固与加强2-01
  7. Java代码生成器原理和编写
  8. 新概念英语第三册 阅读
  9. 计算机win7怎么更改前后面板,解决机箱前置面板没声音(xp/win7图文教程)
  10. MATLAB必看书籍推荐
  11. 【JS】446- 你不知道的 map
  12. 《我为什么熬夜?》系列之 倚天屠龙记
  13. 常用正则表达式,常用表单验证javascript代码(转)
  14. c语言输入密码并将密码掩盖住
  15. 如何扩大你的心理舒适区?
  16. MATLAB小知识(三)——输出矩阵到TXT
  17. 6款程序员常用代码对比工具,你用过几款?
  18. 线性代数让我想想:三阶行列式计算优化策略
  19. ccpd文件名转成xml_在Deepin V20/Ubuntu 20.04下安装佳能LBP2900+打印机的方法
  20. [python]判断麻将和牌算法

热门文章

  1. 华为交换机主备命令_华为交换机命令中文意思
  2. maya cmds 笔记_1
  3. win10 安装硕正
  4. 安卓APP自动更新实现
  5. 云上全流程透明性备品备件协同管理
  6. MongoDB数据库开发环境搭建与配置,Windows环境下
  7. basename函数漏洞之[Zer0pts2020]Can you guess it?
  8. OGG FOR BIGDATA 安装(修正)
  9. 微信小程序调试webview_关于微信小程序webview的使用
  10. 关于蓝桥杯大赛,你应该了解的那些事!