【BFS】-POJ-2386-Lake Counting
来源:互联网 发布:网络推销员 编辑:程序博客网 时间:2024/06/09 19:47
题目链接:http://poj.org/problem?id=2386
题目描述:
八个格内关联的W算一个湖,问地图中有几个湖?
解题思路:
简单的BFS,做过类似的题,回来复习一下,有些手生,不过还是1A了。
AC代码:
#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;typedef struct node{ int x; int y;} ;char grid[120][120];bool use[120][120];int N,M;queue<node> xx;int dx[8]={1,1,1,-1,-1,-1,0,0}, dy[8]={1,0,-1,1,0,-1,1,-1};bool ok(int x,int y){ if(x>=0&&x<N&&y>=0&&y<M&&grid[x][y]=='W') return 1; return 0;}void bfs(node st){ while(!xx.empty())xx.pop(); xx.push(st); while(!xx.empty()) { node cur=xx.front(); use[cur.x][cur.y]=1; xx.pop(); for(int i=0;i<8;i++) { node temp; temp.x=cur.x+dx[i]; temp.y=cur.y+dy[i]; if(ok(temp.x,temp.y)&&!use[temp.x][temp.y]) { xx.push(temp); use[temp.x][temp.y]=1; } } }}int main(){ memset(grid,0,sizeof(grid)); memset(use,0,sizeof(use)); int ans=0,i,j; cin>>N>>M; for(i=0;i<N;i++) scanf("%s",grid[i]); for(i=0;i<N;i++) for(j=0;j<M;j++) { if(grid[i][j]=='W'&&!use[i][j]) { ans++; node st; st.x=i;st.y=j; bfs(st); } } cout<<ans<<endl; return 0;}
AC截图: 0 0
- poj 2386 Lake Counting (BFS~)
- POJ 2386 Lake Counting-------BFS
- 【BFS】-POJ-2386-Lake Counting
- Poj 2386 Lake Counting【Bfs】
- 【BFS】poj 2386 Lake Counting
- poj 2386Lake Counting(DFS, BFS)
- poj 2386Lake Counting(BFS搞定)
- poj 2386 Lake Counting 解题报告(BFS)
- POJ 2386 Lake Counting (DFS or BFS)
- POJ 2386 Lake Counting(dfs or bfs)
- POJ 2386 Lake Counting(BFS)
- 【poj】2386 Lake Counting bfs||dfs
- poj 2386 Lake Counting
- poj 2386 Lake Counting
- POJ 2386 Lake Counting
- poj 2386 Lake Counting
- poj 2386 Lake Counting
- poj 2386 Lake Counting
- Servlet、Filter、Listener、Interceptor
- 家居风水吉凶自测法
- B树索引(一、二)
- ThoughtWorks程序员读书雷达
- Java面向对象——集合框架(Map)
- 【BFS】-POJ-2386-Lake Counting
- 小米三怎么打开usb调试
- Android绘图相关类文档学习与翻译(二)
- 图像的透视变换
- XULRUNNER
- 关于TDI过滤驱动中的TDI_SEND
- 性能测试--运行时间设置和迭代次数区别
- Linux学习记录--文件系统简介
- android 与 linux的动态链接