洪水

http://codevs.cn/problem/3411/
昨天的BFS没好好做,粗略的理解了下,今天的好好的理解了下。并写了代码注释,关于bfs部分的代码没写详细解释,请自行百度bfs。

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int n,m;
//用来存储输入进去的每个位置的高度
int a[1005][1005]={0};
//用了记录访问过了吗
int visited[1005][1005]={0};
//用来存储到达的点
struct mystruct
{
    int x,y;
}start;
//用了检验洪水是否漫过
bool check(mystruct ot,mystruct xt)
{
  if(xt.x<1||xt.x>n||xt.y<1||xt.y>m)return false;
  if(a[ot.x][ot.y]<a[xt.x][xt.y])return false;
  if(visited[xt.x][xt.y])return false;
  visited[xt.x][xt.y]=1;
  return true;
}
int main()
{
    cin>>n>>m;
    //输入每个位置的高度
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            cin>>a[i][j];
        }
    }
    int aa,bb;
    //输入洪水起始位置
    cin>>aa>>bb;
    //定义结构体的队列
    queue<mystruct>q;
    start.x=aa;
    start.y=bb;
    //初始位置入列
    q.push(start);
    mystruct node,xnode;
    //定义上下左右4种移动状态
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    //标记被洪水漫过的位置数
    int count=1;
    //广度优先搜索
    while(!q.empty())
    {
        node=q.front();
        q.pop();
        for (int i = 0; i <=3; ++i)
        {
            /* code */
            xnode.x=node.x+dx[i];
            xnode.y=node.y+dy[i];
            if (check(node,xnode))
            {
                /* code */
                count++;
                q.push(xnode);
            }
        }
    }
    cout<<n*m-count<<endl;
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值