[洛谷]P2958 [USACO09OCT]木瓜的丛林Papaya Jungle (#搜索

阅读: 评论:0

[洛谷]P2958 [USACO09OCT]木瓜的丛林Papaya Jungle (#搜索

[洛谷]P2958 [USACO09OCT]木瓜的丛林Papaya Jungle (#搜索

题目描述

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


思路

本题的大意就是Bassie如何每次走的都是周围木瓜最多的。

#include <stdio.h>
#include <iostream>
using namespace std;
int a[101][101],n,m,s;
int tox[5]={0,1,0,-1,0},toy[5]={0,0,1,0,-1};
void dfs(int x,int y,int i)
{register int j,x2,y2,ans(0);s=s+a[x][y];a[x][y]=0;if(x==n && y==m){return;}for(j=1;j<=4;j++){int x1=x+tox[j];int y1=y+toy[j];if(x1>=1 && x1<=n && y1>=1 && y1<=m && a[x1][y1]>ans){ans=a[x1][y1];x2=x1;y2=y1;}}dfs(x2,y2,i);
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);int i,j;cin>>n>>m;for(i=1;i<=n;i++){for(j=1;j<=m;j++){cin>>a[i][j];}}dfs(1,1,0);cout<<s<<endl;return 0;
}

 

本文发布于:2024-01-31 11:44:32,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170667267928280.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:木瓜   丛林   洛谷   USACO09OCT   Papaya
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23