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. R语言学习——向量,矩阵

    在R中,基本的数据结构有:向量,矩阵,数组,数据框,列表,因子,函数等. 向量:一系列同类型的有序元素构成. 向量是一维结构. 向量是R最简单的数据结构,在R中没有标量. 标量被看成1个元素的向量. ...

  2. 什么是DSCP,如何使用DSCP标记搭配ROS策略

    一.什么是DSCP DSCP:差分服务代码点(Differentiated Services Code Point),IETF于1998年12月发布了Diff-Serv(Differentiated ...

  3. dubbo 官方参考手册~备案(防止哪天阿里一生气把dubbo给删除了)

          首页  ||  下载  ||  用户指南  ||  开发者指南  ||  管理员指南  ||  培训文档  ||  常见问题解答  ||  发布记录  ||  发展路线  ||  社区 E ...

  4. 【Codeforces】CF 2 B The least round way(dp)

    题目 传送门:QWQ 分析 求结尾0的数量QwQ. 10只能是$ 2 \times 5 $,我们预处理出每个数因子中2和5的数量. 我们接着dp出从左上到右下的经过的最少的2的数量和最少的5的数量.两 ...

  5. node使用MySQL数据库

    内容: 1.node连接数据库 2.数据库常用操作 3.数据库实例 - 用户注册.登陆 1.node连接数据库 (1)下载mysql模块 (2)使用mysql模块连接数据库 let db=mysql. ...

  6. MacBook Pro 一月使用体验

    从 2013 年开始,就特别想买 MBP,终于在 2015 年的尾巴用上了 MBPR.原本是要在使用一周后写一份使用体验的,但因为懒,现在拖到一个月了,刚好现在也是2016年的一月,就把标题改成一月使 ...

  7. Security4.1.3实现根据请求跳转不同登录页以及登录后根据权限跳转到不同页配置

    参考博客:https://blog.csdn.net/honghailiang888/article/details/53765508

  8. onMouseDown和onPress的差异AS2

    為了要做出比Button物件更複雜的互動,我們通常會改用MovieClip來製作按鈕.如此一來,就需要處理event handler.與滑鼠有關的MovieClip event handler包括on ...

  9. 书单BookList

    1. <代码大全> 史蒂夫·迈克康奈尔 (Code Complete) 2. <程序员修炼之道> Andrew Hunt [读过了,非常好的一本书] (Pragmatic Pr ...

  10. 【转载】 Java并发编程:深入剖析ThreadLocal

    原文链接:http://www.cnblogs.com/dolphin0520/p/3920407.html感谢作者的辛苦总结! Java并发编程:深入剖析ThreadLocal 想必很多朋友对Thr ...