HDU1885 Key Task

Posted songorz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1885 Key Task相关的知识,希望对你有一定的参考价值。

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.

InputThe 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.OutputFor 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.

题解:BFS+状态压缩;我们可以记录每一种状态是否出现过来搜索,因为只有4种钥匙,故用二进制表示每一种钥匙的状态;
然后就是普通的BFS,在处理时加上钥匙的处理,以及加判是否为门,如果为门的话,判断上个位置时是否已有该门对应的钥匙,
如有,则该点入队,否则跳过;如果遇到出口,就输出步数,如到最后队列为空时都没有出去,则输出被困在里面;
参考代码为:
#include<bits/stdc++.h>
using namespace std;
const int N=105;
int n,m,sx,sy,ex,ey;
char g[N][N];
bool vis[N][N][16];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node{
    int x,y,key,step;
};
int judged(char ch)
{
    if(ch==‘B‘)return 1<<0;
    if(ch==‘Y‘)return 1<<1;
    if(ch==‘R‘)return 1<<2;
    if(ch==‘G‘)return 1<<3;
}
int judgek(char ch)
{
    if(ch==‘b‘)return 1<<0;
    if(ch==‘y‘)return 1<<1;
    if(ch==‘r‘)return 1<<2;
    if(ch==‘g‘)return 1<<3;
    return 0;
}
void solve()
{
    memset(vis,0,sizeof(vis));
    queue<node>q;
    node u,v;
    u.x=sx;u.y=sy;u.key=0;u.step=0;vis[u.x][u.y][u.key]=1;
    q.push(u);
    while(!q.empty())
	{
        u=q.front(); q.pop();
        if(g[u.x][u.y]==‘X‘)
		{	
			printf("Escape possible in %d steps.
",u.step);
			return;
		}
        for(int i=0;i<4;i++)
		{
            v.x=u.x+dir[i][0];v.y=u.y+dir[i][1];v.step=u.step+1;v.key=u.key|judgek(g[v.x][v.y]);
            if(v.x<1||v.x>n || v.y<1||v.y>m)continue;
            if(g[v.x][v.y]==‘#‘ || vis[v.x][v.y][v.key])continue;
            if(g[v.x][v.y]==‘B‘||g[v.x][v.y]==‘Y‘||g[v.x][v.y]==‘R‘||g[v.x][v.y]==‘G‘)
			{
                if(!(u.key&judged(g[v.x][v.y]))) continue;
            }
            vis[v.x][v.y][v.key]=1;
            q.push(v);
        }
    }
    printf("The poor student is trapped!
");
}
int main()
{
    while(scanf("%d%d",&n,&m)&&n+m)
	{
        for(int i=1;i<=n;i++) scanf("%s",g[i]+1);
        for(int i=1;i<=n;i++)
		{
            for(int j=1;j<=m;j++)
			{
                if(g[i][j]==‘*‘)
				{
					sx=i;sy=j;g[sx][sy]=‘.‘;
					break; break;
				}
            }
        }
        solve();
    }
    return 0;
}

  






















以上是关于HDU1885 Key Task的主要内容,如果未能解决你的问题,请参考以下文章

Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题

Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题

[HDU4864]Task (贪心)

HDU 3974 Assign the task

HDU 1076 An Easy Task

hdu&&poj搜索题题号