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
题目大意是在一片N*N区域中,你从起点’M’以每秒1格的速度到终点’T’处,其中’.’是空地,’#’不可通过,’N’,’E’,’W’,’S’是朝向四个方向的摄像头,并瞬时间每秒转动1格方向,也就是说’E’在一秒后会朝向’S’。摄像头探测的范围为摄像头所在的点以及其面对的那个位置。在摄像头区域,你可以以每3秒一格的速度前进,或是停止不前进,这样的动作都不会被摄像头发现。请问你从起点到终点最少用时几秒?若不能到达,输出-1。
这道题其实不难,但是我在看题目的时候被那么多制约条件给搞混淆了。不过冷静下来仔细一想就是一道广搜题目。具体解释结合代码来看。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
const int MAXN=505;
const int INF=0x3f3f3f3f;
int nxt[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};
using namespace std;
struct sa
{
int x,y,step;
bool operator< (const sa &other)const
{
return step>other.step;
}
} a;
int T,n;
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int startx,starty,endx,endy;
bool check(int x,int y,int time)//检查能否被摄像头看到 代码很长 也很好写
{
if (map[x][y]!='.') return 0;
if (x-1>=0 && map[x-1][y]!='.')
{
switch(time)
{
case 0:
if (map[x-1][y]=='S') return 0;
break;
case 1:
if (map[x-1][y]=='E') return 0;
break;
case 2:
if (map[x-1][y]=='N') return 0;
break;
case 3:
if (map[x-1][y]=='W') return 0;
break;
}
}
if (x+1<n && map[x+1][y]!='.')
{
switch(time)
{
case 0:
if (map[x+1][y]=='N') return 0;
break;
case 1:
if (map[x+1][y]=='W') return 0;
break;
case 2:
if (map[x+1][y]=='S') return 0;
break;
case 3:
if (map[x+1][y]=='E') return 0;
break;
}
}
if (y-1>=0 && map[x][y-1]!='.')
{
switch(time)
{
case 0:
if (map[x][y-1]=='E') return 0;
break;
case 1:
if (map[x][y-1]=='N') return 0;
break;
case 2:
if (map[x][y-1]=='W') return 0;
break;
case 3:
if (map[x][y-1]=='S') return 0;
break;
}
}
if (y+1<n && map[x][y+1]!='.')
{
switch(time)
{
case 0:
if (map[x][y+1]=='W') return 0;
break;
case 1:
if (map[x][y+1]=='S') return 0;
break;
case 2:
if (map[x][y+1]=='E') return 0;
break;
case 3:
if (map[x][y+1]=='N') return 0;
break;
}
}
return 1;
}
void bfs(int x0,int y0)
{
priority_queue<sa>q;//创建优先队列
a.x=x0;
a.y=y0;
a.step=0;
vis[a.x][a.y]=0;
q.push(a);
int time;
int i,j;
while(!q.empty())
{
a=q.top();
if (a.x==endx && a.y==endy)//走到终点了
{
printf("%d\n",a.step);
break;
}
q.pop();
a.step++;
for(i=0; i<4; i++)//广搜 4个方向的遍历
{
a.x+=nxt[i][0];
a.y+=nxt[i][1];
if (map[a.x][a.y]=='#' || a.x<0 || a.y<0 || a.x>=n || a.y>=n)//边界条件
{
a.x-=nxt[i][0];
a.y-=nxt[i][1];
continue;
}
if (map[a.x][a.y]=='.')
{
for(j=0; j<3; j++)//最多停2秒 不然你还不如3秒一格走过去
{
time=(a.step+j-1)%4;
if (check(a.x,a.y,time) && check(a.x-nxt[i][0],a.y-nxt[i][1],time)
&& (a.step+j<vis[a.x][a.y]))
{
a.step+=j;
vis[a.x][a.y]=a.step;
q.push(a);
a.step-=j;
break;
}
}
if (j==3)//3秒1格走
{
a.step+=2;
if (a.step<vis[a.x][a.y])
{
vis[a.x][a.y]=a.step;
q.push(a);
}
a.step-=2;
}
}
else//所在就是摄像头 也用3秒一格走
{
a.step+=2;
if (a.step<vis[a.x][a.y])
{
vis[a.x][a.y]=a.step;
q.push(a);
}
a.step-=2;
}
a.x-=nxt[i][0];
a.y-=nxt[i][1];
}
}
if (q.empty()) printf("-1\n");
}
int main()
{
scanf("%d",&T);
for(int t=1; t<=T; t++)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
scanf(" %c",&map[i][j]);
if (map[i][j]=='M')
{
startx=i,starty=j;
map[i][j]='.';
}
if (map[i][j]=='T')
{
endx=i;
endy=j;
map[i][j]='.';
}
}
}
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
vis[i][j]=INF;
printf("Case #%d: ",t);
bfs(startx,starty);
}
return 0;
}