Honeycomb

http://codeforces.com/gym/102028/problem/F

time limit per test

4.0 s

memory limit per test

1024 MB

input

standard input

output

standard output

A honeycomb is a mass wax cells built by honey bees, which can be described as a regular tiling of the Euclidean plane, in which three hexagons meet at each internal vertex. The internal angle of a hexagon is 120120 degrees, so three hexagons at a point make a full 360360degrees. The following figure shows a complete honeycomb with 33 rows and 44 columns.

Here we guarantee that the first cell in the second column always locates in the bottom right side of the first cell in the first column, as shown above. A general honeycomb may, on the basis of a complete honeycomb, lose some walls between adjacent cells, but the honeycomb is still in a closed form. A possible case looks like the figure below.

Hamilton is a brave bee living in a general honeycomb. Now he wants to move from a starting point to a specified destination. The image below gives a feasible path in a 3×43×4 honeycomb from the 11-st cell in the 22-nd column to the 11-st cell in the 44-th column.

Please help him find the minimum number of cells that a feasible path has to pass through (including the starting point and the destination) from the specified starting point to the destination.

Input

The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 104104.

For each test case, the first line contains two integers rr and cc indicating the number of rows and the number of columns of the honeycomb, where 2≤r,c≤1032≤r,c≤103.

