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. Linux进程与线程概述

    进程与线程 实现方式的区别:进程是资源分配的基本单位,线程是调度的基本单位. 为什么对于大多数合作性任务,多线程比多个独立的进程更优越呢?这是因为,线程共享相同的内存空间.不同的线程可以存取内存中的同 ...

  2. 一个PHPer的规划

    前言:学PHP过时了吗?PHP开发人员如何快速成长?怎么进行职业规划?特别是近几年非常火热的人工智能,机器学习,区块链技术等等,这多少会带动一些人盲目跟风,迷茫等,下面是PHP大牛魏永强带来的一篇根据 ...

  3. IDEA编译Flume Sink通不过解决方法

    Build/Rebuild Project之后

  4. pip安装包(python安装gevent(win))

    下载: https://www.lfd.uci.edu/~gohlke/pythonlibs/#greenlet greenlet greenlet-0.4.14-cp36-cp36m-win_amd ...

  5. ORA:01745 无效的主机 绑定变量名

    原因是:mybatis中的mapping映射时,sql语句中忘了加逗号,且逗号处有换行

  6. linux7系统开机报错failed to start login service

    1.开机报错failed to start login service 参考网站:https://unix.stackexchange.com/questions/264994/kali-sudden ...

  7. packert tracer配置路由器

    配置路由器snmp: https://wenku.baidu.com/view/e73c343f0b4c2e3f57276329.html

  8. myeclipse 代码提示

    from http://fuyiyuan2011.iteye.com/blog/1258264 在软件开发过程中,有了代码提示能使开发能够更加快捷与便利.但在Eclipse ,MyEclipse等ja ...

  9. net send 换行和发送广播

    net send ip message 要换行的时候按ctrl+t.最后按enter 或 ctrl + m 发出 如果是批处理里面要用: 在命令行下使用:echo ^T > a.txt,注意这里 ...

  10. as3的全屏功能的实现主要是舞台stage的displayState属性

    StageDisplayState.NORMAL                                               正常 StageDisplayState.FULL_SCR ...