[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][8];

char mpt[N][N];

int dir[4][2]={-1,0,0,1,0,-1,1,0};



void bfs()

{

    Node now,next;

    priority_queue<Node> q;

    memset(vis,0,sizeof(vis));

    now.x=sx;

    now.y=sy;

    now.t=now.r=0;

    vis[sx][sy][0]=1;

    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=0;i<4;i++){

            next=now;

            next.x+=dir[i][0];

            next.y+=dir[i][1];

            next.t+=2;

            if(next.r>=1) {next.t--;next.r--;}

            if(next.x>=1 && next.x<=n && next.y>=1 && next.y<=m && mpt[next.x][next.y]!='#'){

                if(mpt[next.x][next.y]=='A') next.r=5;

                if(!vis[next.x][next.y][next.r]){

                    vis[next.x][next.y][next.r]=1;

                    q.push(next);

                }

            }

        }

    }

    printf("Please help BH!\r\n");

}

int main()

{

    while(scanf("%d%d",&n,&m)!=EOF)

    {

        for(int i=1;i<=n;i++){

            for(int j=1;j<=m;j++){

                scanf(" %c",&mpt[i][j]);

                if(mpt[i][j]=='S'){

                    sx=i;

                    sy=j;

                }

            }

        }

        bfs();

    }

    return 0;

}

 

你可能感兴趣的:(escape)