3303——鬼吹灯之牧野诡事(图的遍历)

鬼吹灯之牧野诡事

			Time Limit: 1000 ms
			Memory Limit: 65536 KiB
							
			
				Submit
				Statistic
			
			Problem Description
			
			蓝色空间号和万有引力号进入了四维水洼,发现了四维物体--魔戒。

这里我们把飞船和魔戒都抽象为四维空间中的一个点,分别标为 “S” 和 “E”。空间中可能存在障碍物,标为 “#”,其他为可以通过的位置。

现在他们想要尽快到达魔戒进行探索,你能帮他们算出最小时间是最少吗?我们认为飞船每秒只能沿某个坐标轴方向移动一个单位,且不能越出四维空间。

			Input
			
			输入数据有多组(数据组数不超过 30),到 EOF 结束。

每组输入 4 个数 x, y, z, w 代表四维空间的尺寸(1 <= x, y, z, w <= 30)。

接下来的空间地图输入按照 x, y, z, w 轴的顺序依次给出,你只要按照下面的坐标关系循环读入即可。

for 0, x-1

for 0, y-1

    for 0, z-1

        for 0, w-1

保证 “S” 和 “E” 唯一。

			Output
			
			对于每组数据,输出一行,到达魔戒所需的最短时间。

如果无法到达,输出 “WTF”(不包括引号)。

			Sample Input
			
				2 2 2 2


.S

#.
#.
.E
.#

2 2 2 2

.S
#.

E.
.#
#.

			Sample Output
			
				1

3

#include <bits/stdc++.h>
using namespace std;
char ma[40][40][40][40];
struct node
{
    int x, y, z, w;
    int step;
}que[20000221], p, q;
int v[40][40][40][40];
int x, y, z, w;
int xx, yy, zz, ww;
int xa, ya, za, wa;
int qq[8][4] = {0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0};
void bfs()
{
    memset(v, 0, sizeof(v));
    q.x = xa;
    q.y = ya;
    q.z = za;
    q.w = wa;
    q.step = 0;
    v[xa][ya][za][wa] = 1;
    int front = 0, rear = 0;
    que[rear++] = q;
    while(front<rear)
    {
        p = que[front++];
        if(p.x==xx&&p.y==yy&&p.z==zz&&p.w==ww)
        {
            cout<<p.step<<endl;
            return ;
        }
        for(int i=0;i<8;i++)
        {
            int xi = p.x + qq[i][0];
            int yi = p.y + qq[i][1];
            int zi = p.z + qq[i][2];
            int wi = p.w + qq[i][3];
            if(xi>=0&&xi<x&&yi>=0&&yi<y&&zi>=0&&zi<z&&wi>=0&&wi<w&&v[xi][yi][zi][wi]==0&&ma[xi][yi][zi][wi]!='#')
            {
                q.x = xi;
                q.y = yi;
                q.z = zi;
                q.w = wi;
                q.step = p.step + 1;
                v[xi][yi][zi][wi] = 1;
                que[rear++] = q;
            }
        }
    }
    cout<<"WTF"<<endl;
}
int main()
{
    while(cin>>x>>y>>z>>w)
    {
        for(int i=0;i<x;i++)
        {
            for(int j=0;j<y;j++)
            {
                for(int k=0;k<z;k++)
                {
                scanf("%s", ma[i][j][k]);
                for(int l=0;l<w;l++)
                {
                    if(ma[i][j][k][l]=='S')
                    {
                        xa = i;
                        ya = j;
                        za = k;
                        wa = l;
                    }
                    else if(ma[i][j][k][l]=='E')
                    {
                        xx = i;
                        yy = j;
                        zz = k;
                        ww = l;
                    }
                }
                }
            }
        }
        bfs();
    }
    return 0;
    }
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N = 33;
typedef struct node
{
    int i, j, l, r, step;
} Node;
char str[N][N][N][N];
bool map[N][N][N][N];
int x, y, z, w;
int xx[8] = {0,0,0,1,0,0,0,-1};
int yy[8] = {0,0,1,0,0,0,-1,0};
int zz[8] = {0,1,0,0,0,-1,0,0};
int ww[8] = {1,0,0,0,-1,0,0,0};
void BFS(int a, int b, int c, int d)
{
    map[a][b][c][d] = 1;
    queue<Node> q;
    Node t, p;
    t.i = a;
    t.j = b;
    t.l = c;
    t.r = d;
    t.step = 0;
    q.push(t);
    while(!q.empty())
    {
        t = q.front();
        q.pop();
        if(str[t.i][t.j][t.l][t.r] == 'E')
        {
            printf("%d\n", t.step);
            return;
        }
        for(int k = 0; k < 8; k++)
        {
            p.i = t.i + xx[k];
            p.j = t.j + yy[k];
            p.l = t.l + zz[k];
            p.r = t.r + ww[k];
            if(p.i >= 0 && p.j >= 0 && p.l >= 0 && p.r >= 0 && p.i < x && p.j < y && p.l < z && p.r < w)
            {
                if(str[p.i][p.j][p.l][p.r] != '#' && !map[p.i][p.j][p.l][p.r])
                {
                    p.step = t.step + 1;
                    q.push(p);
                    map[p.i][p.j][p.l][p.r] = 1;
                }
            }
        }
    }
    printf("WTF\n");
}
int main()
{
    while(~scanf("%d%d%d%d", &x, &y, &z, &w))
    {
        int a, b, c, d;
        memset(map, 0, sizeof(map));
        for(int i = 0; i < x; i++)
        {
            for(int j = 0; j < y; j++)
            {
                for(int l = 0; l < z; l++)
                {
                    scanf("%s", str[i][j][l]);
                    for(int r = 0; r < w; r++)
                    {
                        if(str[i][j][l][r] == 'S')
                        {
                            a = i;
                            b = j;
                            c = l;
                            d = r;
                            break;
                        }
                    }
                }
            }
        }
        BFS(a, b, c, d);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值