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

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

2014 ACM/ICPC Asia Regional Beijing Online

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

#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");
    }
}
时间: 2024-10-13 22:05:05

HDU-5040-Instrusive(BFS+优先队列)的相关文章

HDU 5040 Instrusive BFS

如果题意明确了的话就是一个简单bfs...... 用优先队列搞一下还是很快的. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #inc

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

HDU 5040 Instrusive(北京网络赛I题)

HDU 5040 Instrusive 题目链接 思路:记忆化广搜,先预处理出图,每个位置用一个二进制数表示,表示4秒为1个周期内,这个位置是否会被照到,然后进行记忆化广搜即可,状态多开一个4,表示在4秒一周期,然后进行转移即可 代码: #include <cstdio> #include <cstring> #include <queue> using namespace std; const int N = 505; const int d[4][2] = {-1,

hdu 5040 Instrusive【BFS+优先队列】

2014北京网络赛09题,hdu 5040 这次网络赛真是惨,也怪做题策略没想好,当时切完签到题之类的水题之后,马上就去看06青蛙那题去了.结果被那只死青蛙给坑惨了T_T...搞了四小时没搞出来...跪给那只青蛙了...本来当时是准备要做这道题的,题目描述也是好蛋疼,有人说这题不如直接去看Clarification,不看题目了,这也说明这题题目描述确实不清晰,虽然没这么夸张,题目还是得看的. 重新看这道题,不是很难,最短时间的话,那当然想到BFS,纯BFS的话,只是最短步数,所以我们需要+优先队

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

2014 ACM/ICPC Asia Regional Beijing Online HDU 5040 Instrusive 优先队列

WA到死的一道题目. 一个人从起点走到目标点.这个过程中有摄像头,摄像头的照射范围为两个单位长度,包括摄像头自己的位置.为了避免被照射到,可以有两种选择. 在一个位置等待1S,或者坐在盒子里过去(花费3S),走一步花费1S.摄像头每秒顺时针转一次. 1.4S有一个循环,所以每个位置vis[r][c][sec] 四种情况的最优解 2.不用显示构图, 每个摄像头都记录一下四种情况 3.使用优先队列,判断 RE,TLE,WA,AC..... 刚开始每个摄像头旋转的时候没有判断是否在范围内,一直RE,起

HDU 5040 Instrusive

题目链接~~> 做题感悟:这题在网络赛的时候没有解出来,当时最后才写的有点慌了,so~>思路一点不清楚. 解题思路: 这题首先弄清楚在走每一步的时候是人先走,还是摄像头先走,当然是人先走,如果一个人从 A ---> B ,那么需要判断前一秒A 和 B 是否被摄像头照到,因为人先走,如果曾被摄像头找到,那么走过去会被发现,这样可以选择等一秒,下一秒同样再判断一次,如果还是不可以,就需要带着箱子走.还要注意时间第一秒时摄像头转到先一个方向.因为时间不是都加 1 ,需要用优先队列,同时一个点可

HDU 2425-Hiking Trip(BFS+优先队列)

Hiking Trip Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1303    Accepted Submission(s): 576 Problem Description Hiking in the mountains is seldom an easy task for most people, as it is extr

HDU 1242 (BFS+优先队列)

题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条件——时间,所以我们用优先队列去优先获取时间短的路径,总体实现起来没有太大难度. 代码如下: #include <iostream> #include <cstdio> #include <vector> #include <set> #include <

HDU 5040 Instrusive(最短路)

题目 : http://acm.hdu.edu.cn/showproblem.php?pid=5040 题意 : 从'M' 到  'T' 最短路程,每次只能走四个方向,并且有一些摄像头每个时间点都会转变下方向(初始方向给出).你有一个box,你在没有罩box的情况下不能被照到,可以在点上等待,也可以罩着box走(时间会慢成3个单位). 思路 : 就是一个最短路问题,只要算出在当前时间点u->v最多要花多时间才能到就行了(最多是3),用spfa跑. 1 #include <cstdio>