• 胜利大逃亡(续)


    Problem Description
    Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训。把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚開始Ignatius被关在(sx,sy)的位置。离开地牢的门在(ex,ey)的位置。Ignatius每分钟仅仅能从一个坐标走到相邻四个坐标中的当中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。

    经过若干次的尝试。Ignatius已画出整个地牢的地图。如今请你帮他计算是否能再次成功逃亡。仅仅要在魔王下次视察之前走到出口就算离开地牢。假设魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。

     

    Input
    每组測试数据的第一行有三个整数n,m,t(2<=n,m<=20,t>0)。

    接下来的n行m列为地牢的地图。当中包含: . 代表路 * 代表墙 @ 代表Ignatius的起始位置 ^ 代表地牢的出口 A-J 代表带锁的门,相应的钥匙分别为a-j a-j 代表钥匙。相应的门分别为A-J 每组測试数据之间有一个空行。

     

    Output
    针对每组測试数据。假设能够成功逃亡。请输出须要多少分钟才干离开。假设不能则输出-1。

     

    Sample Input
    4 5 17 @A.B. a*.*. *..*^ c..b* 4 5 16 @A.B. a*.*. *..*^ c..b*
     

    Sample Output
    16 -1
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    using namespace std;
    int n, m, t;
    char map[30][30];
    bool f[1029][30][30];
    int dir[] = {0, 1, 0, -1, 0};
    struct node {
        int x, y;
        int temp;
        int w;
    };
    int bfs(node x)
    {
        queue<node>q;
        q.push(x);
        node a, b;
        while (!q.empty()) {
            a = q.front();q.pop();
            for (int i = 0; i < 4; i++) {
                b = a;
                b.x += dir[i];
                b.y += dir[i + 1];
                if (b.x < 0 || b.x >= n || b.y < 0 || b.y >= m)continue;
                if (map[b.x][b.y] == '*')continue;
                if (b.temp >= t)return -1;
                if (map[b.x][b.y] >= 'a' && map[b.x][b.y] <= 'z') {
                    b.w |= (1 << map[b.x][b.y] - 'a');
                }
                if (map[b.x][b.y] >= 'A' && map[b.x][b.y] <= 'Z') {
                    if (!(b.w & (1 << map[b.x][b.y] - 'A')))continue;
                }
                if (f[b.w][b.x][b.y] != 0)continue;
                b.temp++;
                f[b.w][b.x][b.y] = true;
    
                if (map[b.x][b.y] == '^') {
    
                    if (b.temp >= t)return -1;
                    return b.temp;
                }
                q.push(b);
            }
        }
        return -1;
    }
    int main()
    {
        node a;
        while (cin >> n >> m >> t) {
            memset(f, 0 , sizeof(f));
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++) {
                    cin >> map[i][j];
                    if (map[i][j] == '@') {
                        a.x = i;
                        a.y = j;
                        a.w = 0;
                        a.temp = 0;
                    }
                }
            cout << bfs(a) << endl;
        }
        return 0;
    }
    


  • 相关阅读:
    mysql网文收录
    centos7编译安装memcached
    计算机网络网文
    操作系统网文
    redis网文
    【Leetcode】746. Min Cost Climbing Stairs
    【Leetcode】198. House Robber
    【Leetcode】121. Best Time to Buy and Sell Stock
    【Leetcode】1. Two Sum
    函数的参数 2018-3-27
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6745204.html
Copyright © 2020-2023  润新知