1250. Sea Burial

Time limit: 1.0 second
Memory limit: 64 MB
There is Archipelago in the middle of a shoreless ocean. An ancient tribe of cannibals lives there. Shamans of this race have been communicating with gods and admonishing people for ages. They could generate a rain during a drought and clear the sky in a raining season. A long time ago the first shaman of the tribe jumped into one of the seas and drowned while being in a sacred trance. Since then all the land inside this sea is regarded as sacred. According to an ancient law, all shamans must be buried on a sacred land. However, souls of dead shamans cannot get on with each other, so each shaman must be buried on a separate island. An old prophecy says that if two shamans are buried on the same land, then a dreadful time will come and the tribe will perish.
How many shamans will the tribe outlive? This problem bothered all the chiefs of the tribe who were coming into power. So one of the chiefs ordered to compile a map of Archipelago. The cannibals toiled for a whole year and coped with the task. But the map was too large and complicated to count all the sacred islands. So the tribe's shaman appealed to gods and asked them to help with counting the islands. And the tribe was blessed with a Programmer and a Computer, which came down to earth in a cloud of light and fire. Yes, you are this Programmer, and you are destined to live with these cannibals until you have counted the islands; then you'll be transferred back home. Remember that there may be seas inside islands, and islands inside those seas.

Input

The first input line contains four integers WHX and Y, separated with one or several spaces. 1 ≤ WH ≤ 500 are respectively the width and the height of the map. 1 ≤ X ≤ W and 1 ≤ Y ≤ Hare the coordinates of the point where the first shaman drowned. The next H lines contain description of the map. Each line contains W symbols describing correspondent fragments of the map. Symbol "." stands for a sea fragment and symbol "#" stands for a land fragment. Two fragments belong to the same sea if they are adjacent horizontally, vertically, or diagonally. Two fragments belong to the same island if they are adjacent horizontally or vertically (but not diagonally). Land fragments that are adjacent to the map's border are not considered as islands. Coordinates are counted from the left upper corner.

Output

The output should contain a single integer, which is the number of the islands inside the sacred sea.

Samples

input output
9 7 1 1
........#
.#######.
.#.....#.
.#.#.#.#.
.#.....#.
.#######.
#........
3
9 7 3 3
........#
.#######.
.#.....#.
.#.#.#.#.
.#.....#.
.#######.
#........
2
Problem Author: Stanislav Skorb (prepared by Ivan Dashkevich)
Problem Source: Open collegiate programming contest for student teams, Ural State University, March 15, 2003
Difficulty: 723
 
题意:给出一张海图,#代表陆地,.代表海洋。
八方向相连为同一片海域,四方向相连为同一个岛屿。与边界相连的岛屿不算。
给出一个点,问这个点所在海域包含了几个岛屿。
注意如果包含一个岛屿,那么这个岛屿的内海包含的岛屿也算。
 
