Binbin's treasure map

链接:https://ac.nowcoder.com/acm/contest/3674/C
来源:牛客网

题目描述
Binbin is a clever boy who likes to wear a skirt. One day, he saw some beautiful skirts on Taobao, but he had not enough money, which made him sad.

In order to make Binbin feel happy again, his’s friend Xu1024 gave him a treasure map. The treasure map is composed of three characters: ‘.’, ‘#’, ‘$’.

Character ‘.’ indicates that this position is empty and passable;

Character ‘#’ indicates that this position is blocked by a stone and not passable;

Character ‘$’ indicates that this position has 1 coin on it and is passablle.

The locations outside the treasure map are all empty and passable.

Binbin can move up, down, left, and right, but not at an angle.
Firstly, Binbin can choose any grid as starting point.
Because Binbin wanted to wear a skirt so much, he had a chance to teleport to a certain grid as another starting point.

Binbin wanted to know how many coins he could eventually collect to buy skirts.

Can you help him?
输入描述:
The first line of each group contains two numbers N and M,(1<=N, M<=500), representing the number of rows and the number of columns of treasure map.

Then there are next N lines, each line contains M characters. which describe the terasure map.
输出描述:
Ouput a integer representing
the maxinum money Binbin would collected.
示例1
输入
复制
4 4
. . . .
.##.
#$#.
.#.#
输出
复制
3
说明
Binbin can start at point(1,1) so he can collect 2 coins.

Then Binbin can teleport to point(3,2) so he can collect 1 coin.

Finally, Binbin would collect 3 coins.
示例2
输入
复制
4 4
.#$#

$$
输出
复制
7
说明
Binbin can start at point(1,3) so he can collect 1 coin.

Then Binbin can teleport to point(3,2) so he can collect 6 coins.

Finally, Binbin would collect 7 coins.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x7FFFFFFF;
  
int n,m,ans;
char a[505][505];
int vis[505][505];
int b[600000];
int op[4][2]={1,0,0,1,-1,0,0,-1};
  
void fun(int x,int y){
    vis[x][y] = 1;
    if(a[x][y] == '$')
        ans++;
  
    for(int i = 0;i < 4;i++){
        int e = x+op[i][0];
        int f = y+op[i][1];
        if(e >= 0 && e <= n && f >=0 && f <= m && vis[e][f] == 0 && a[e][f] != '#'){
            fun(e,f);
        }
    }
}
int main(){
    int len = 0;
    cin >> n >> m;
    for(int i = 1;i <= n;i++){
        cin >> (a[i]+1);
    }
  
    n++;
    m++;
    for(int i = 0;i <= n;i++){
        for(int j = 0;j <= m;j++){
            if(vis[i][j] == 0 && a[i][j] == '$'){
                ans = 0;
                fun(i,j);
                b[len++] = ans;
            }
        }
    }
    sort(b,b+len);
    int ans = 0;
    if(len-1 >= 0) ans += b[len-1];
    if(len-2 >= 0) ans += b[len-2];
    cout<<ans<<endl;
      
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值