The following (4r+3)(4r+3) lines describe the whole given honeycomb, where each line contains at most (6c+3)(6c+3) characters. Odd lines contain grid vertices represented as plus signs ("+") and zero or more horizontal edges, while even lines contain two or more diagonal edges. Specifically, a cell is described as 66 vertices and at most 66 edges. Its upper boundary or lower boundary is represented as three consecutive minus signs ("-"). Each one of its diagonal edges, if exists, is a single forward slash ("/") or a single backslash ("\") character. All edge characters will be placed exactly between the corresponding vertices. At the center of the starting cell (resp. the destination), a capital "S" (resp. a capital "T") as a special character is used to indicate the special cell. All other characters will be space characters. Note that if any input line could contain trailing whitespace, that whitespace will be omitted.

We guarantee that all outermost wall exist so that the given honeycomb is closed, and exactly one "S" and one "T" appear in the given honeycomb. Besides, the sum of r⋅cr⋅c in all test cases is up to 2×1062×106.

Output

For each test case, output a line containing the minimum number of cells that Hamilton has to visit moving from the starting cell ("S") to the destination ("T"), including the starting cell and the destination. If no feasible path exists, output -1 instead.

Example
input

Copy
1
3 4
+---+ +---+
/ \ / \
+ +---+ +---+
\ \ / \
+ + S +---+ T +
/ \ / /
+ +---+ + +
\ \ / \
+---+ +---+ +
/ /
+ +---+ + +
\ / \
+---+ +---+ +
\ / \ /
+---+ +---+
output

Copy
7

比赛的时候煞笔了,没写出来。。。

 #include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
using namespace std; int n,m,fanwei;
struct sair{
int pos,step;
}; char str[][];
int book[];
vector<int> ve[]; void bfs(int ss,int tt){
sair s,e;
queue<sair>Q;
s.pos=ss,s.step=;
book[ss]=;
Q.push(s);
while(!Q.empty()){
s=Q.front();
Q.pop();
for(int i=;i<ve[s.pos].size();i++){
if(ve[s.pos][i]>=&&ve[s.pos][i]<=fanwei&&!book[ve[s.pos][i]]){
book[ve[s.pos][i]]=;
e.pos=ve[s.pos][i];
e.step=s.step+;
if(e.pos==tt){
printf("%d\n",e.step);
return;
}
Q.push(e);
}
}
}
printf("-1\n");
} void join(int x,int y){
ve[x].push_back(y);
ve[y].push_back(x);
////如果wa的话,加单向边试试??
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d %d%*c",&n,&m);
int tmp=n*m;
fanwei=tmp;
for(int i=;i<=tmp;i++){
book[i]=;
ve[i].clear();
}
for(int i=;i<n*+;i++){
gets(str[i]);
} //最后三行不用判断
int nn=n*;
int co;
for(int i=;i<nn;i++){
int len=strlen(str[i]);
int j=;
if(i%==){
j=;
co=;
while(j<len){
if(str[i][j]==' '){
join((i/-)*m+co,i/*m+co); }
co+=;
j+=;
}
}
else if(i%==){
j=;
co=;
while(j<len){
if(str[i][j]==' '){
join((i/-)*m+co-,i/*m+co);
}
j+=;
co+=;
}
j=;
co=;
while(j<len){
if(str[i][j]==' '){
join((i/-)*m+co+,i/*m+co);
}
j+=;
co+=;
}
}
else if(i%==){
co=;
j=;
while(j<len){
if(str[i][j]==' '){
join((i/-)*m+co,i/*m+co);
}
co+=;
j+=;
} }
else if(i%==){
j=;
co=;
while(j<len){
if(str[i][j]==' '){
join(i/*m+co,i/*m+co-);
}
j+=;
co+=;
}
j=;
co=;
while(j<len){
if(str[i][j]==' '){
join(i/*m+co,i/*m+co-); }
j+=;
co+=;
}
}
}
int s=-,t=-;
nn=n*;
int xxx=;
for(int i=;i<nn;i+=){
int len=strlen(str[i]);
int j=;
co=xxx*m+;
xxx++;
while(j<len){
if(str[i][j]=='S'){
s=co;
}
else if(str[i][j]=='T'){
t=co;
}
j+=;
co+=;
}
}
nn+=;
xxx=;
for(int i=;i<nn;i+=){
int len=strlen(str[i]);
int j=;
co=xxx*m+;
xxx++;
while(j<len){
if(str[i][j]=='S'){
s=co;
}
else if(str[i][j]=='T'){
t=co;
}
j+=;
co+=;
}
}
//cout<<s<<" "<<t<<endl;
bfs(s,t);
} // system("pause"); return ;
}
/*
100
3 4
+---+ +---+
/ \ / \
+ +---+ +---+
\ \ / \
+ + S +---+ T +
/ \ / /
+ +---+ + +
\ \ / \
+---+ +---+ +
/ /
+ +---+ + +
\ / \
+---+ +---+ +
\ / \ /
+---+ +---+ 3 4
+---+ +---+
/ \ / \
+ +---+ +---+
\ \ / \
+ + S +---+ +
/ \ / /
+ +---+ + +
\ \ / \
+---+ T +---+ +
/ / /
+ +---+ + +
\ / \
+---+ +---+ +
\ / \ /
+---+ +---+ 3 3
+---+ +---+
/ \ / \
+ +---+ +
\ \ /
+ + S +---+
/ \ / \
+ +---+ +
\ \ /
+---+ T +---+
/ / \
+ +---+ +
\ /
+---+ +---+
\ /
+---+
*/

Honeycomb的更多相关文章

  1. icpc2018-焦作-F Honeycomb bfs

    http://codeforces.com/gym/102028/problem/F 就是一个bfs,主要问题是建图,要注意奇数和偶数列的联通方案是略有不同的.比赛的时候写完一直不过样例最后才发现没考 ...

  2. POJ 3036 Honeycomb Walk

    http://poj.org/problem?id=3036 在每一个格子可以转移到与这个各自相邻的六个格子 那么设置转移变量 只需要六个 int d[6][2] = {-1, 0, -1, 1, 0 ...

  3. 2018ICPC焦作 F. Honeycomb /// BFS

    题目大意: 给定n m表示一共n行每行m个蜂巢 求从S到T的最短路径 input 1 3 4 +---+ +---+ / \ / \ + +---+ +---+ \ \ / \ + + S +---+ ...

  4. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  5. Android SDK 与API版本对应关系

    Android SDK版本号 与 API Level 对应关系如下表: Code name Version API level   (no code name) 1.0 API level 1   ( ...

  6. Android程序中--不能改变的事情

    有时,开发人员会对应用程序进行更改,当安装为以前版本的更新时出现令人惊讶的结果 - 快捷方式断开,小部件消失或甚至根本无法安装. 应用程序的某些部分在发布后是不可变的,您可以通过理解它们来避免意外. ...

  7. 详解Paint的setXfermode(Xfermode xfermode)

    一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...

  8. Android Xfermode 学习笔记

    一.概述 Xfermode全名transfer-mode,其作用是实现两张图叠加时的混合效果. 网上流传的关于Xfermode最出名的图来源于AndroidSDK的samples中,名叫Xfermod ...

  9. Android版本和API Level对应关系

    http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version       API ...

随机推荐

  1. ES之六:ElasticSearch中Filter和Query的异同

    如下例子,查找性别是女,所在的州是PA,过滤条件是年龄是39岁,balance大于等于10000的文档: { "query": { "bool": { &quo ...

  2. selenium java-3 定位元素的八种方法

    web driver提供了八种元素定位的方法: id name class name tag name link text partial link text xpath css selector 如 ...

  3. windows 网管常用命令

    Windows网络命令行程序 这部分包括: 使用 ipconfig /all 查看配置 使用 ipconfig /renew 刷新配置 使用 ipconfig 管理 DNS 和 DHCP 类别 ID ...

  4. mysql 字符集排查

    mysql 字符集排查 库级别 SELECT * FROM information_schema.schemata WHERE schema_name NOT IN ( 'information_sc ...

  5. 使用alter database datafile 'XXX' offline drop 是否能够恢复(非归档模式下)

    今天在群里面听到一位网友在说使用了alter database datafile 'XXX' offline drop命令是否能够恢复数据,在非归档模式下,下面是用一个实验来验证一下 ######## ...

  6. Java 中 HashMap 初始化时赋值

      1.HashMap 初始化的文艺写法 HashMap 是一种常用的数据结构,一般用来做数据字典或者 Hash 查找的容器.普通青年一般会这么初始化:HashMap<String, Strin ...

  7. mysql 分片

    MySQL Fabric(分片)  是一个用于管理 MySQL 服务器群的可扩展框架.该框架实现了两个特性 — 高可用性 (HA ) 以及使用数据分片的横向扩展.这两个特性既可以单独使用,也可以结合使 ...

  8. c#面向对象基础4

    一.namespace 命名空间 作用:解决不同类重名的问题  我们可以认为类是属于命名空间的 当我们需要再一个类中与另一个类建立关系时,通过命名空间来区别不同的类.所以需要我们这样做:导入命名空间 ...

  9. OpenACC 云水参数化方案

    ▶ 书上第十三章,用一系列步骤优化一个云水参数化方案.用于熟悉 Fortran 以及 OpenACC 在旗下的表现 ● 代码,文件较多,放在一起了 ! main.f90 PROGRAM main US ...

  10. redis——队列

    Redis消息通知系统的实现 Posted on 2012-02-29 最近忙着用Redis实现一个消息通知系统,今天大概总结了一下技术细节,其中演示代码如果没有特殊说明,使用的都是PhpRedis扩 ...