POJ-2386题解
来源:互联网 发布:西北工业大学网络教育 编辑:程序博客网 时间:2024/06/17 22:07
这是一题经典到足以当模板的简单dfs题目
题目:有一个大小为NxM的园子,雨后积起了水。八连通的积水被认为是连接在一起的。请求出园子里总共有多少水洼?(N,M≤100)
样例输入:
10 12W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.W.W.....W..W.W......W...W.......W.
样例输出:
3
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;int n,m;char field[105][105]; //园子void dfs(int x,int y){ field[x][y]='.'; for(int dx = -1;dx <= 1;dx++){ for(int dy = -1;dy <= 1;dy++){ int nx=x+dx,ny=y+dy; if(nx>=0 && nx<n && ny<m && ny>=0 && field[nx][ny] =='W') dfs(nx,ny); } } return ;}void solve(){ int res = 0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(field[i][j]=='W'){ dfs(i,j); res++; } } } printf("%d\n",res);}void init(){ scanf("%d %d",&n,&m); for(int i=0;i<n;i++) scanf("%s",field[i]);}int main(){ //freopen("input.txt","r",stdin); init(); solve(); return 0;}
阅读全文
0 1
- POJ-2386题解
- Poj 2386 Lake Counting 题解
- POJ 2386 Lake Counting 搜索题解
- POJ 1011 题解
- poj-1050 题解
- poj-1088题解
- poj-1159题解
- POJ 1009题解
- POJ 1458题解
- Poj 2352 Stars 题解
- POJ 2739题解---小菜
- POJ 1753题解
- POJ 1328题解
- POJ 2049题解
- poj 2679题解
- poj 1149题解
- poj 2790迷宫题解
- POJ - Counterfeit Dollar 题解
- 详细记录python的range()函数用法
- ArrayList
- HyperLedger Fabric:自顶向下的方法--第1篇 编译fabric源码及手工搭建单个Peer节点网络
- CSS 让数字滑动显示
- 泛型类与接口、泛型方法
- POJ-2386题解
- 二维码通讯录的实现
- Align With View和Align View To Select之妙用
- wait()函数,自杀函数abort(),WIFEXITED WIFSIGNALED
- Vue新手入门-1
- Sprites,Editor使图片生成prefab
- 数据库Oracle 数据导入/导出说明
- python any()和all()用法
- Rhyme/Java自定义队列