18. 地下城与勇士

Description

龙神觉得无聊于是来到了地下城,这里是一个巨大的迷宫,有一些可以通行的路、一些不可以通行的墙,还有一些怪物。虽然龙神可以轻易地干掉这些怪物,但他觉得这样就太没意思了,他观察到这些怪物每

#include <queue>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn = 105;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};//上下右左

struct node {
    int x, y, z;
    int step;
    // 初始化列表
    node(int _x, int _y,int _z, int _step) : x(_x), y(_y), z(_z), step(_step){}
};
queue<node> que;
int vis[maxn][maxn][55];
char mp[maxn][maxn];

int main() {
    int n, m, T, k;
    int start_x, start_y;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &n, &m,&k);
        for (int i = 0; i < n; ++i) {
            scanf("%s", &mp[i]);
            for (int j = 0; j < m; ++j) {
                if (mp[i][j] == 'S') {
                    start_x = i;
                    start_y = j;
                }
            }
        }

        // 又是一套初始化
        memset(vis, 0, sizeof(vis));
        que.push(node(start_x, start_y, 0, 0));//把括号内的三个作为三个参数传递进去,把一个node型数据放进队列
        vis[start_x][start_y][0] = 1;
        int ans;
        int flag = 0;
        while (!que.empty()) {
            node tmp = que.front();//tmp为取出来的待处理的队首struct node类型的元素
            que.pop();
            if (mp[tmp.x][tmp.y] == 'E') {
                ans = tmp.step;
                flag = 1;
                break;
            }

            for (int i = 0; i < 4; ++i) {
                int x = tmp.x + dx[i], y = tmp.y + dy[i];
                if((tmp.z+1)%k)//此层有怪
                {
                    if (x < 0 || x >= n || y < 0 || y >= m || mp[x][y] == '#' || mp[x][y]=='*'||vis[x][y][(tmp.z+1)%k] == 1)
                    continue;
                }
                else//此层无怪
                {
                    if (x < 0 || x >= n || y < 0 || y >= m || mp[x][y] == '#'||vis[x][y][(tmp.z+1)%k] == 1)
                    continue;
                }
                
                que.push(node(x, y, tmp.z+1, tmp.step + 1));
                vis[x][y][(tmp.z+1)%k] = 1;
            }
        }    
        if(flag)
            printf("%d\n", ans);
        else
            printf("-1\n");
        while(!que.empty())
            que.pop();
    }
    

    return 0;
}

// 7 7
// ...#...
// .....#F
// .......
// .#.#...
// .......
// .S#..#.
// .......

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值