【bfs】【状态压缩】胜利大逃亡(续) HDU - 1429 + Key Task HDU - 1885【有钥匙的迷宫】

【bfs】【状态压缩】胜利大逃亡(续) HDU - 1429 + Key Task HDU - 1885【有钥匙的迷宫】


胜利大逃亡(续) HDU - 1429

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

题意:
一个迷宫,有被锁起来的门(A-J),只有拿到钥匙(a - j)之后才能开门。魔王t秒检查一次,问能否在t秒内走出迷宫,最短时间。

思路:
光用bfs不行,还需要记录有没有钥匙,那么有10把钥匙和门,要怎么记录呢?状态压缩,用二进制进行记录。

如果是钥匙
1 << (mapp[t.x][t.y] - 'a'是这种钥匙状压后的数,将它和前面已经有的钥匙进行“ | ”运算,可以表示现在已有的所有钥匙。

if(mapp[t.x][t.y] >= 'a' && mapp[t.x][t.y] <= 'z')
{
    t.k = s.k | (1 << (mapp[t.x][t.y] - 'a'));
    if(vis[t.x][t.y][t.k] == 0)
    {
        vis[t.x][t.y][t.k] = 1;
        q.push(t);
    }
}

如果是钥匙
1 << (mapp[t.x][t.y] - 'A'是这种门状压后的数,将它和前面已经有的钥匙进行“ & ”运算,可以比较是否有这种门的钥匙

if(mapp[t.x][t.y] >= 'A' && mapp[t.x][t.y] <= 'Z')
{
    int kk = s.k & (1 << (mapp[t.x][t.y] - 'A'));
    if(vis[t.x][t.y][t.k] == 0 && kk)
    {
        vis[t.x][t.y][t.k] = 1;
        q.push(t);
    }
}

AC代码:

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

using namespace std;
const int maxn = 25;
int n, m, L, sx, sy, ex, ey;
char mapp[maxn][maxn];
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int vis[maxn][maxn][2000];
struct node
{
    int x, y, step, k;
    node() {};
    node(int xx, int yy, int ss, int kk): x(xx), y(yy), step(ss), k(kk) {};
};

queue<node> q;

void bfs()
{
    while(!q.empty())
        q.pop();
    memset(vis, 0, sizeof(vis));
    node st(sx, sy, 0, 0);
    vis[sx][sy][0] = 1;
    q.push(st);
    while(!q.empty())
    {
        node s = q.front(), t;
        q.pop();
        if(s.step >= L)
        {
            printf("-1\n");
            return ;
        }
        if(s.x == ex && s.y == ey)
        {
            printf("%d\n", s.step);
            return ;
        }
        for(int i = 0; i < 4; i++)
        {
            t = s;
            t.x += dx[i];
            t.y += dy[i];
            if(t.x >= 1 && t.y >= 1 && t.x <= n && t.y <= m && mapp[t.x][t.y] != '*')
            {
                t.step++;
                if(mapp[t.x][t.y] >= 'a' && mapp[t.x][t.y] <= 'z')
                {
                    t.k = s.k | (1 << (mapp[t.x][t.y] - 'a'));
                    if(vis[t.x][t.y][t.k] == 0)
                    {
                        vis[t.x][t.y][t.k] = 1;
                        q.push(t);
                    }
                }
                else if(mapp[t.x][t.y] >= 'A' && mapp[t.x][t.y] <= 'Z')
                {
                    int kk = s.k & (1 << (mapp[t.x][t.y] - 'A'));
                    if(vis[t.x][t.y][t.k] == 0 && kk)
                    {
                        vis[t.x][t.y][t.k] = 1;
                        q.push(t);
                    }
                }
                else
                {
                    if(vis[t.x][t.y][t.k] == 0)
                    {
                        vis[t.x][t.y][t.k] = 1;
                        q.push(t);
                    }
                }
            }
        }
    }
    printf("-1\n");
    return ;
}

int main()
{
    while(cin >> n >> m >> L)
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
            {
                cin >> mapp[i][j];
                if(mapp[i][j] == '@')
                {
                    sx = i;
                    sy = j;
                }
                if(mapp[i][j] == '^')
                {
                    ex = i;
                    ey = j;
                }
            }
        bfs();
    }
    return 0;
}

Key Task HDU - 1885

The Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places.

The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game.

The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color.

You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.
Input
The input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following:

这里写图片描述

Note that it is allowed to have

  • more than one exit,

  • no exit at all,

  • more doors and/or keys of the same color, and

  • keys without corresponding doors and vice versa.

You may assume that the marker of your position (“*”) will appear exactly once in every map.

There is one blank line after each map. The input is terminated by two zeros in place of the map size.

Output
For each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead.

One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.

Sample Input

1 10
*........X

1 3
*#X

3 20
####################
#XY.gBr.*.Rb.G.GG.y#
####################

0 0

Sample Output
Escape possible in 9 steps.
The poor student is trapped!
Escape possible in 45 steps.

题意:
与上面几乎一致,但是可能没有出口,或者出口不止一个。

AC代码:

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

using namespace std;
const int maxn = 105;
int n, m, sx, sy;
char mapp[maxn][maxn];
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int vis[maxn][maxn][100];
struct node
{
    int x, y, step, k;
    node() {};
    node(int xx, int yy, int ss, int kk): x(xx), y(yy), step(ss), k(kk) {};
};

queue<node> q;

void bfs()
{
    memset(vis, 0, sizeof(vis));
    node st(sx, sy, 0, 0);
    vis[sx][sy][0] = 1;
    while(!q.empty())
        q.pop();
    q.push(st);
    while(!q.empty())
    {
        node s = q.front(), t;
        q.pop();
        if(mapp[s.x][s.y] == 'X')
        {
            printf("Escape possible in %d steps.\n", s.step);
            return ;
        }
        for(int i = 0; i < 4; i++)
        {
            t = s;
            t.x += dx[i];
            t.y += dy[i];
            if(t.x >= 1 && t.y >= 1 && t.x <= n && t.y <= m && mapp[t.x][t.y] != '#')
            {
                t.step++;
                if(mapp[t.x][t.y] >= 'a' && mapp[t.x][t.y] <= 'd')
                {
                    t.k = s.k | (1 << (mapp[t.x][t.y] - 'a'));
                    if(vis[t.x][t.y][t.k] == 0)
                    {
                        vis[t.x][t.y][t.k] = 1;
                        q.push(t);
                    }
                }
                else if(mapp[t.x][t.y] >= 'A' && mapp[t.x][t.y] <= 'D')
                {
                    int kk = s.k & (1 << (mapp[t.x][t.y] - 'A'));
                    if(vis[t.x][t.y][t.k] == 0 && kk)
                    {
                        vis[t.x][t.y][t.k] = 1;
                        q.push(t);
                    }
                }
                else
                {
                    if(vis[t.x][t.y][t.k] == 0)
                    {
                        vis[t.x][t.y][t.k] = 1;
                        q.push(t);
                    }
                }
            }
        }
    }
    printf("The poor student is trapped!\n");
    return ;
}

int main()
{
    while(cin >> n >> m && n && m)
    {
        int flag = 0;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
            {
                cin >> mapp[i][j];
                if(mapp[i][j] == 'b')
                    mapp[i][j] = 'a';
                else if(mapp[i][j] == 'B')
                    mapp[i][j] = 'A';
                else if(mapp[i][j] == 'g')
                    mapp[i][j] = 'b';
                else if(mapp[i][j] == 'G')
                    mapp[i][j] = 'B';
                else if(mapp[i][j] == 'r')
                    mapp[i][j] = 'c';
                else if(mapp[i][j] == 'R')
                    mapp[i][j] = 'C';
                else if(mapp[i][j] == 'y')
                    mapp[i][j] = 'd';
                else if(mapp[i][j] == 'Y')
                    mapp[i][j] = 'D';
                if(mapp[i][j] == '*')
                {
                    sx = i;
                    sy = j;
                }
                if(mapp[i][j] == 'X')
                {
                    flag = 1;
                }

            }
        if(flag)
            bfs();
        else
            printf("The poor student is trapped!\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Floraqiu/article/details/81348286