2018 ICPC Asia Jiaozuo Onsite F. Honeycomb #BFS#

限于喜欢 提交于 2020-02-08 19:15:54

F. Honeycomb

time limit per test

4 seconds

memory limit per test

1024 megabytes

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 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 𝑇T indicating the number of test cases which is up to 104104.

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≤𝑟,𝑐≤1032≤r,c≤103.

The following (4𝑟+3)(4r+3) lines describe the whole given honeycomb, where each line contains at most (6𝑐+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⋅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

Solution

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e4;
const int dx[6] = { -1, -2, -1, 1, 2, 1 };
const int dy[6] = { -3, 0, 3, -3, 0, 3 };
int r, c, n, m;
char g[maxn][maxn];
struct node { p pos; int cnt; };
map<p, bool> vis;
queue<node> q;

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

bool out(p pos)
{
    if (pos.first < 0 || pos.first > n) return true;
    if (pos.second < 0 || pos.second > m) return true;
    return false;
}

int bfs(p s, p t)
{
    node src = node{ s, 1 };
    vis[s] = true;
    while (!q.empty()) q.pop();
    q.push(src);
    while (!q.empty())
    {
        node now = q.front();
        q.pop();
        if (now.pos == t) return now.cnt;
        for (int i = 0; i < 6; i++)
        {
            p bdry = p(now.pos.first + dx[i], now.pos.second + dy[i]);
            p pnxt = p(now.pos.first + dx[i] * 2, now.pos.second + dy[i] * 2);
            if (g[bdry.first][bdry.second] != ' ') continue;
            if (out(pnxt) || vis[pnxt]) continue;
            vis[pnxt] = true;
            node next = node{ pnxt, now.cnt + 1 };
            q.push(next);
        }
    }
    return -1;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    int t = read();
    while (t--)
    {
        vis.clear();
        r = read(); c = read();
        n = r * 4 + 3; m = c * 6 + 3;
        p src, des;
        for (int i = 0; i < n; i++)
        {
            fgets(g[i], m + 2, stdin);
            char* ys = strchr(g[i], 'S');
            char* yt = strchr(g[i], 'T');
            if (ys)
            {
                int j = (int)(ys - g[i]);
                src = p(i, j);
            }
            if (yt)
            {
                int j = (int)(yt - g[i]);
                des = p(i, j);
            }
        }
        write(bfs(src, des));
        putchar(10);
    }
    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!