程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU-5040-Instrusive(BFS+優先隊列)

HDU-5040-Instrusive(BFS+優先隊列)

編輯:C++入門知識

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 
#include 
#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=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+1que;

        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						

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved