2018 焦作 现场(异型bfs)

题目描述:

F. Honeycomb

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 3rows 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 to10^4.

For each test case, the first line contains two integers rand c indicating the number of rows and the number of columns of the honeycomb, where2 \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 6vertices 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 cin 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.

Example

input

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

output

7

题意:

      给出一个蜂巢图,问起点到终点的最少经过几个点。

思路:

      显然难点在建图上,这种图点较多最好还是用邻接表。

我们可以定位 r 行 c 列到原图对应的中点坐标,以此来建图。

邻接表存储的是1~r*c个点。

      建图时的坐标转换函数写了好久。

     大佬直接封装了一个图,学习了一下,挺好用的。另外,

这种坐标变换(包括一维二维转换)还是用起始下标为零的

方式更好。

代码实现:

#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <queue>
#include <algorithm>

#define LL long long
#define inf 0x3f3f3f3f
#define ull unsigned long long

using namespace std;

const int N = 2e6 + 100;
const int M = 6*2e6 + 100;

struct graph {

    int rn,cn;
    int head[N],Next[M],ver[M],tot;
    int dis[N];
    queue<int>qc;

    void clear(int r,int c) {
        tot=0;
        rn=r;
        cn=c;
        for(int i=0; i<=r*c; i++)
            head[i]=Next[i]=dis[i]=0;
    }
    void add(int x,int y) {
        ver[++tot]=y;
        Next[tot]=head[x];
        head[x]=tot;

        ver[++tot]=x;
        Next[tot]=head[y];
        head[y]=tot;
    }
    int bfs(int sx,int ex) {
        while(qc.size())qc.pop();
        dis[sx]=1;
        qc.push(sx);
        while(qc.size()) {
            int x=qc.front();
            qc.pop();
            for(int i=head[x]; i; i=Next[i]) {
                int y=ver[i];
                if(dis[y]||!(y>=1&&y<=rn*cn))continue;
                dis[y]=dis[x]+1;
                if(y==ex)return dis[y];
                qc.push(y);
            }
        }
        return -1;
    }
} g;
char mp[4024][6024];

void zb(int r,int c,int &x,int &y) {
    if(!(c&1))x=r*4+2,y=4+c*6;
    else x=r*4+4,y=6*c+4;
}

int r,c,x,y;
int id(int x,int y) {
    return x*c+y+1;
}

int main() {
#ifdef MYHOME_Wjvje
    freopen("input.txt","r",stdin);
#endif
//cout<<sizeof(mp)+sizeof(g)<<endl;
    int t,ans,sx,ex;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&r,&c);
        g.clear(r,c);
        getchar();
        for(int i=0; i<4*r+3; i++)gets(mp[i]);
        for(int i=0; i<r; i++) {
            for(int j=0; j<c; j++) {
                zb(i,j,x,y);
                if(!(x>0&&(x<4*r+3)&&(y<6*c+3)&&y>0))continue;
                if(mp[x][y]=='S')sx=id(i,j);
                if(mp[x][y]=='T')ex=id(i,j);
                if(x>1&&i>0&&mp[x-2][y]==' ') {
                    g.add(id(i,j),id(i-1,j));
                }
                if(x>0&&y>2&&j>0&&mp[x-1][y-3]==' ') {
                    if(j&1)g.add(id(i,j),id(i,j-1));
                    else if(i>0)g.add(id(i,j),id(i-1,j-1));
                }
                if(y<6*c&&x>0&&j+1<c&&mp[x-1][y+3]==' ') {
                    if(j&1)g.add(id(i,j),id(i,j+1));
                    else if(i>0)g.add(id(i,j),id(i-1,j+1));
                }
            }
        }
        ans=g.bfs(sx,ex);
        printf("%d\n",ans);
    }
    return 0;

}

THE END;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值