码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-5040-Instrusive(BFS+优先队列)

时间:2014-09-21 19:27:11      阅读:466      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   io   os   ar   for   2014   

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
 

思路:从一个点到另一个点的话,任意一个点都被照到的话都不能直接走。所以对于任一个目标点,如果有灯就直接躲盒子里走,如果是空地,就最多等三秒。详见代码。

#include <cstdio>
#include <queue>
#define INF 99999999;
using namespace std;

char mp[500][505];
int n,ex,ey,nxt[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int vis[500][500];

struct S{
int x,y,step;

bool operator<(const S &p) const
{
    return step>p.step;
}

}t;

bool check(int x,int y,int time)//判断该点是否被照到
{
    if(mp[x][y]!='.') return 0;

    if(x-1>=0 && mp[x-1][y]!='.')
    {
        switch(time)
        {
            case 0:if(mp[x-1][y]=='S') return 0; break;
            case 1:if(mp[x-1][y]=='E') return 0; break;
            case 2:if(mp[x-1][y]=='N') return 0; break;
            case 3:if(mp[x-1][y]=='W') return 0; break;
        }
    }

    if(x+1<n && mp[x+1][y]!='.')
    {
        switch(time)
        {
            case 0:if(mp[x+1][y]=='N') return 0; break;
            case 1:if(mp[x+1][y]=='W') return 0; break;
            case 2:if(mp[x+1][y]=='S') return 0; break;
            case 3:if(mp[x+1][y]=='E') return 0; break;
        }
    }

    if(y-1>=0 && mp[x][y-1]!='.')
    {
        switch(time)
        {
            case 0:if(mp[x][y-1]=='E') return 0; break;
            case 1:if(mp[x][y-1]=='N') return 0; break;
            case 2:if(mp[x][y-1]=='W') return 0; break;
            case 3:if(mp[x][y-1]=='S') return 0; break;
        }
    }

    if(y+1<n && mp[x][y+1]!='.')
    {
        switch(time)
        {
            case 0:if(mp[x][y+1]=='W') return 0; break;
            case 1:if(mp[x][y+1]=='S') return 0; break;
            case 2:if(mp[x][y+1]=='E') return 0; break;
            case 3:if(mp[x][y+1]=='N') return 0; break;
        }
    }

    return 1;
}

int main()
{
    int T,i,j,time,cases=1;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d",&n);

        for(i=0;i<n;i++) scanf("%s",mp[i]);

        printf("Case #%d: ",cases++);

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(mp[i][j]=='M') mp[i][j]='.',t.x=i,t.y=j;
                else if(mp[i][j]=='T') mp[i][j]='.',ex=i,ey=j;
            }
        }

        for(i=0;i<n;i++) for(j=0;j<n;j++) vis[i][j]=INF;

        t.step=0;

        vis[t.x][t.y]=0;

        priority_queue<S>que;

        que.push(t);

        while(!que.empty())
        {
            t=que.top();

            if(t.x==ex && t.y==ey)
            {
                printf("%d\n",t.step);

                break;
            }

            que.pop();

            t.step++;

            for(i=0;i<4;i++)
            {
                t.x+=nxt[i][0];
                t.y+=nxt[i][1];

                if(mp[t.x][t.y]=='#' || t.x<0 || t.x>=n || t.y<0 || t.y>=n)
                {
                    t.x-=nxt[i][0];
                    t.y-=nxt[i][1];

                    continue;
                }

                if(mp[t.x][t.y]=='.')//如果目标点是空地
                {
                    for(j=0;j<3;j++)//最多等三秒
                    {
                        time=(t.step+j-1)%4;

                        if(check(t.x,t.y,time) && check(t.x-nxt[i][0],t.y-nxt[i][1],time) && t.step+j<vis[t.x][t.y])//起点和目标点都不被照到
                        {
                            t.step+=j;

                            vis[t.x][t.y]=t.step;

                            que.push(t);

                            t.step-=j;

                            break;//找到一个最小能到的时间即可
                        }
                    }

                    if(j==3)//如果等三秒都没等到可以走的,就直接躲盒子里面走
                    {
                        t.step+=2;

                        if(t.step<vis[t.x][t.y])
                        {
                            vis[t.x][t.y]=t.step;

                            que.push(t);
                        }

                        t.step-=2;
                    }
                }
                else//如果目标点是灯,则直接躲盒子里面走
                {
                    t.step+=2;

                    if(t.step<vis[t.x][t.y])
                    {
                        vis[t.x][t.y]=t.step;

                        que.push(t);
                    }

                    t.step-=2;
                }

                t.x-=nxt[i][0];
                t.y-=nxt[i][1];
            }
        }

        if(que.empty()) printf("-1\n");
    }
}

HDU-5040-Instrusive(BFS+优先队列)

标签:des   style   http   color   io   os   ar   for   2014   

原文地址:http://blog.csdn.net/faithdmc/article/details/39453345

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!