CodeForces - [ACM-ICPC Jiaozuo Onsite D]Honeycomb(BFS)

题目链接https://codeforces.com/gym/102028/problem/F
Time limit: 4.0 s Memory limit: 1024 MB

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

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

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

Example

Input

1
3 4

  +---+       +---+
 /     \     /     \
+       +---+       +---+
 \           \     /     \
  +   +   S   +---+   T   +
 /     \     /           /
+       +---+       +   +
 \           \     /     \
  +---+       +---+       +
 /                       /
+       +---+       +   +
 \                 /     \
  +---+       +---+       +
       \     /     \     /
        +---+       +---+

Output

7

Problem solving report:

Description:t组样例,每组给出若干由字符构成的六边形单元格,总共n行m列,现在在某两个单元格中有一个S一个T分别代表起点和重点,问从S到T的最短路径。
Problem solving:如果图不是由六边形给出的,那么是一个BFS裸题,题目的难点在于如何建图。我们可以向六边形的六个方向搜,判断该方向是否可以通过,如果可以就进入六边形的中心。就是在搜的方向上和走的步数有所改变,其它的和普通的广搜没啥区别。注意,不要用memset,会TLE的!!!

Accepted Code:

/* 
 * @Author: lzyws739307453 
 * @Language: C++ 
 */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e3 + 10, MAXM = 6e3 + 10;
const int dir[6][2] = {{2, 0}, {-2, 0}, {1, 3}, {1, -3}, {-1, 3}, {-1, -3}};
char str[MAXN][MAXM];
bool vis[MAXN][MAXM];
struct edge {
    int x, y, t;
    edge() {}
    edge(int x, int y, int t) : x(x), y(y), t(t) {}
};
int r, c;
bool Judge(int x, int y) {
    if (x >= 0 && x < r && y >= 0 && y < c)
        return true;
    return false;
}
int BFS(int x, int y) {
    queue <edge> Q;
    vis[x][y] = true;
    Q.push(edge(x, y, 1));
    while (!Q.empty()) {
        edge p = Q.front();
        Q.pop();
        if (str[p.x][p.y] == 'T')
            return p.t;
        for (int i = 0; i < 6; i++) {
            int tx = p.x + dir[i][0];
            int ty = p.y + dir[i][1];
            if (str[tx][ty] == ' ' && !vis[tx + dir[i][0]][ty + dir[i][1]] && Judge(tx, ty)) {
                Q.push(edge(tx + dir[i][0], ty + dir[i][1], p.t + 1));
                vis[tx + dir[i][0]][ty + dir[i][1]] = true;
            }
        }
    }
    return -1;
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int sx, sy;
        scanf("%d%d", &r, &c);
        getchar();
        r = r * 4 + 3;
        c = c * 6 + 3;
        for (int i = 0; i < r; i++) {
            scanf("%[^\n]", str[i]);
            getchar();
            for (int j = 0; str[i][j]; j++) {
                if (str[i][j] == 'S')
                    sx = i, sy = j;
            }
        }
        printf("%d\n", BFS(sx, sy));
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                vis[i][j] = false;
    }
    return 0;
}
发布了808 篇原创文章 · 获赞 331 · 访问量 244万+

猜你喜欢

转载自blog.csdn.net/lzyws739307453/article/details/102312105