2018-2019 ACM-ICPC, Asia Jiaozuo Regional Contest F. Honeycomb

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lingzidong/article/details/84900167

简单BFS,看起来有点不好写,但是我们仔细观察,会发现,只要先走一步,判断门是不是开着,之后再朝这个方向走一步,判断走过没有,就完了。
于是,判断走过没有的时候,用了二维数组,既然是多组样例,就要memset是吧
然而,我开的是6000*6000的数组,咕算一下memset的时间复杂度。就算是我不用memset,给这么大一个SX数组清空,也要很长的时间。
所以以后碰到这样大数组,还是要注意。
虽然注意也没有意义了……

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int dx[6] = {1,1,-1,-1,2,-2};
int dy[6] = {3,-3,3,-3,0,0};
const int MAXN = 6300;
char mp[MAXN][MAXN];
bool vis[MAXN][MAXN];
int tx,ty,sx,sy;
int n,m;
int r,c;
struct node{int x,y,s;};
bool check(int x,int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}
void bfs()
{

    vis[sx][sy] = 1;
    node a;
    a.x = sx,a.y = sy,a.s = 1;
    queue<node> q;
    q.push(a);
    while(!q.empty())
    {
        node tmp = q.front();
        q.pop();
        if(tmp.x == tx && tmp.y == ty)
        {
            printf("%d\n",tmp.s);
            return ;
        }
        for(int i = 0;i<6;i++)
        {
            int xx = tmp.x + dx[i];
            int yy = tmp.y + dy[i];
            if(check(xx,yy) && mp[xx][yy] == ' ')
            {
                int xxx = xx + dx[i],yyy = yy + dy[i];
                if(check(xxx,yyy) && !vis[xxx][yyy])
                {
                    vis[xxx][yyy] = 1;
                    a.x = xxx,a.y = yyy,a.s = tmp.s +1;
                    q.push(a);
                }
            }
        }
    }
    printf("-1\n");
    return;
}
int main()
{
    //freopen("input.txt","r",stdin);
    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%d%d",&r,&c);
        n = 4*r+3,m =6*c+3;
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=m;j++)
            {
                vis[i][j] = 0;
            }
        }
        getchar();
        for(int i = 1;i<=n;i++)
        {
            fgets(mp[i],MAXN,stdin);
        }
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=m;j++)
            {
                if(mp[i][j] == 'S')
                {
                    sx = i,sy =j;
                }
                if(mp[i][j] == 'T')
                {
                    tx = i,ty = j;
                }
            }
        }
        bfs();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lingzidong/article/details/84900167