分析:暴力搜索。注意处理即可。
可以先把掉落所在海域求出。
然后将不在海域之内的所有格子标记。
然后问题就变为求一张图内#连通块的个数。
 
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
const int DX[] = {-, , , , -, -, , }, DY[] = {, -, , , -, , -, };
#define Land 1
#define Sea 2
int n, m, sx, sy;
int graph[N][N], mark[N][N];
int ans; inline void Input()
{
scanf("%d%d%d%d", &n, &m, &sx, &sy);
swap(sx, sy), swap(n, m);
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
{
char ch = ' ';
while(ch != '.' && ch != '#') ch = getchar();
graph[i][j] = ch == '.' ? Sea : Land;
}
} inline bool Check(int x, int y, int state)
{
if(x < || x > n || y < || y > m) return ;
if(!(graph[x][y] & state) || mark[x][y]) return ;
return ;
} inline void Draw(int sx, int sy, int state, int tag)
{
static int que[N * N][], tail;
mark[sx][sy] = tag;
tail = , que[][] = sx, que[][] = sy;
for(int head = ; head <= tail; head++)
{
int ux = que[head][], uy = que[head][];
for(int t = ; t < ; t++)
{
if(t > && graph[ux][uy] == Land) break;
int vx = ux + DX[t], vy = uy + DY[t];
if(!Check(vx, vy, state)) continue;
mark[vx][vy] = tag, tail++;
que[tail][] = vx, que[tail][] = vy;
}
}
} inline void Solve()
{
if(graph[sx][sy] == '#')
{
puts("");
return;
} Draw(sx, sy, Sea, );
for(int i = ; i <= n; i++)
{
Draw(i, , Land | Sea, );
Draw(i, m + , Land | Sea, );
}
for(int i = ; i <= m; i++)
{
Draw(, i, Land | Sea, );
Draw(n + , i, Land | Sea, );
} for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
if(!mark[i][j] && graph[i][j] == Land)
{
ans++;
Draw(i, j, Land, );
} printf("%d\n", ans);
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

ural 1250. Sea Burial的更多相关文章

  1. Ural 1250 Sea Burial 题解

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

  2. URAL 1250 Sea Burial 简单Floodfill

    问这个人掉落的海域包含几个岛屿. 八方向相连为同一片海域,四方向相连为同一个岛屿.与边界相连的岛屿不算. 方法:在给定地图外面填充一圈".",从这个人掉落的地方开始进行floodf ...

  3. ural 1250

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

  4. URAL 1934 spfa算法

    D - Black Spot Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  5. sea.js模块化编程

    * 为什么要模块化? 解决文件依赖 解决命名冲突 ; var var2 = 2; function fn1(){ } function fn2(){ } return { fn1: fn1, fn2: ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 解决sea.js引用jQuery提示$ is not a function的问题

    在使用sea.js的如下写法引用jQuery文件时, //main.jsdefine(function(require,exports,module){ var $ = require('jquery ...

  8. 模块化开发--sea.js

    当你的网站开发越来越复杂的时候,会经常遇到一下问题吗?1.冲突2.性能3.依赖如果在多人开发或者是复杂的开发过程中会经常遇到这些问 题,就可以用模块化开发来解决.以上问题是如何产生的?1.冲突:如果你 ...

  9. sea.js 入门

    上个月学了 require.js 现在顺便来学学 sea.js. 对比下这两种的区别,看自己喜欢哪个,就在接下来的项目中去使用它吧. seajs中的所有 JavaScript 模块都遵循 CMD 模块 ...

随机推荐

  1. timestamp 类型的索引

    由这条语句datetime.strftime('2014/12/05','%Y/%m/%d')转换出来的索引 是pandas内置类型相同,如果使用datetime.strftime('2014/12/ ...

  2. hadoop+javaWeb的开发中遇到包冲突问题(java.lang.VerifyError)

    1.HDFS + WEB 项目 报java.lang.VerifyError... 异常 抛异常: Exception in thread "main" java.lang.Ver ...

  3. RST_n的问题

     有一个灰常郁闷的问题... module CLK_Generater(                    input        CLOCK_100,                    i ...

  4. 按键使用方法(二)------verilog

    这里我们要验证一键两用的情况:点击与长击,单击与双击 代码: /********************************Copyright*************************** ...

  5. MVC缓存03,扩展方法实现视图缓存

    关于缓存,先前尝试了: ● 在"MVC缓存01,使用控制器缓存或数据层缓存"中,分别在控制器和Data Access Layer实现了缓存 ● 在"MVC缓存02,使用数 ...

  6. MVC - 17.OA项目

          1.分层   2.项目依赖关系 MODEL IDAL -> MODEL DAL -> IDAL,MODEL,EntityFramewrok(注意和MODEL里的版本要一致),S ...

  7. MVC4 WEBAPI(一)使用概述

    所谓概述,也就是总结一些WEB API常用的使用用法.MVC APIWEB是一个轻量级的服务接口,完全符合RestFul框架设计,每个URL代表一种资源,使用方便,没有WCF那么庞大,但是麻雀虽小五脏 ...

  8. SQL Server 2014 BI新特性(二)结合Data Explorer和GeoFlow进行数据分析

    Data Explorer和GeoFlow作为Excel的新功能被写入到即将发布的SQL Server 2014当中.Data Explorer为业务分析人员提供了一种数据获取,整理以及组织的方式,通 ...

  9. 一致性hash算法简介与代码实现

    一.简介: 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance) 2.单调性(Monotonicity) 3.分散性(Spread) 4.负 ...

  10. 7-15ALL、 ANY、SOME子查询

    ALL:所有 ANY:部分 SOME:与ANY相同,使用ANY的地方都可以用SOME替换. >ALL:父查询中列的值必须大于子查询返回的值列表的每一个值. >ANY:父查询中的返回值必须大 ...