CODE[VS] 3411 洪水

题目描述 Description

小浣熊松松和朋友到野外露营,没想到遇上了π年一次的大洪水,好在松松是一只爱观察的小浣熊,他发现露营地的地形和洪水有如下性质:

①露营地可以被看做是一个N*M的矩形方阵,其中左上角坐标为(1,1),右下角坐标为(n,m),每个格子(i,j)都有一个高度h(i,j)。

②洪水送(r,c)开始,如果一个格子被洪水淹没,那这个格子四周比它低(或相同)的格子也会被淹没。

现在松松想请你帮忙算算,有多少个格子不会被淹没,便于他和朋友逃脱。

【原有误数据已删除】

输入描述 Input Description

第一行包含两个整数n,m,表示矩形方阵右下角坐标。

以下n行,每行m个数,第i行第j个数表示格子(i,j)的高度。

最后一行包含两个整数r,c,表示最初被洪水淹没的格子。

输出描述 Output Description

输出仅一行,为永远不会被淹没的格子的数量。

样例输入 Sample Input

3 3

1 2 3

2 3 4

3 4 5

2 2

样例输出 Sample Output

5

数据范围及提示 Data Size & Hint

对于90%的数据,保证随机生成。

对于100%的数据,1<=N,M<=1000。

标签是bfs,

我只会用dfs。

下面是dfs爆搜。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

int n,m,h[1002][1002],a,b,ans=1;
int xx[5]={-1,1,0,0},yy[5]={0,0,1,-1};
bool w[1002][1002],vis[1002][1002];

void dfs(int x,int y)
{
    vis[x][y]=1;
    for(int i=0;i<4;++i)
    {
        int dx=x+xx[i],dy=y+yy[i];
        if(dx<1||dx>n||dy<1||dy>m||w[dx][dy]==1||vis[dx][dy]==1)
            continue;
        if(h[dx][dy]<=h[x][y])
        {
            w[dx][dy]=1;
            ans++;
        }
    }
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            if(w[i][j]==1&&!vis[i][j])
                dfs(i,j);
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            scanf("%d",&h[i][j]);
    scanf("%d%d",&a,&b);
    w[a][b]=1;
    dfs(a,b);
    printf("%d",n*m-ans);
}

DFS75

这个也是dfs,还是很慢,不过能过这道题,

对比一下思路吧。

#include<cstdio>
#include<iostream>
using namespace std;

int next[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
int mp[1000][1000];
int a[1000][1000];
int n,m,sum=0;

void dfs(int x,int y)
{
    sum++;
    int t;
    t=mp[x][y];
    int i,j;
    for(i=0; i<4; i++)
    {
        int xx=x+next[i][0];
        int yy=y+next[i][1];
        if(xx<1||yy<1||xx>n||yy>m)
            continue ;
        if(mp[xx][yy]<=t&&a[xx][yy]==0)
        {
            a[xx][yy]=1;
            dfs(xx,yy);
        }
    }
}

int main()
{
    int i,j;
    cin>>n>>m;
    for(i=1; i<=n; i++)
        for(j=1; j<=m; j++)
            cin>>mp[i][j];
    int x;
    int y;
    cin>>x>>y;
    a[x][y]=1;
    dfs(x,y);
    cout<<n*m-sum<<endl;
    return 0;
}

dfs AC

有人说这是个bfs的板子。

hh,反正我不会。

狗子zxl又在外面发火了。。。

另附一份bfsAC代码。

自行理解。

#include<iostream>
#include<queue>
using namespace std;

struct point
{
    int x,y;
};

int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
int lock[1001][1001] = {0};
int a[1001][1001];
int n, m, sum=0;

void BFS(int x, int y)
{
    queue <point> q;
    point now,  next;
    now.x = x,   now.y = y;
    q.push(now);
    lock[now.x][now.y] = -1;
    while(!q.empty())
    {
        now = q.front();
        for(int i=0; i<4; i++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];
            if(next.x<=0||next.y<=0||next.x>n||next.y>m) //判断是否越界
                continue;
            if((a[next.x][next.y]<=a[now.x][now.y])&&lock[next.x][next.y]!=-1)//如果洪水可以淹没,且点没有访问过
            {
                q.push(next);  //该点入栈
                lock[next.x][next.y] = -1;
            }
        }
        q.pop();
        sum++;
    }
}

