问这个人掉落的海域包含几个岛屿。

八方向相连为同一片海域,四方向相连为同一个岛屿。与边界相连的岛屿不算。

方法:在给定地图外面填充一圈".",从这个人掉落的地方开始进行floodfill,标出他所在的海域。

然后再从(0, 0)点进行一次floodfill,把之前标记的海域之外的东西全部清掉。

最后统计岛屿个数即可。

注意:坐标是反着的

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm> using namespace std; const int MAXN = ; const int dx[] = { -, , , , -, -, , , - };
const int dy[] = { , -, , , -, , , -, - }; struct Point
{
int x, y;
Point( int x = , int y = ): x(x), y(y) { }
}; Point Q[MAXN * MAXN * ];
char G[MAXN][MAXN];
bool vis[MAXN][MAXN];
int R, C, X, Y; bool check( int x, int y, bool wh )
{
if ( wh ) return x > && x <= R && y > && y <= C;
else return x >= && x <= R + && y >= && y <= C + ;
} void FloodFill( Point st, char ch1, char ch2, char ch3, int dir )
{
int front = , rear = ;
bool wh = false;
if ( dir == ) wh = true; Q[rear++] = st; while ( front < rear )
{
Point p = Q[front]; G[ p.x ][ p.y ] = ch3;
vis[ p.x ][ p.y ] = true;
//printf( "%d %d\n", p.x, p.y );
for ( int i = ; i < dir; ++i )
{
int xx = p.x + dx[i];
int yy = p.y + dy[i];
//printf( "vis[%d][%d] = %d\n", xx, yy, vis[xx][yy] );
if ( check( xx, yy, wh ) && !vis[xx][yy] && ( G[xx][yy] == ch1 || G[xx][yy] == ch2 ) )
{
vis[xx][yy] = true;
Q[rear++] = Point( xx, yy );
}
} ++front;
}
return;
} int main()
{
//freopen("s.txt", "w", stdout );
while ( ~scanf( "%d%d%d%d", &C, &R, &Y, &X ) )
{
memset( G, '.', sizeof(G) );
G[][ C + ] = '\0';
for ( int i = ; i <= R; ++i )
{
scanf( "%s", &G[i][] );
G[i][ C + ] = '\0';
} memset( vis, false, sizeof(vis) );
FloodFill( Point( X, Y ), '.', '.', '*', );
memset( vis, false, sizeof(vis) );
FloodFill( Point( , ), '.', '#', ' ', ); memset( vis, false, sizeof(vis) );
int ans = ;
for ( int i = ; i <= R; ++i )
for ( int j = ; j <= C; ++j )
if ( G[i][j] == '#' )
{
++ans;
FloodFill( Point( i, j ), '#', '#', '$', );
}
printf( "%d\n", ans );
}
return ;
}

URAL 1250 Sea Burial 简单Floodfill的更多相关文章

  1. Ural 1250 Sea Burial 题解

    目录 Ural 1250 Sea Burial 题解 题意 输入 题解 程序 Ural 1250 Sea Burial 题解 题意 给定一个\(n\times m\)的地图,\(.\)为水,\(\#\ ...

  2. ural 1250. Sea Burial

    1250. Sea Burial Time limit: 1.0 secondMemory limit: 64 MB There is Archipelago in the middle of a s ...

  3. sea.js简单使用教程

    sea.js简单使用教程 下载sea.js, 并引入 官网: http://seajs.org/ github : https://github.com/seajs/seajs 将sea.js导入项目 ...

  4. ural 1250

    有点坑的dfs  看懂题应该就会做了 神圣海必然围成一个圈  dfs将神圣还外围的全部去掉   简单题 #include <cstdio> #include <cstring> ...

  5. URAL 1203 Scientific Conference 简单dp 难度:0

    http://acm.timus.ru/problem.aspx?space=1&num=1203 按照结束时间为主,开始时间为辅排序,那么对于任意结束时间t,在此之前结束的任务都已经被处理, ...

  6. URAL - 1091 Tmutarakan Exams (简单容斥原理)

    题意:K个不同数组成的集合,每个数都不超过S且它们的gcd>1.求这样的数的个数 分析:从2开始枚举gcd,但这样会发生重复.譬如,枚举gcd=2的集合个数和gcd=3的集合个数,枚举6的时候就 ...

  7. URAL 1326. Bottle Taps(简单的状压dp)

    题目不太好读懂,就是先给你一个n代表要从n个物品中买东西,然后告诉你这n个东西的单位价格,在给你m个集合的情况.就是每一个结合中有x件物品.他们合起来买的价格是k.这x件物品依次是:p1--px.之后 ...

  8. ural 1009. K-based Numbers(简单dp)

    http://acm.timus.ru/problem.aspx?space=1&num=1009 题意:将一个n位数转化为合法的K进制数,有多少种情况.合法的K进制数即不含前导0,且任意两个 ...

  9. URAL 1513. Lemon Tale(简单的递推)

    写几组数据就会发现规律了啊. .但是我是竖着看的.. .还找了半天啊... 只是要用高精度来写,水题啊.就当熟悉一下java了啊. num[i] = 2*num[i-1]-num[i-2-k]. 15 ...

随机推荐

  1. ios 百度地图

    百度地图  中的注意事项 1. 百度地图中 使用了c++   设置buidSeting compoileSource 为 Object-C++  强制使用oc++编译器 2. 设置  BuidSeti ...

  2. VSFTPD全攻略(/etc/vsftpd/vsftpd.conf文件详解)

    /etc/vsftpd/vsftpd.conf文件详解,分好类,方便大家查找与学习 #################匿名权限控制############### anonymous_enable=YE ...

  3. vitrualbox虚拟机64位安装报错解决

    1 NtCreateFile(\Device\VBoxDrvStub) failed: 0xc0000034 STATUS_OBJECT_NAME_NOT_FOUND (0 retries) 解决办法 ...

  4. CSS 外边距(margin)重叠及防止方法

    边界重叠是指两个或多个盒子(可能相邻也可能嵌套)的相邻边界(其间没有任何非空内容.补白.边框)重合在一起而形成一个单一边界. 两个或多个块级盒子的垂直相邻边界会重合.结果的边界宽度是相邻边界宽度中最大 ...

  5. Android 添加Button事件

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...

  6. self._raiseerror(v) File "D:\GameDevelopment\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror

    D:\BaiDuYun\Plist>python unpack_plist.py lobbyRelieveTraceback (most recent call last): File &quo ...

  7. 使用行为树(Behavior Tree)实现网游奖励掉落系统

    原地址:http://blog.csdn.net/akara/article/details/6165421 [原创]使用行为树(Behavior Tree)实现网游奖励掉落系统by AKara 20 ...

  8. 自绘按钮,添加Color属性(转载)

    在标准的Windows程序中所有按钮均没有颜色.因此Delphi提供的所有按钮组件也均无颜色属性,有时你可能做了一个五颜六色的程序界面,而按钮颜色可能很不相称. 在此本人提供一种用自定义组件制作有颜色 ...

  9. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  10. JSONObject 包的依赖

    commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json- ...