Honeycomb

5 篇文章 0 订阅
2 篇文章 0 订阅

Description

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 \times 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 $$$10^4$$$.

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 \leq r, c \leq 10^3$$$.

The following $$$(4 r + 3)$$$ lines describe the whole given honeycomb, where each line contains at most $$$(6 c + 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 \cdot c$$$ in all test cases is up to $$$2 \times 10^6$$$.

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.

Sample Input

Input

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

Output

7

题意:n行m列的蜂巢,其中有起点s,终点t,问从s到t最少经过几个蜂巢

思路:直接在给的图上bfs就好了

#include<stdio.h>
#include <bits/stdc++.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int n,m;char s[8015][11015];
struct node
{
    int x,y,step;
}st,ed;queue<node>q;
int vis[8000][11000];
int bfs(node st)
{
    while(!q.empty()) q.pop();

    st.step=1;
    q.push(st);
    while(!q.empty())
    {
        node w,no=q.front();
        w.step=no.step+1;
        q.pop();
        if(vis[no.x][no.y]==1) continue;
        vis[no.x][no.y]=1;
        if(s[no.x][no.y]=='T')
        {
            return no.step;
        }
        if(s[no.x+2][no.y]==' ')
        {
            s[no.x+2][no.y]=='.';
            w.x=no.x+4;w.y=no.y;
            q.push(w);
        }
        if(s[no.x-2][no.y]==' ')
        {
            s[no.x-2][no.y]=='.';
            w.x=no.x-4;w.y=no.y;
            q.push(w);
        }
        if(s[no.x-1][no.y-3]==' ')
        {
            s[no.x-1][no.y-3]=='.';
            w.x=no.x-2;w.y=no.y-6;
            q.push(w);
        }
        if(s[no.x+1][no.y+3]==' ')
        {
            s[no.x+1][no.y+3]=='.';
            w.x=no.x+2;w.y=no.y+6;
            q.push(w);
        }
        if(s[no.x-1][no.y+3]==' ')
        {
            s[no.x-1][no.y+3]=='.';
            w.x=no.x-2;w.y=no.y+6;
            q.push(w);
        }
        if(s[no.x+1][no.y-3]==' ')
        {
            s[no.x+1][no.y-3]=='.';
            w.x=no.x+2;w.y=no.y-6;
            q.push(w);
        }
    }
    return -1;
}
int main()
{
   int t;
   scanf("%d",&t);
   while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=5*n+10;i++)
        {
            for(int j=1;j<=10*m+10;j++)
            {
                s[i][j]='\0';
                vis[i][j]=0;
            }
        }
        getchar();
        int c = n*4+3;
        for(int i = 1; i <= c; i++)
        gets(s[i]+1);
        for(int i=1;i<=c;i++)
        {
            for(int j=1;s[i][j]!='\0';j++)
            {
                if(s[i][j]=='S')
                {
                    st.x=i;st.y=j;
                }
                if(s[i][j]=='T')
                {
                    ed.x=i;ed.y=j;
                }
            }
        }
        printf("%d\n",bfs(st));
   }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值