Honeycomb
Honeycomb
http://codeforces.com/gym/102028/problem/F
4.0 s
1024 MB
standard input
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.
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.
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.
1
3 4
+---+ +---+
/ \ / \
+ +---+ +---+
\ \ / \
+ + S +---+ T +
/ \ / /
+ +---+ + +
\ \ / \
+---+ +---+ +
/ /
+ +---+ + +
\ / \
+---+ +---+ +
\ / \ /
+---+ +---+
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的更多相关文章
- icpc2018-焦作-F Honeycomb bfs
http://codeforces.com/gym/102028/problem/F 就是一个bfs,主要问题是建图,要注意奇数和偶数列的联通方案是略有不同的.比赛的时候写完一直不过样例最后才发现没考 ...
- POJ 3036 Honeycomb Walk
http://poj.org/problem?id=3036 在每一个格子可以转移到与这个各自相邻的六个格子 那么设置转移变量 只需要六个 int d[6][2] = {-1, 0, -1, 1, 0 ...
- 2018ICPC焦作 F. Honeycomb /// BFS
题目大意: 给定n m表示一共n行每行m个蜂巢 求从S到T的最短路径 input 1 3 4 +---+ +---+ / \ / \ + +---+ +---+ \ \ / \ + + S +---+ ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- Android SDK 与API版本对应关系
Android SDK版本号 与 API Level 对应关系如下表: Code name Version API level (no code name) 1.0 API level 1 ( ...
- Android程序中--不能改变的事情
有时,开发人员会对应用程序进行更改,当安装为以前版本的更新时出现令人惊讶的结果 - 快捷方式断开,小部件消失或甚至根本无法安装. 应用程序的某些部分在发布后是不可变的,您可以通过理解它们来避免意外. ...
- 详解Paint的setXfermode(Xfermode xfermode)
一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...
- Android Xfermode 学习笔记
一.概述 Xfermode全名transfer-mode,其作用是实现两张图叠加时的混合效果. 网上流传的关于Xfermode最出名的图来源于AndroidSDK的samples中,名叫Xfermod ...
- Android版本和API Level对应关系
http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version API ...
随机推荐
- js 下关于json的销毁和添加
var json={a:1,b:2} 现在给json添加个c,可以这样写 json.c=3或json["c"]=3 我删除一个属性 delete json.a alert(json ...
- 了解vue
什么是Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 状态,其实指的是实例之间的 ...
- 如何在Windows中使用netsh命令进行端口转发
自Windows XP开始,Windows中就内置网络端口转发的功能.任何传入到本地端口的TCP连接(IPv4或IPv6)都可以被重定向到另一个本地端口,或远程计算机上的端口,并且系统不需要有一个专门 ...
- rsync同步web数据
rsync远程同步web服务器的数据 实验拓扑 服务器A(rsync服务器)--------------服务器B( ...
- text-overflow:ellipsis ,溢出文本显示省略号
text-overflow:ellipsis 对溢出文本显示省略号有两个好处, 一是不用通过程序限定字数 二是有利于SEO. 需要使用对对溢出文本显示省略号的通常是文章标题列表,这样处理对搜索引擎更友 ...
- 理解无偏估计(unbiased estimation)
判断一个估计量“好坏”,至少可以从以下三个方面来考虑: 无偏估计 有效性 一致性 参考内容: 如何理解无偏估计量?https://www.matongxue.com/madocs/808.html 衡 ...
- 返回标签数据示例 (PHP)
标签接口函数 获取标签数据 array uc_tag_get(string tagname [, array nums]) 函数参数 参数 含义 string tagname 标签名称 array n ...
- ASP.NET CMS: Administration Template
ASP.NET CMS: Administration Template For many creating advanced ASP.NET website or application admin ...
- Activity服务类-5 IdentityService服务类
一.内置用户组(角色)设计表概念 用户和组(或者叫做角色),多对多关联,通过关联表实现 act_id_user 用户表: act_id_group 用户组表: act_id_membership 用户 ...
- svn更新代码时控制台出现的英文字母表示什么意思
U:表示从服务器收到文件更新了 G:表示本地文件以及服务器文件都已经更新,而且成功的合并了 A:表示有文件或者目录添加到工作目录 R:表示文件或者目录被替换了 C:表示文件的本地修改和服务器修改发生冲 ...