A typical flood-fill algorithm application (BFS). Not very complex, except only 1 tip: instead of searching for new space, I keep all spaces(occupied or not) in a hash_map that gets updated in a flood-fill.

#include <vector>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <iostream>
using namespace std; #include <ext/hash_map>
using namespace __gnu_cxx; /////////////////////////
#define gc getchar_unlocked
int read_int()
{
char c = gc();
while(c<'' || c>'') c = gc();
int ret = ;
while(c>='' && c<='') {
ret = * ret + c - ;
c = gc();
}
return ret;
}
/////////////////////////
char map[][] = {};
hash_map<int,int> hm;
void clear()
{
memset(map, , * );
}
int read_map(int x, int y) // returns ppl num
{
int ret = ;
for(int j = ; j < y; j ++)
for(int i = ; i <= x; i ++)
{
char c = gc();
if(i < x) // excluding \n
{
map[j][i] = c;
if(c == '*')
{
ret ++;
}
if(c != '#')
{
hm[j * + i] = ;
}
}
}
return ret;
} int count_room(int x, int y)
{
int ret = ; while(!hm.empty())
{
stack<int> seeds;
seeds.push(hm.begin()->first);
while(!seeds.empty())
{
int seed = seeds.top(); seeds.pop();
hm.erase(seed); int sx = seed % ;
int sy = seed / ;
map[sy][sx] = '#'; // <-
if(sx > )
{
if(map[sy][sx-] != '#') seeds.push(sy * + sx -);
}
// ->
if(sx < x)
{
if(map[sy][sx+] != '#') seeds.push(sy * + sx +);
}
// ^
if(sy > )
{
if(map[sy-][sx] != '#') seeds.push((sy - ) * + sx);
}
// v
if(sy < y)
{
if(map[sy+][sx] != '#') seeds.push((sy + ) * + sx);
}
}// inner while
ret ++;
}
return ret;
}
/////////////////////////
int main()
{
int runcnt = read_int();
while(runcnt--)
{
clear(); int y = read_int();
int x = read_int(); int ppl = read_map(x, y);
int rcnt = count_room(x, y);
printf("%.2f\n", ppl*1.0/rcnt);
} return ;
}

SPOJ #691. Hotel Floors的更多相关文章

  1. Codeforces Round #336 (Div. 2) A. Saitama Destroys Hotel 模拟

    A. Saitama Destroys Hotel   Saitama accidentally destroyed a hotel again. To repay the hotel company ...

  2. Codeforces Round #336 (Div. 2)A. Saitama Destroys Hotel 水题

    A. Saitama Destroys Hotel 题目连接: http://www.codeforces.com/contest/608/problem/A Description Saitama ...

  3. Codeforces 608 A. Saitama Destroys Hotel

      A. Saitama Destroys Hotel   time limit per test 1 second memory limit per test 256 megabytes input ...

  4. CodeForces - 608A-Saitama Destroys Hotel(模拟)

    Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to o ...

  5. Codefroces A. Saitama Destroys Hotel

    A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabytes input sta ...

  6. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  7. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  8. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  9. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

随机推荐

  1. js 中对字符串操作的函数

    concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. indexOf() – 返回字符串中一个子串第一处出现的索引.如果没有匹配项,返回 -1 . charAt() – 返回指定 ...

  2. java实现读取文件大全

    1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件 ...

  3. typeof、offsetof、container_of的解释

    链表是内核最经典的数据结构之一,说到链表就不得不提及内核最经典(没有之一)的宏container_of. container_of似乎就是为链表而生的,它的主要作用是根据一个结构体变量中的一个域成员变 ...

  4. Android内存管理机制之一:low memory killer

    转载自http://www.miui.com/thread-29268-1-1.html 准备写这个专题之前,心里是有点忐忑的.首先Android内存管理机制相当复杂,想要讲清楚比较困难:其次对于绝大 ...

  5. C++ STL算法系列 unique

    类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值 ...

  6. tyvj 1402 dp

    P1402 [NOIP2010]乌龟棋 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2010提高组复赛第二题 描述 小明过生日的时候,爸爸送给他一 ...

  7. c 函数及指针学习 8

    联合体 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h>   union sa     {     double a;     int b; ...

  8. leetcode 122. Best Time to Buy and Sell Stock II ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. HDU5889 Barricade(最短路)(网络流)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  10. POJ2391 Ombrophobic Bovines(网络流)(拆点)

                         Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...