您的位置:首页 > 其它

2014 ACM/ICPC Asia Regional Beijing Online(hdu5040)优先队列BFS

2015-08-16 23:41 411 查看

Instrusive

Problem Description

The legendary mercenary Solid Matt gets a classic mission: infiltrate a military base.

The military base can be seen as an N * N grid. Matt’s target is in one of the grids and Matt is now in another grid.

In normal case, Matt can move from a grid to one of the four neighbor grids in a second. But this mission is not easy.

Around the military base there are fences, Matt can’t get out of the base.

There are some grids filled with obstacles and Matt can’t move into these grids.

There are also some surveillance cameras in the grids. Every camera is facing one of the four direction at first, but for every second, they will rotate 90 degree clockwisely. Every camera’s sight range is 2, which means that if Matt is in the same grid as the camera, or in the grid that the camera is facing, he will be seen immediately and the mission will fail.

Matt has a special equipment to sneak: a cardbox. Matt can hide himself in the card box and move without being noticed. But In this situation, Matt will have to use 3 seconds to move 1 grid. Matt can also just hide in the cardbox without moving. The time to hide and the time to get out of the cardbox can be ignored.

Matt can’t take the risk of being noticed, so he can’t move without cardbox into a grid which is now insight of cameras or from a grid which is now insight of cameras. What’s more, Matt may be in the cardbox at the beginning.

As a live legend, Matt wants to complete the mission in the shortest time.

Input

The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.

For each test cases, the first line contains one integer:N(1<=N<=500)

In the following N lines, each line contains N characters, indicating the grids.

There will be the following characters:

● ‘.’ for empty

● ‘#’ for obstacle

● ‘N’ for camera facing north

● ‘W’ for camera facing west

● ‘S’ for camera facing south

● ‘E’ for camera facing east

● ‘T’ for target

● ‘M’ for Matt

Output

For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the answer.

If Matt cannot complete the mission, output ‘-1’.

Sample Input

2

3

M..

.N.

..T

3

M..

#

..T

Sample Output

Case #1: 5

Case #2: -1

题意:N*N的格子,有一些格子上面有监视器,ESNW分别表示刚开始的朝向,监视器每秒钟顺时针转90度,它能监视它和他当前朝向的相邻的一个格子。现在想从M走到T,最小需要多长时间。从被监视器监视的格子走到相邻的格子,或者走到有监视器的格子,需要花费三秒的时间,其他一秒

思路:显然监视器是以4为周期的,那么如果对于两个状态,如果坐标相同,并且监视器的状态相同的话,那么这两个状态就是相同的,那么我们可以用vis[x][y][cur%4]来标记这个状态是不是走过来转移,当然因为求最小时间,所以用优先队列就可以了

[code]#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=510;
int N;
char s[maxn][maxn];
int sx,sy;
int vis[maxn][maxn][5];
struct Node{
    int x,y,t;
    Node(){}
    Node(int _x,int _y,int _t):x(_x),y(_y),t(_t){}
    bool operator<(const Node &A)const{
        return t>A.t;
    }
};
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};
int is_light(int x,int y){
    if(s[x][y]=='E')return 0;
    if(s[x][y]=='S')return 1;
    if(s[x][y]=='W')return 2;
    if(s[x][y]=='N')return 3;
    return -1;
}
int judge(int x,int y,int t){
    if(x<0||x>=N||y<0||y>=N||s[x][y]=='#')return 0;
    if(is_light(x,y)!=-1)return 1;
    for(int i=0;i<4;i++){
        int tx=x+dx[i];
        int ty=y+dy[i];
        if(tx<0||tx>=N||ty<0||ty>=N||s[tx][ty]=='#')continue;
        int tmp=is_light(tx,ty);
        if(tmp!=-1&&(tmp+t)%4==i){
            return 1;
        }
    }
    return 2;
}
int BFS(){
    memset(vis,0,sizeof(vis));
    priority_queue<Node> q;
    q.push(Node(sx,sy,0));
    vis[sx][sy][0]=1;
    int cur=0;
    while(!q.empty()){
        Node t=q.top();
        q.pop();
        if(s[t.x][t.y]=='T'){
            return t.t;
        }
        cur=t.t+1;
        if(!vis[t.x][t.y][cur%4]){
            vis[t.x][t.y][cur%4]=1;
            q.push(Node(t.x,t.y,cur));
        }
        int flag=judge(t.x,t.y,t.t);
        for(int i=0;i<4;i++){
            int tx=t.x+dx[i];
            int ty=t.y+dy[i];
            int f=judge(tx,ty,t.t);
            if(f==0)continue;
            if(flag==1||f==1)cur=t.t+3;
            else if(f==2)cur=t.t+1;
            if(!vis[tx][ty][cur%4]){
                vis[tx][ty][cur%4]=1;
                q.push(Node(tx,ty,cur));
            }
        }
    }
    return -1;
}
int main(){
    int T,cas=1;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        for(int i=0;i<N;i++){
            scanf("%s",s[i]);
            for(int j=0;j<N;j++){
                if(s[i][j]=='M'){
                    sx=i,sy=j;
                }
            }
        }
        printf("Case #%d: %d\n",cas++,BFS());
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: