您的位置:首页 > 其它

洪水

2015-08-29 21:03 141 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: