POJ 2386 Lake Counting (DFS)
来源:互联网 发布:程序员一般工资多少 编辑:程序博客网 时间:2024/05/28 15:52
Lake Counting
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 32140 Accepted: 16061
Description
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.
Given a diagram of Farmer John's field, determine how many ponds he has.
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
Output
* Line 1: The number of ponds in Farmer John's field.
Sample Input
10 12W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.W.W.....W..W.W......W...W.......W.
Sample Output
3
Hint
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
Source
USACO 2004 November
思路:
题意不多说。。不明白为什么在输入nm的时候用~scanf 就不行,按理说不应该啊,加上~scanf 也不执行函数体 就一直处于待输入状态,去掉就出结果了。想不明白。
另外一个就是如果出现 [Error] ld returned 1 exit status。就说明系统exe文件还在运行,建议强制关闭之后重启dev。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[105][105];int sum;int n,m;void dfs(int i,int j){int dir[8][2]={{0,1},{0,-1},{-1,0},{1,0},{1,-1},{-1,1},{1,1},{-1,-1}};a[i][j]=0;for(int w=0;w<8;w++){int dx=i+dir[w][0];int dy=j+dir[w][1];if(dx>=0&&dx<n&&dy>=0&&dy<m&&a[dx][dy]==1)dfs(dx,dy);}return;}int main(){cin>>n>>m; sum=0;for(int i=0;i<n;i++)for(int j=0;j<m;j++){char x;cin>>x;if(x=='.')a[i][j]=0;if(x=='W')a[i][j]=1;}for(int i=0;i<n;i++)for(int j=0;j<m;j++){if(a[i][j]==1){dfs(i,j);sum++;}}//for(int i=0;i<n;i++)//for(int j=0;j<m;j++)//{//cout<<a[i][j]<<" ";//if(j==m-1)cout<<endl;// } printf("%d\n",sum);return 0;}
0 0
- POJ 2386 Lake Counting (DFS)
- poj 2386 Lake Counting (DFS~)
- POJ 2386 Lake Counting (dfs)
- POJ Lake Counting 2386(DFS)
- DFS:Lake Counting POJ 2386
- POJ 2386 Lake Counting DFS
- POJ 2386 Lake Counting [DFS]
- POJ 2386 Lake Counting - DFS
- POJ 2386 Lake Counting(DFS)
- POJ - 2386 dfs-Lake Counting
- POJ 2386 Lake Counting (DFS)
- 【DFS】poj 2386 Lake Counting
- POJ 2386 Lake Counting dfs
- POJ - Lake Counting(DFS)
- poj Lake Counting (dfs)
- poj 2386 Lake Counting 深搜DFS
- poj 2386Lake Counting(DFS, BFS)
- Lake Counting (POJ No.2386) DFS
- POJ 1654 Area
- android_107_ProgressBar旋转
- CCF201609-2 火车购票(100分)
- OpenStack基础之stevedore
- POJ3592 Instantaneous Transference题解
- POJ 2386 Lake Counting (DFS)
- Postgres安装指南-windows10-flask-py3
- MYSQL问题解决方案:Access denied for user 'root'@'localhost' (using password:YES)
- 比大小
- python 数据结构中被忽视的小技巧
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。
- Python相关文章索引(3)
- 企业级财富库socket开发和设计
- hibernate的crud操作