[swustoj 1023] Escape

时间:2023-03-10 03:33:01
[swustoj 1023] Escape
Escape
Description

BH is in a maze,the maze is a matrix,he wants to escape!

Input

The input consists of multiple test cases.

For each case,the first line contains 2 integers N,M( 1 <= N, M <= 100 ).

Each of the following N lines contain M characters. Each character means a cell of the map.

Here is the definition for chracter.

For a character in the map:

'S':BH's start place,only one in the map.

'E':the goal cell,only one in the map.

'.':empty cell.

'#':obstacle cell.

'A':accelerated rune.

BH can move to 4 directions(up,down,left,right) in each step.It cost 2 seconds without accelerated rune.When he get accelerated rune,moving one step only cost 1 second.The buff lasts 5 seconds,and the time doesn't stack when you get another accelerated rune.(that means in anytime BH gets an accelerated rune,the buff time become 5 seconds).

Output

The minimum time BH get to the goal cell,if he can't,print "Please help BH!".

Sample Output

5 5
....E
.....
.....
##...
S#...
5 8
........
........
..A....A
A######.
S......E

Sample Output
Please help BH!
12

BFS、注意一下优先级判断就行了

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 110 struct Node
{
int x,y,t,r; //坐标,时间,剩余加速时间
bool operator <(const Node &T)const{
if(t!=T.t)return t>T.t;
return r<T.r;
}
}; int n,m;
int sx,sy;
int vis[N][N][];
char mpt[N][N];
int dir[][]={-,,,,,-,,}; void bfs()
{
Node now,next;
priority_queue<Node> q;
memset(vis,,sizeof(vis));
now.x=sx;
now.y=sy;
now.t=now.r=;
vis[sx][sy][]=;
q.push(now);
while(!q.empty())
{
now=q.top();
q.pop();
if(mpt[now.x][now.y]=='E'){
printf("%d\r\n",now.t);
return;
}
for(int i=;i<;i++){
next=now;
next.x+=dir[i][];
next.y+=dir[i][];
next.t+=;
if(next.r>=) {next.t--;next.r--;}
if(next.x>= && next.x<=n && next.y>= && next.y<=m && mpt[next.x][next.y]!='#'){
if(mpt[next.x][next.y]=='A') next.r=;
if(!vis[next.x][next.y][next.r]){
vis[next.x][next.y][next.r]=;
q.push(next);
}
}
}
}
printf("Please help BH!\r\n");
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf(" %c",&mpt[i][j]);
if(mpt[i][j]=='S'){
sx=i;
sy=j;
}
}
}
bfs();
}
return ;
}