2018焦作 Gym - 102028F - 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 120120 degrees, so three hexagons at a point make a full 360360 degrees. 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 66edges. 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

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

Output

7

 

 

分析:按照题意BFS一下即可。

#include "bits/stdc++.h"
using  namespace std;
typedef long long ll;
const long long mod = 998244353;
const int maxN = 2e6 + 7;
const int dir[6][2] =
        {
                -2, 0,
                -1, 3,
                1, 3,
                2, 0,
                1, -3,
                -1, -3
        };
const int fir[6][2] =
        {
                -4, 0,
                -2, 6,
                2, 6,
                4, 0,
                2, -6,
                -2, -6
        };
int N, M;
char mp[4050][6050];
bool vis[maxN];
struct node
{
    int x, y, t;
    node(int a=0, int b=0, int c=0):x(a), y(b), t(c) {}
};
queue<node> Q;
inline int _id(int x, int y)
{
    if( (x + 1)%4 == 0 ) return ((int)(x/4)) * M + y/12 + 1;
    else return ((int)(x/4) - 1) * M + (M + 1)/2 + y/12 + 1;
}
bool In_Map(int x, int y) { return x > 0 && y > 0  && x <= 4 * N + 3 && y <= 6 * M + 3; }
int bfs(int x, int y)
{
    Q.push(node(x, y, 1));
    vis[_id(x, y)] = true;
    while(!Q.empty())
    {
        node tmp = Q.front();   Q.pop();
        x = tmp.x; y = tmp.y;
        for(int i=0; i<6; i++)
        {
            int xx = x + dir[i][0], yy = y + dir[i][1];
            int nx = x + fir[i][0], ny = y + fir[i][1];
            if(In_Map(xx, yy) && In_Map(nx, ny) && mp[xx][yy] == ' ' && !vis[_id(nx, ny)])
            {
                if(mp[nx][ny] == 'T') return tmp.t + 1;
                vis[_id(nx, ny)] = true;
                Q.push(node(nx, ny, tmp.t + 1));
            }
        }
    }
    return -1;
}
inline void init()
{
    for(int i=1; i<=N * M; i++) vis[i] = false;
    while(!Q.empty()) Q.pop();
}
int main()
{
    int T;  scanf("%d", &T);
    int sx, sy;
    while(T--)
    {
        scanf("%d%d ", &N, &M);
        init();
        for(int i=1, len; i<=4 * N + 3; i++)
        {
            //scanf("%s", mp[i] + 1);
            gets(mp[i] + 1);
            len = strlen(mp[i] + 1);
            if( (i + 1) % 4 == 0 )
            {
                for(int j = 5; j < len; j += 12)
                {
                    if(mp[i][j] == 'S')
                    {
                        sx = i;
                        sy = j;
                    }
                }
            }
            else if(i & 1)
            {
                for(int j=11; j < len; j += 12)
                {
                    if(mp[i][j] == 'S')
                    {
                        sx = i;
                        sy = j;
                    }
                }
            }
        }
        printf("%d\n", bfs(sx, sy));
    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值