HDU 5040 Instrusive 最短路

Instrusive

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 138 Accepted Submission(s): 34


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

Recommend
hujie


题意:在一个NxN的方格图中,有些位置有摄像头,每一秒都会顺时针转90度,摄像头能看守的范围是2个格子(摄像头所在的和面对的一个格子),然后要从起点走到终点,你有一个箱子,当你藏在箱子里面移动时,摄像头照到你也米有事,但是这样走一格需要3秒,不藏在箱子里走需要1秒,最后要求完成任务最少的时间。

思路:首先,题目说了,当一个格子被摄像头照着时,你又要进入这个格子,那么你就必须要用箱子。当你从一个正在被摄像头照着的格子出去时,也必须要用箱子。注意到摄像头的运动是会循环的,当你的当前时间d,根据d%4能直接得出摄像头正在照的方向。 所以我们用d[r][c][mod] 表示当前在(r,c),并且时间%4 为mod时,的最少时间。 那么我们转移的时候也可以选择不动,其他的转移根据摄像头的状态判断一下就好了。


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)
#define clr(a,x) memset(a,x,sizeof(a))
#define eps 1e-4
#define zero(x) (-eps < (x) && (x) < eps)
const int maxn = 500 + 5;
const int Move[2][4] = { {-1,0,1,0},{0,1,0,-1} };
const int inf = 0x3f3f3f3f;
int n;
inline int getx(int r,int c) { return r*n+c; }
inline int row(int x) { return x / n; }
inline int col(int x) { return x % n; }
inline bool inRange(int r,int c) { return 0<=r&&r<n&&0<=c&&c<n; }
char maze[maxn][maxn];
int d[maxn][maxn][4];
bool cam[maxn][maxn][4];
int S, T;
void input()
{
    scanf("%d",&n);
    clr(cam,0);
    rep(i,0,n) scanf("%s",maze[i]);
    rep(i,0,n) rep(j,0,n) {
        if (maze[i][j] == 'M') S = getx(i,j);
        else if(maze[i][j] == 'T') T = getx(i,j);
        else if (maze[i][j] == 'N') {
            rep(k,0,4) cam[i][j][k] = true;
            rep(k,0,2) {
                if (i-k>=0) cam[i-k][j][0] = true;
                if (j-k>=0) cam[i][j-k][3] = true;
                if (i+k<n) cam[i+k][j][2] = true;
                if (j+k<n) cam[i][j+k][1] = true;
            }
        } else if(maze[i][j] == 'S') {
            rep(k,0,2) {
                if (i-k>=0) cam[i-k][j][2] = true;
                if (j-k>=0) cam[i][j-k][1] = true;
                if (i+k<n) cam[i+k][j][0] = true;
                if (j+k<n) cam[i][j+k][3] = true;
            }
        } else if (maze[i][j] == 'E') {
            rep(k,0,2) {
                if (i-k>=0) cam[i-k][j][3] = true;
                if (j-k>=0) cam[i][j-k][2] = true;
                if (i+k<n) cam[i+k][j][1] = true;
                if (j+k<n) cam[i][j+k][0] = true;
            }
        } else if(maze[i][j] == 'W') {
            rep(k,0,2) {
                if (i-k>=0) cam[i-k][j][1] = true;
                if (j-k>=0) cam[i][j-k][0] = true;
                if (i+k<n) cam[i+k][j][3] = true;
                if (j+k<n) cam[i][j+k][2] = true;
            }
        }
    }
}


struct State
{
    int r,c,mod,d;
    State(int r,int c,int mod,int d)
    :r(r),c(c),mod(mod),d(d) { }
    bool operator<(const State&st) const
    {
        return d > st.d;
    }
};

bool done[maxn][maxn][4];
bool vis[maxn][maxn];
void solve()
{
    priority_queue<State> q;
    clr(d,0x3f);
    q.push(State(row(S),col(S),0,0));
    int r = row(S), c = col(S) , mod = 0;
    d[r][c][mod] = 0;
    clr(done,0);  clr(vis,0);
    while (q.size()) {
        State t = q.top(); q.pop();
        r = t.r, c = t.c , mod = t.mod;
        if (done[r][c][mod]) continue;
        done[r][c][mod] = true;
     //   printf("%d %d %d %d\n",r,c,mod,hide);
        vis[r][c] = true;
        rep(i,0,4) {
            int rr = r + Move[0][i];
            int cc = c + Move[1][i];
            if(!inRange(rr,cc) || maze[rr][cc] == '#') continue;
            int md;
            int w = 1; if (cam[r][c][mod] || cam[rr][cc][mod]) w = 3;
            md = (mod + w) % 4;
            if (d[rr][cc][md] > d[r][c][mod] + w) {
                d[rr][cc][md] = d[r][c][mod] + w;
                q.push(State(rr,cc,md,d[rr][cc][md]));
            }
        }
        int md = (mod+1)%4;
        if (d[r][c][md] > d[r][c][mod] + 1) {
            d[r][c][md] = d[r][c][mod] + 1;
            q.push(State(r,c,md,d[r][c][md]));
        }
    }

    int ans = inf;
    rep(i,0,4) ans = min(ans,d[row(T)][col(T)][i]);
    if (!vis[row(T)][col(T)]) puts("-1");
    else printf("%d\n",ans);
}

int main()
{
    int T; cin >> T;
    rep(cas,1,T+1) {
        input();
        printf("Case #%d: ",cas);
        solve();
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值