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). ...
随机推荐
- Linux下利用phpize安装memcashe的php源码扩展包
phpize是php的一种构建工具,为PHP扩展准备构建环境,通过phpize可以编译php的扩展源码文件为php扩展模块. 一.安装 phpize工具可以通过安装php-dev包自动集成安装.安装完 ...
- MySQL5.5中文支持
1. /etc/my.cnf.d/client.cnf [client] #password = [your_password] port = 3306 socket = /tmp/mysql.soc ...
- 计算机图形学(二)输出图元_6_OpenGL曲线函数_2_中点画圆算法
中点画圆算法 如同光栅画线算法,我们在每一个步中以单位间隔取样并确定离指定圆近期的像素位置.对于给定半径r和屏幕中心(xc,yc),能够先使用算法计算圆心在坐标原点(0, 0)的圆的像素 ...
- 【整理】Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来或打印出来却是乱码
转自:http://www.crifan.com/python_already_got_correct_encoding_string_but_seems_print_messy_code/ [背景] ...
- Project Euler:Problem 87 Prime power triples
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...
- angualejs
http://segmentfault.com/a/1190000000347412 http://www.xker.com/page/e2015/06/199141.html http://www. ...
- 同样的代码在java和c++中结果不同
#include <iostream> using namespace std; /* run this program using the console pauser or add y ...
- Android hellocharts 柱形图详解
近日需要做图表结构的项目,目前最火的就是hellocharts 和MPAndroidChart 相对来说hellocharts集成比较简单: 官网地址 https://github.com/l ...
- Pollard-Rho大整数拆分模板
随机拆分,简直机智. 关于过程可以看http://wenku.baidu.com/link?url=JPlP8watmyGVDdjgiLpcytC0lazh4Leg3s53WIx1_Pp_Y6DJTC ...
- POJ1942
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24236 Accepted: 6006 ...