Honeycomb (Gym-102028F)

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 degrees, so three hexagons at a point make a full 360 degrees. The following figure shows a complete honeycomb with 33 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 rr and cc 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 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×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.

Examples

Input

Output

7

题意:t 组样例,每组给出若干由字符构成的六边形单元格,总共 n 行 m 列,现在在某两个单元格中有一个 S 一个 T 分别代表起点和重点,问从 S 到 T 的最短路径

思路:

如果图不是由字符给出的,那么是一个 Dijkstra 裸题,题目的难点在于如何建图

从 1 开始,从左到右从上到下,依次分配单元格序号,共有 n*m 个单元格,然后将图读入后数格子找规律连边建图即可,具体看程序

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<LL,LL>
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 1000050+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;


struct Edge {
    int next;
    int to;
    int w;
} edge[N*6];
int head[N],tot;
struct Node {
    int id;
    int dis;
    bool operator<(const Node &rhs)const{
        return dis>rhs.dis;
    }
};
int n,m;
int st,endd;
int dis[N];
bool vis[N];
void addEdge(int x,int y,int w) {
    edge[tot].to=y;
    edge[tot].w=w;
    edge[tot].next=head[x];
    head[x]=tot++;
}
void dijkstra() {
    n=n*m;
    for(int i=0; i<=n; i++)
        dis[i]=INF;

    memset(vis,0,sizeof(vis));
    dis[st]=0;

    priority_queue<Node> Q;
    Node rec;
    rec.id=st;
    rec.dis=0;

    Q.push(rec);
    while(!Q.empty()) {
        int u=Q.top().id;
        Q.pop();
        if(vis[u])
            continue;
        vis[u]=1;
        for(int i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].to;
            int w=edge[i].w;
            if(dis[v]>dis[u]+w) {
                dis[v]=dis[u]+w;
                Node rec;
                rec.id=v;
                rec.dis=dis[v];
                Q.push(rec);
            }
        }
    }
}
char s[4100][9100];
int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        tot=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        getchar();

        int row= m>=2 ? n*4+3 : n*4+1;//字符矩阵总行数
        for(int i=1; i<=row; i++)
            gets(s[i]);

        int cnt1=-m;//i+1%4==0
        int cnt2=0;//i-1%4==0
        int cnt3=0;//i%4==0
        int cnt4=m;//else
        for(int i=3; i<=row; i++) {//从第2行开始
            if( (i+1)%4==0 ) {
                for(int j=0; j<m; j++) {
                    if(j%2==1) {
                        if(s[i][j*6+4]==' ') {
                            addEdge( j+cnt1+1, j+cnt1+m+1, 1 );
                            addEdge( j+cnt1+m+1, j+cnt1+1, 1 );
                        }
                    }
                    else {
                        if(s[i][j*6+4]=='S')
                            st=j+cnt1+m+1;
                        if(s[i][j*6+4]=='T')
                            endd=j+cnt1+m+1;
                    }
                }
                cnt1+=m;
            }
            else if( i%4==0 ) {
                for(int j=1; j<=m; j++) {
                    if(s[i][j*6+1]==' ') {
                        addEdge( cnt2+j, cnt2+j+1,1 );
                        addEdge( cnt2+j+1, cnt2+j,1 );
                    }
                }
                cnt2+=m;
            }
            else if( (i-1)%4==0 ) {
                for(int j=0; j<m; j++) {
                    if(j%2==1) {
                        if(s[i][j*6+4]=='S')
                            st=j+cnt3+1;
                        if(s[i][j*6+4]=='T')
                            endd=j+cnt3+1;
                    }
                    else {
                        if(s[i][j*6+4]==' ') {
                            addEdge( j+cnt3+1, j+cnt3+m+1, 1 );
                            addEdge( j+cnt3+m+1, j+cnt3+1, 1 );
                        }
                    }
                }
                cnt3+=m;
            }
            else {
                for(int j=1; j<=m; j++) {
                    if(s[i][j*6+1]==' ') {
                        if(j%2==1) {
                            addEdge( cnt4+j, cnt4+j-m+1,1 );
                            addEdge( cnt4+j-m+1, cnt4+j,1 );
                        }
                        else {
                            addEdge( cnt4-m+j, cnt4+j+1,1 );
                            addEdge( cnt4+j+1, cnt4-m+j,1 );
                        }
                    }
                }
                cnt4+=m;
            }
        }

        dijkstra();

        if(dis[endd]==INF)
            puts("-1");
        else
            printf("%d\n",dis[endd]+1);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值