int main()
{
    int p1, p2;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>a[i][j];
    }
    cin>>p1>>p2;
    BFS(p1,p2);
    cout<<n*m-sum<<endl;
    return 0;
}

bfs

原文地址:https://www.cnblogs.com/Mary-Sue/p/9163418.html

时间: 2024-08-03 09:07:13

CODE[VS] 3411 洪水的相关文章

深搜整理汇总

2801 LOL-盖伦的蹲草计划 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫盖伦的英雄.他的伟大之处在于他特别喜欢蹲草丛阴人(XL:蹲草阴人也算英雄?!CZQ:没办法,个个都是这么玩的).某日,德玛西亚与诺克萨斯之间又发生了一场战斗,嘉文四世希望盖伦能带领一支K人的德玛西亚军队出战. 战斗发生在召唤师峡谷.整个召唤师峡谷被分割成M行N列的一个矩阵,矩阵中有空地和几片草丛.这几片草丛中有

POJ 3411 Mondriaan&#39;s Dream 【状压Dp】 By cellur925

Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles),

【转】Windows Error Code(windows错误代码详解)

本文来自: http://blog.sina.com.cn/s/blog_5e45d1be0100i0dr.html http://blog.sina.com.cn/s/blog_5e45d1be0100i0dt.html http://blog.sina.com.cn/s/blog_5e45d1be0100i0dv.html 这三篇,因为格式实在太乱,因此拿来整理了一下.找这个的原因是今天在改程序的时候蹦出来个WindowsError: [Error 2],也没有说这个东西是什么错误.于是百度

从微信官方获取微信公众号名片:http://open.weixin.qq.com/qr/code/?username=haihongruanjian

从微信官方获取微信公众号名片:http://open.weixin.qq.com/qr/code/?username=haihongruanjian 个人的号,不知道怎么获取.

微信 {&quot;errcode&quot;:40029,&quot;errmsg&quot;:&quot;invalid code, hints: [ req_id: Cf.y.a0389s108 ]&quot;}

{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"} 问题:微信网页授权后,获取到 openid 了,一刷新又没了 微信网页授权获取到的 code 只能使用一次(5分钟内有效),使用一次后,马上失效. 页面授权跳转成功,根据 code 也换取到 openid 了. 此时刷新页面,并不会再次进行授权,而是直接刷新了一下上一次授权跳转后的链接,带的还是

在linux系统中安装VSCode(Visual Studio Code)

1.从官网下载压缩包(话说下载下来解压就直接可以运行了咧,都不需要make) 访问Visual Studio Code官网 https://code.visualstudio.com/docs?dv=linux64 我是64位的: wget https://az764295.vo.msecnd.net/stable/7ba55c5860b152d999dda59393ca3ebeb1b5c85f/code-stable-code_1.7.2-1479766213_amd64.tar.gz 2.解

set up trace code tool

這以 GNU GLOBAL 6.5.6 為示範 1: install GNU GLOBAL https://www.gnu.org/software/global/download.html sudo ./configure; 若有以下 error,請看更下方的 Q5 說明. configure: checking "location of ncurses.h file"... configure: error: curses library is required but not f

Ubuntu16.04下安装VS Code

在Ubuntu下面安装Visual Studio Code sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make sudo apt-get update sudo apt-get install ubuntu-make umake web visual-studio-code 终于又看见亲切的大麻花了

CSDN code使用教程之git用法详解

首先需要下载GIT客户端,http://git-scm.com/downloads...   然后再code.csdn.net上面创建一个项目,如果 你的项目已经存在,那么请建立项目的时候不要选择自动生成readme文件.填写项目名称,去掉下面的勾勾,然后点击创建就OK了. 下面的就是配置本地客户端了,确认你在CSDN id,获取的方式是在登录后,进入passport.csdn.net,在"个人帐号"的最下端查看用户名:也就是你的昵称,我的就是Linux_Google 然后在命令行中输