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. 1009 Product of Polynomials (25 分)

    1009 Product of Polynomials (25 分) This time, you are supposed to find A×B where A and B are two pol ...

  2. Appium+python自动化8-Appium Python API

    Appium+python自动化8-AppiumPython API   前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts conte ...

  3. [UE4]让AI跑起来

    让AI由静止状态变成跑步状态,做法跟玩家角色走路一样. 一.创建1D混合动画 二.在AI角色关联的动画蓝图中使用第一步创建的混合动画

  4. select count(*) as total from(select count(*) from tab_cb_casim group by `card_no`) as cai;

    子查询必须加一个别名才能执行!!

  5. 学习git最好的方式

    1:登陆git官网网站 https://git-scm.com 2:点击esay to learn连接 3:点击Book连接 4:选择简体中文,下载PDF文档,也可以在线学习.

  6. TensorFlow函数:tf.FIFOQueue队列

    转载:https://blog.csdn.net/akadiao/article/details/78552037 tf.FIFOQueue tf.FIFOQueue继承基类QueueBase. Qu ...

  7. 解析Excel数据

    解析Excel数据常用的方式就是使用POI和JXL工具了,这两种工具使用起来有些类似,这里记录一下使用方式提供个参考 POI使用 File file = new File(filePath); Fil ...

  8. iOS 基础

    layoutSubviews: layoutSubviews是对sbuviews的重新布局,比如,我们想更新子视图的位置,可以通过调用layoutSubviews方法(不能直接调用) layoutSu ...

  9. ECharts之饼图和柱形图demo

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  10. spring boot + jpa + kotlin入门实例

    spring boot +jpa的文章网络上已经有不少,这里主要补充一下用kotlin来做. kotlin里面的data class来创建entity可以帮助我们减少不少的代码,比如现在这个User的 ...