挖出最大财宝

news/2024/7/5 10:18:11

Description

给你一个大小为m*n的矩阵Mine表示矿场,里面有丰富的宝石。Mine(i,j)若大于0,表示该点存在宝石,其值为宝石价值;若等于0,表示的是不可进入的区域;若小于-1表示危险区域。当你选择一个点挖宝石的时候,你可以把相邻(上下左右)的宝石也挖出来。但是注意,任意你挖出宝石的点不可与危险区域相邻。

请计算你从某一点作为起点能挖出的最大财富,财富等于你挖出宝石价值的总和。

Input

第一行给出m和n,第二行给出Mine的一维表示
1<=m,n<=50
-1<=Mine[i][j]<=10

Output

整数最大价值

Sample Input 1

3 3
1 0 0 4 0 1 2 3 1

Sample Output 1

12

Sample Input 2

3 3
0 1 0 1 1 1 0 1 0

Sample Output 2

5

Sample Input 3

2 3
1 2 3 4 -1 5

Sample Output 3

3

#include<iostream>
using namespace std;
int Mine[51][51];
int maxn=0;
int m,n;
bool vv[51][51]={false};
int DFS(int a,int b){
    int max=0;
    vv[a][b]=true;
    max+=Mine[a][b];
    for(int i=0;i<4;i++){
        if(b+1<=n-1&&Mine[a][b+1]!=0&&vv[a][b+1]==false)
        {
            max+=DFS(a,b+1);
        }
        if(a+1<=m-1&&Mine[a+1][b]!=0&&vv[a+1][b]==false){
            max+=DFS(a+1,b);
        }
        if(b-1>=0&&Mine[a][b-1]!=0&&vv[a][b-1]==false){
            max+=DFS(a,b-1);
        }
        if(a-1>=0&&Mine[a-1][b]!=0&&vv[a-1][b]==false){
            max+=DFS(a-1,b);
        }
    }
    return max;
}
int main(){
    
    cin>>m>>n;
    //cout<<"dsal";
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++){
            cin>>Mine[i][j];
        }
   
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
        {
            if(Mine[i][j]<0){
                Mine[i][j]=0;
                if(i==0&&j==0)
                {
                    Mine[i][j+1]=0;
                    Mine[i+1][j]=0;
                }
                else if(i==0){
                    Mine[i][j-1]=0;
                    Mine[i][j+1]=0;
                    Mine[i+1][j]=0;
                }
                else if(j==0){
                    Mine[i-1][j]=0;
                    Mine[i+1][j]=0;
                    Mine[i][j+1]=0;
                }
                else{
                    Mine[i-1][j]=0;
                    Mine[i+1][j]=0;
                    Mine[i][j-1]=0;
                    Mine[i][j+1]=0;
                }
            }
        }
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
        {
            if(Mine[i][j]>0){
                int temp=DFS(i,j);
                if(maxn<temp){
                    maxn=temp;
                }
            }
        }
    cout<<maxn;
    return 0;

}

http://www.niftyadmin.cn/n/4636573.html

相关文章

【Linux操作系统】多线程抢票逻辑——学习互斥量(锁)函数接口

文章目录 1.进程线程间的互斥相关背景概念2.联系代码学习同步互斥问题3.互斥量&#xff08;锁&#xff09;的函数接口3.1初始化互斥量3.2销毁互斥量3.3互斥量加锁和解锁3.4改进多线程抢票代码 1.进程线程间的互斥相关背景概念 临界资源&#xff1a;多线程执行流共享的资源就叫…

1067 Sort with Swap(0, i)

Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the followi…

1090 Highest Price in Supply Chain

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;经销商&#xff09;, and suppliers&#xff08;供应商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppl…

linux 设备文件和设备之间联系的建立

<设备驱动模型> 注&#xff1a;几乎所有的设备结构体都包含"strcut kobject kobj"和"srtuct list_head list"该结构体。struct kobject kobj: 该结构体用于构建Linux设备驱动模型的模型建立 struct list_head { struct list_head *prev,*next; }; 该…

mysql-存储过程-触发器-事务---4

本节所讲内容&#xff1a; 存储过程 触发器事务一、存储过程 什么是存储过程 大多数SQL语句都是针对一个或多个表的单条语句。并非所有的操作都怎么简单。经常会有一个完整的操作需要多条才能完成。存储过程&#xff08;Stored Procedure&#xff09;是在大型数据库系统中&…

Linux系统安装与初用 实验报告

南京信息工程大学实验报告 一、实验目的 1.掌握 linux 系统的安装方法 2.理解 linux 系统安装过程中涉及的基础知识 3.熟悉 linux 系统的操作环境 4.尝试简单的 linux shell 命令 二、实验准备 1. 围绕下述问题结合教材、课件、互联网学习指定内容。 问题&#xff1a; &#xf…

Maven学习笔记三(Eclipse创建Maven项目)

配置 Eclipse Maven 环境1.配置 Manen 地址将下载的Maven导入进来&#xff0c;然后勾选使用2.设置 setting.xml 地址选中Maven下conf目录下的settings.xml&#xff0c;然后local Repository会自动识别出设置的local Repository创建 Maven 项目选中模板 &#xff08;如果是创建w…

1058 A+B in Hogwarts easy

If you are a fan of Harry Potter, you would know the world of magic has its own currency system — as Hagrid explained it to Harry, “Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it’s easy enough.” Your job is to write a progr…