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.
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
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'.
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
很麻烦容易写错但是不难的一个广搜
注意几点:
1.方向不要乱写,否则判断旁边是否有照相机特别麻烦,还容易写错
2.人走的这秒照相机是没有动的,比方说第一个示例,答案是5不是4,就是因为人第一秒走的时候照相机还朝南,会被照到==
3.先判断是否可以走再判断这个点是否出现过,因为vis数组是有时间的,能走的话才可以判断vis是否存在过==
4.不要懒,结构体的构造函数写了可以省不少事,judge参数也不要只是一个结构体,三个参数分开会好一些
5.不要纠结与为啥先判断走3秒再判断往下走,两者是矛盾的,先写谁都行
思路参考袁同学的代码==
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int dx[4]= {0,-1,0,1};
int dy[4]= {-1,0,1,0};
const int maxn=510;
char str[maxn][maxn];
bool vis[maxn][maxn][5];
int n;
int pos[maxn];
struct node
{
int x,y,t;
node()
{
;
}
node(int nx,int ny,int nt)
{
x=nx;
y=ny;
t=nt;
}
bool operator<(const node s)const{return t>s.t;}
};
int judge(int x,int y,int t)
{
if(x<0||x>=n||y<0||y>=n||str[x][y]=='#')
return 0;///不操作
if(pos[str[x][y]]!=-1)
return 1;///有摄像头或者被照射
int nx,ny;
int nowt;
for(int i=0; i<4; i++)
{
nx=dx[i]+x;
ny=dy[i]+y;
if(nx<0||nx>=n||ny<0||ny>=n||str[nx][ny]=='#')
continue;
int temp=pos[str[nx][ny]];
if(temp>=0&&(temp+t)%4==i)
return 1;
}
return 2;///通行无阻
}
int bfs(int fx,int fy)
{
priority_queue<node>Q;///优先队列
memset(vis,false,sizeof(vis));
Q.push(node(fx,fy,0));
while(!Q.empty())
{
node now=Q.top();
Q.pop();
/// cout<<now.x<<" "<<now.y<<" "<<now.t<<endl;
if(str[now.x][now.y]=='T')
return now.t;
int nx,ny,nowt;
nowt=now.t+1;
if(!vis[now.x][now.y][nowt%4])///考虑当前状态,未有过,加入队列
{
vis[now.x][now.y][nowt%4]=true;
Q.push(node(now.x,now.y,nowt));
}
int nowv=judge(now.x,now.y,now.t);///检查当前状态
for(int i=0; i<4; i++)
{
nx=dx[i]+now.x;
ny=dy[i]+now.y;
int nextv=judge(nx,ny,now.t);
if(nextv==0)
continue;
if(nextv==1||nowv==1)///当前状态被照射或者下一位置被照射
nowt=now.t+3;
else if(nextv==2)
nowt=now.t+1;
if(!vis[nx][ny][nowt%4])///判断该位置该时刻状态是否有过
{
vis[nx][ny][nowt%4]=true;
Q.push(node(nx,ny,nowt));
}
}
}
return -1;
}
int main()
{
// freopen("cin.txt","r",stdin);
int t;
int T=0;
int mi,mj;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(pos,-1,sizeof(pos));
pos['E']=0,pos['S']=1,pos['W']=2,pos['N']=3;
for(int i=0; i<n; i++)
{
scanf("%s",str[i]);
for(int j=0; j<n; j++)
{
if(str[i][j]=='M')
mi=i,mj=j;
}
}
int ans=bfs(mi,mj);
printf("Case #%d: %d\n",++T,ans);
}
return 0;
}