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(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
随机推荐
- 听VOA还不如学这些 (转自知乎恶魔奶爸)
该专栏文章网址 http://zhuanlan.zhihu.com/aisapo/19634180 鉴于知乎无法插图片和音频,所以有了这篇教程集合,大家看这个就足够了其实 每次一学英语,材料无非就是V ...
- soapdenovo
配置文件中的=号两边不能有空格,否则会报错 SOAPdenovo-63mer_v2.0 all -s TongJiN2.config -p 25 -K 63 -d 1 -R -F -o Lily_2 ...
- 读metronic文档学到的几个知识点
1.RTL 同样的页面,它做了两套.为什么,因为在这个世界上,有些民族,有些语种,是从右向左来的. 2. google material design 同样的一套东西,又分别做了google mat ...
- HTML5 3D动画效果
对以前来讲,3D动画拿到网页上展示是一件非常奢侈的事情,第一是浏览器不够先进,第二是大部分只能用flash实现伪3D.HTML5的出现,让实现网页3D动画变得非常简单,当然前提是你不要再使用像IE67 ...
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求(转载)
转自:http://www.cnblogs.com/dudu/archive/2009/11/10/1600062.html 今天下午17点左右,博客园博客站点出现这样的错误信息: Error Sum ...
- 关于freemarker标签+Spring3.0 V层学习
import标签 就是把其他的ftl页面引用进来 <#import "/common/ui.ftl" as ui> 使用时 <@ui.message/>,m ...
- 转:struts标签之select详解
<html:select>生成HTML<select>元素 <html:option>:生成HTML<option>元素 <html:option ...
- 卸载linux自带版本JDK
1)卸载系统自带的jdk版本: 查看自带的jdk: #rpm -qa|grep gcj 可能看到如下类似的信息: libgcj-4.1.2-44.el5 java-1.4 ...
- IOS中使用手机号注册
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface KCVVerify : NSObject ...
- 黑马程序员——JAVA基础之程序控制流结构之循环结构,循环嵌套
------- android培训.java培训.期待与您交流! ---------- 循环结构: 代表语句:while ,do while ,for while语句格式 : while(条件表达式) ...