SPOJ #691. Hotel Floors
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的更多相关文章
- Codeforces Round #336 (Div. 2) A. Saitama Destroys Hotel 模拟
A. Saitama Destroys Hotel Saitama accidentally destroyed a hotel again. To repay the hotel company ...
- Codeforces Round #336 (Div. 2)A. Saitama Destroys Hotel 水题
A. Saitama Destroys Hotel 题目连接: http://www.codeforces.com/contest/608/problem/A Description Saitama ...
- Codeforces 608 A. Saitama Destroys Hotel
A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabytes input ...
- CodeForces - 608A-Saitama Destroys Hotel(模拟)
Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to o ...
- Codefroces A. Saitama Destroys Hotel
A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- 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 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
随机推荐
- java 中的几种 "通用方法“
前言 Java中,除了基本的数值类型,其他所有数据类型(包括数组)都是对象. 而Object这个类是所有类的超类,它提供的方法,自然能够使用于它的所有子类(所有非基本数值类型). 本文介绍了Objec ...
- 图像处理之image stitching
背景介绍 图像拼接是一项应用广泛的图像处理技术.根据特征点的相互匹配,可以将多张小视角的图像拼接成为一张大视角的图像,在广角照片合成.卫星照片处理.医学图像处理等领域都有应用.早期的图像拼接主要是运用 ...
- System program problem detected 解决
每次开机都出现:System program problem detected 管理员权限打开:/etc/default/apport su root vim /etc/default/app ...
- 第3章 rpm命令管理
3-1 RPM包命名规则 3-2 安装命令 3-3 升级与卸载 3-4 RPM包查询 3-5 RPM包校验
- 写出bool,int,float,指针与零值比较的if语句
这个里面float与零值的比较颇有些意思. bool: bool flag; if (flag == true) return; int: int var; if (var == 0) { retur ...
- kuangbin_ShortPath J (POJ 1511)
其实虽然一开始有被这个题的8000MS 和 256MB限制又被吓到 但是严格来说跟之前的POJ 3268是一样的做法只是数据大了点 但是问题就出在数据大了点上 其实严格来说也不大 1e6 数组加起来大 ...
- AWR分析。(shared_pool,sga_size大小设置)
Execute to Parse 指标反映了执行解析比 其公式为 1-(parse/execute) , 目标为100% 及接近于只 执行而不解析 在oracle中解析往往是执行的先提工作,但是通过游 ...
- 套接字I/O模型-select
共有6种类型套接字I/O模型.blocking(阻塞),select(选择),WSAAsyncSelect(异步选择),WSAEventSelect(事件选择),overlapped(重叠),comp ...
- linux如何查看磁盘剩余空间
[root@Linux var]# df -hl 文件系统 容量 已用 可用 已用% 挂载点 /dev/hdb2 75G 75G 0 100% / /dev/hdb1 99M 9.2M 85M 10% ...
- oracle更新统计信息以及解锁统计信息
begin dbms_stats.unlock_table_stats(ownname => 'DM_MPAY', tabname => 'PLAT_INFO');end; begin d ...