HDU5040 Instrusive,bfs+优先队列

来源:互联网 发布:建设银行mac版网银盾 编辑:程序博客网 时间:2024/09/21 08:59

1.题目叙述:

Instrusive

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


Problem Description

The legendarymercenary 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 thegrids and Matt is now in another grid.

In normal case, Matt can move from a grid to one of the four neighbor grids ina 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 thesegrids.

There are also some surveillance cameras in the grids. Every camera is facingone of the four direction at first, but for every second, they will rotate 90degree clockwisely. Every camera's sight range is 2, which means that if Mattis in the same grid as the camera, or in the grid that the camera is facing, hewill be seen immediately and the mission will fail.

Matt has a special equipment to sneak: a cardbox. Matt can hide himself in thecard box and move without being noticed. But In this situation, Matt will haveto use 3 seconds to move 1 grid. Matt can also just hide in the cardbox withoutmoving. 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 cardboxinto a grid which is now insight of cameras or from a grid which is now insightof 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 ofthe input contains an integer T, denoting the number of testcases. Then T testcases 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 thegrids.

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 testcase, 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/ICPCAsia Regional Beijing Online

 

 

Recommend

hujie   |   Wehave carefully selected several similar problems for you:  6014 6013 6012 6011 6010 

2.题目大意:一个n*n的矩阵,#表示墙,M表示人Matt,T表示目标,M要到T点去,#不可以走,路途有摄像头,如果被摄像头发现任务就是败了,摄像头可以看到两个格子的区域,一是自身所在区域,二是它面对方向的位子,摄像头可以转动,1秒转动一次,有四个方向N,S,W,E,表示 上北,下南,作西,右东,Matt有一个神秘盒子,他可以躲到里面不让摄像头发现,在盒子中移动每一格要花费3秒,不带盒子每移动一格花费1秒,也可以在盒子待着不动;问Matt到达T花费最短时间是多少,如果不能完成任务输出-1;

3.思路解决:

bfs搜索+优先队列,用一个三维数组表示状态,因为一个位子有四种状态,也就是四个时间点,(所有摄像头都转,每4秒一个周期啊),也就是Matt最多在一个位子呆4秒,进行bfs时,要走的下一点位置要考虑4种情况:1 有摄像头,2被摄像头照射,3空地且不被摄像头照射,4是# 墙不可以通过;  策略:遇到情况1,要走,时间加3,第二种情况,要停等待一秒,时间加1位置不变,情况3,走,时间加1 ,情况4可以直接跳过不操作;

对于摄像头转向问题,用时间%4就可以计算出当前摄像头方向了;对于四种情况直接开一个int数组,[110],初始化为-1,然后用W,E,S,N的ASCII作为下标,然后赋值也是相当方便的;考虑四个方向赋问题,可以结合搜索顺序,使得搜索顺序与方向代表值一一对应,举一个例子,我第一次向下S搜索,那么我要让‘N'赋值为0,也就是当搜索顺序值与方向值相等时刚好该位置被摄像图照射;

4.Ac代码:

0 0