洪水
2015-08-29 21:03
141 查看
http://codevs.cn/problem/3411/
昨天的BFS没好好做,粗略的理解了下,今天的好好的理解了下。并写了代码注释,关于bfs部分的代码没写详细解释,请自行百度bfs。
昨天的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; }
相关文章推荐
- Android开发环境搭建及常见问题解决方法
- linux命令行快捷键
- 【Android】WebView读取本地图片
- 不要死性不改
- od、hexdump、xxd---进制查看工具
- pat(B) 1010. 一元多项式求导
- 《IOS_C语言》高级指针(结构体指针、结构体数组指针、结构体数组指针,作为函数的参数使用、预编译指令)
- MVC+EF登陆 并跳转成功页面
- 【项目】git的部署使用
- MongoDB(4)--MongoDB服务的启动
- 初识JAVA3(重载、单子模式)
- 2.GitHub译文之创建一个仓库
- Java数据结构面试题
- [总结]关系型数据库规范化
- 对眼睛好的颜色
- [leetcode]Happy Number C语言
- sqlite之语句多重引号处理
- UVa 1663 - Purifying Machine(二分匹配)
- String和StringBuffer
- poll&&epoll实现分析(一)—poll实现