Honeycomb(bfs)

题目

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 120 degrees, so three hexagons at a point make a full 360 degrees. The following figure shows a complete honeycomb with 3 rows and 4 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×4 honeycomb from the 1-st cell in the 2-nd column to the 1-st cell in the 4-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 T indicating the number of test cases which is up to 104.

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

The following (4r+3) lines describe the whole given honeycomb, where each line contains at most (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 6 vertices and at most 6 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⋅c in all test cases is up to 2×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:

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

output:
7

一个广搜的题,问你从S到T最短路,找不到就-1
一开始一直在思考怎么把六边形转成以前做的那种类型,后来才发现…直接做就好了,判断6个方向是否是空白,如果是,就是通的,那么在往相同的方向走一步就好了

代码

#include 
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e4+5;
struct pe{int x,y,z;
};char mp[maxn][maxn];
int vis[maxn][maxn];
int dx[6]={1,1,-1,-1,2,-2};
int dy[6]={3,-3,3,-3,0,0};
int n,m;
int sx,sy,ex,ey;
int bfs(int x,int y){pe a={x,y,1};vis[x][y]=0;queue w;w.push(a);while(!w.empty()){pe t=w.front();w.pop();if(t.x==ex&&t.y==ey) return t.z;for(int i=0;i<6;i++){int xx=t.x+dx[i];int yy=t.y+dy[i];if(xx>=0&&xx=0&&yy=0&&xx=0&&yyt.z+1){vis[xx][yy]=t.z+1;pe tt={xx,yy,t.z+1};w.push(tt);}}}}return -1;
}
int main() {int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);getchar();n=4*n+3;m=6*m+3;for(int i=0;i


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部