hdu5040 優先佇列+bfs

life4711發表於2015-10-14

http://acm.hdu.edu.cn/showproblem.php?pid=5040

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
 

Source
 
/**
hdu5040  優先佇列+bfs
題目大意:給定一個n*n的圖,從M走到T,#不能走,有一些照相機如果被照相機照住可以3秒一格的速度繼續走或者停留1秒,照相機
           只能找到其所在位置和它朝向的相鄰一個格,並且沒秒順時針旋轉90度,問最短的時間
解題思路:b[i][j][t%4]表示在ij點t秒時格子是否被照到,然後優先佇列bfs就可以搞定了
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=555;
int n,xx1,xx2,yy1,yy2,vis[maxn][maxn][5],b[maxn][maxn][5];
int dx[][2]= {-1,0,1,0,0,-1,0,1,0,0};
char a[maxn][maxn];

struct note
{
    int x,y,t;
    bool operator <(const note &other)const
    {
        return t>other.t;
    }
};
int bfs()
{
//    printf("%d %d,%d %d\n",xx1,yy1,xx2,yy2);
    memset(vis,0,sizeof(vis));
    priority_queue<note>q;
    note tmp;
    tmp.x=xx1,tmp.y=yy1,tmp.t=0;
    q.push(tmp);
    vis[xx1][yy1][0]=1;
    while(!q.empty())
    {
        tmp=q.top();
        int x=tmp.x;
        int y=tmp.y;
        int t=tmp.t;
//        printf("t==%d\n",t);
        q.pop();
        if(x==xx2&&y==yy2)return t;
        for(int i=0; i<5; i++)
        {
            int xx=x+dx[i][0];
            int yy=y+dx[i][1];
            if(xx<0||yy>=n||xx>=n||yy<0||a[xx][yy]=='#')continue;
            if(b[xx][yy][t%4]||b[x][y][t%4])
            {
                if(xx==x&&yy==y&&!vis[xx][yy][(t+1)%4])///原地不動呆1秒
                {
                    vis[xx][yy][(t+1)%4]=1;
                    tmp.x=xx,tmp.y=yy,tmp.t=t+1;
                    q.push(tmp);
                }
                else if(!vis[xx][yy][(t+3)%4])///3秒一格走一格
                {
                    vis[xx][yy][(t+3)%4]=1;
                    tmp.x=xx,tmp.y=yy,tmp.t=t+3;
                    q.push(tmp);
                }
            }
            else if(!vis[xx][yy][(t+1)%4])///1秒一格走一格
            {
                vis[xx][yy][(t+1)%4]=1;
                tmp.x=xx,tmp.y=yy,tmp.t=t+1;
                q.push(tmp);
            }
        }
    }
    return -1;
}
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        memset(b,0,sizeof(b));
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%s",a[i]);
            for(int j=0; j<n; j++)
            {
                if(a[i][j]=='N')
                {
                    b[i][j][0]=b[i][j][1]=b[i][j][2]=b[i][j][3]=1;
                    if(i-1>=0)b[i-1][j][0]=1;
                    if(j+1<n)b[i][j+1][1]=1;
                    if(i+1<n)b[i+1][j][2]=1;
                    if(j-1>=0)b[i][j-1][3]=1;
                }
                else if(a[i][j]=='E')
                {
                    b[i][j][0]=b[i][j][1]=b[i][j][2]=b[i][j][3]=1;
                    if(i-1>=0)b[i-1][j][3]=1;
                    if(j+1<n)b[i][j+1][0]=1;
                    if(i+1<n)b[i+1][j][1]=1;
                    if(j-1>=0)b[i][j-1][2]=1;
                }
                else if(a[i][j]=='S')
                {
                    b[i][j][0]=b[i][j][1]=b[i][j][2]=b[i][j][3]=1;
                    if(i-1>=0)b[i-1][j][2]=1;
                    if(j+1<n)b[i][j+1][3]=1;
                    if(i+1<n)b[i+1][j][0]=1;
                    if(j-1>=0)b[i][j-1][1]=1;
                }
                else if(a[i][j]=='W')
                {
                    b[i][j][0]=b[i][j][1]=b[i][j][2]=b[i][j][3]=1;
                    if(i-1>=0)b[i-1][j][1]=1;
                    if(j+1<n)b[i][j+1][2]=1;
                    if(i+1<n)b[i+1][j][3]=1;
                    if(j-1>=0)b[i][j-1][0]=1;
                }
                else if(a[i][j]=='T')
                {
                    xx2=i,yy2=j;
                }
                else if(a[i][j]=='M')
                {
                    xx1=i,yy1=j;
                }
            }
        }
        printf("Case #%d: %d\n",++tt,bfs());
    }
    return 0;
}


相關文章