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. Dubbox监控在服务器中的安装

    Jdk-1.6.30以上版本 Tomcat-7.0.42 Duboo-2.5.3 Zookeeper-3.4.5 端口分配 序 系统/端口 http https shutdown ajp 调度JMX ...

  2. 【题解】【数组】【Leetcode】Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  3. Debian中如何切换默认Python版本

    当你安装 Debian Linux 时,安装过程有可能同时为你提供多个可用的 Python 版本,因此系统中会存在多个 Python 的可执行二进制文件,你可以按照以下方法使用 ls 命令来查看你的系 ...

  4. HDU-4614 Vases and Flowers (线段树区间更新)

    题目大意:有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花清空.如果是第一种操作,输出 ...

  5. UVALive-4329 Ping pong (树状数组)

    题目大意:有n个数排成一列,问从中能找出几个三元组(ai,aj,ak)满足i<j<k并且这三个数严格单调. 题目分析:枚举中间的数字aj,如果aj前面有c(j)个数a(j)小,后面有d(j ...

  6. java多线程机制

    多线程使用场景 1.同时需要做两件以上事情时需要开多个线程(例如:上传功能附带进度条显示,一边做上传,一边更新进度条状态.) 2.大量同类型数据需要进行处理(导入导出大量数据) 3.office文档转 ...

  7. javascript输出图的简单路径

    <script> //图的构建 function vnode() { this.visited=0; this.vertex=0; this.arcs=new Array(); } fun ...

  8. http协议传输二进制数据以及对输入流(php://input)和http请求的理解

    1.index.php <?php $data=file_get_contents('./a.jpg'); $opts = array('http' => array( 'method' ...

  9. ID3算法 决策树的生成(2)

    # coding:utf-8 import matplotlib.pyplot as plt import numpy as np import pylab def createDataSet(): ...

  10. Unity3d 适配机型

    1,为了是更多机型能够安装你的游戏,Unity3d Device Filter设置:ARMv6 with VFP: 2,华为C8600,一运行强制停止: 参考网址:http://forum.unity ...