POJ 之2386 Lake Counting
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 20003 | Accepted: 10063 |
Description
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* 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
Sample Input
10 12
W........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
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
#include <string.h>
#include <stdlib.h>
char map[101][101];
int vt[101][101];
int dir[8][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int n, m;
void dfs(int x, int y)
{
int i;
for(i=0; i<8; i++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(map[xx][yy]=='W' && xx>=0 && xx<m && yy>=0 && yy<n && vt[xx][yy]==0 )
{
vt[xx][yy] = 1;
dfs(xx, yy);
}
}
}
int main()
{
int cnt;
int i, j;
while(scanf("%d %d%*c", &m, &n)!=EOF)
{
if(m==0 && n==0)
break;
cnt = 0;
memset(vt, 0, sizeof(vt));
for(i=0; i<m; i++)
{
scanf("%s", map[i]);
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
if(map[i][j]=='W' && vt[i][j]==0)
{
vt[i][j] = 1;
cnt++;
dfs(i, j);
}
}
}
printf("%d\n", cnt);
}
return 0;
}
POJ 之2386 Lake Counting的更多相关文章
- POJ No.2386 Lake Counting
题目链接:http://poj.org/problem?id=2386 分析:八联通的则为水洼,我们则需遍历一个单位附近的八个单位并将它们都改成'.',但附近单位可能仍连接着有'W'的区域,这种情况下 ...
- POJ 2386 Lake Counting(深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ...
- POJ 2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28966 Accepted: 14505 D ...
- [POJ 2386] Lake Counting(DFS)
Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...
- POJ 2386 Lake Counting(搜索联通块)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- POJ 2386 Lake Counting 八方向棋盘搜索
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53301 Accepted: 26062 D ...
- poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)
http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...
随机推荐
- PHP面试题及答案解析(3)—MySQL数据库
1.mysql_num_rows()和mysql_affected_rows()的区别. mysql_num_rows()和mysql_affected_rows(),这两个函数都作用于 mysql_ ...
- MQTT--入门
一.简述 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议 ...
- java游戏开发基础Swing之JCheckBox
© 版权声明:本文为博主原创文章,转载请注明出处 1.复选框(JCheckBox) 使用复选框可以完成多项选择.Swing中的复选框与AWT中的复选框相比,优点是Swing复选框中可以添加图片 JCh ...
- const_cast去除const限制,同一片内存
本质很简单,但一些优化 和 编程上的错误,却让人看不清本质. :const_cast<type_id> (expression) 该运算符用来修改类型的const或volatile属性.除 ...
- linux权限的深入讨论
1. 怎样查看文件的权限 1) 掌握使用ls –l命令查看文件上所设定的权限. drwxr-xr-x. 2 root root 6 May 26 2017 binfmt.d 权限信 ...
- java.lang.IllegalArgumentException: Request header is too large的解决方法
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&q ...
- warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'.
'matching'参数是 git 1.x 的默认行为,其意是如果你执行 git push 但没有指定分支,它将 push 所有你本地的分支到远程仓库中对应匹配的分支. 而 Git 2.x 默认的是 ...
- 【JMeter4.0】之 “jdk1.8、JMeter4.0” 安装与配置以及JMeter永久汉化和更改界面背景、并附加附录:个人学习总结
目录: 一.首先,需要安装.配置jdk 二.其次,安装.配置JMeter 三.JMeter汉化以及更改界面背景 四.附录:个人学习总结 一.首先,需要安装.配置jdk 返回目录 1.到官网下载1. ...
- Hadoop学习笔记(二)——zookeeper使用和分析
分布式架构是中心化的设计.就是一个主控机连接多个处理节点,因此保证主控机高可用性十分关键.分布式锁是解决该问题的较好方案,多主控机抢一把锁.Zookeeper就是一套分布式锁管理系统,用于高可靠的维护 ...
- ios - 使用@try、catch捕获异常:
@try { // 可能会出现崩溃的代码 } @catch (NSException *exception) { // 捕获到的异常exception } @finally { // 结果处理 }