Instrusive

Time Limit: 3000/1500 MS (Java/Others)

Memory Limit: 262144/262144 K (Java/Others)

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


解题心得:

  1. 题意就是一个骑士去救公主,但是迷宫中有监控,每个监控可以监控自己的位置和自己面对的前面一个位置,监控每1秒钟顺时针旋转90度,骑士有一个神奇的盒子,骑士在盒子下可以不被监控看到,在盒子下面可以选择不走也可以顶着盒子走路,每三秒走一格。问骑士最少要花多少时间才可以救到公主。
  2. 其实看似很多个状态,但是地图每四秒形成一个循环,所以在记录监控的时候就很简单了,每四个一个循环,而骑士也有三种选择,顶着盒子不走(在任意一个点都可以选择不走),自身位置没有被监控看到,要走的位置也没被监控看到,就直接花费一秒走过去,或者要被监控看到,可以选择顶着盒子每三秒走一格。
  3. 其实在标记已经走过的状态的时候用一个三维的数组就行了,前两维记录位置,第三维记录时间,时间每四秒循环一次,所以在记录时间也很简单。因为每一秒走的速度是不一样的(可以顶着盒子),所以选择优先队列,每次取出的时间都是花费的最少的时间,到达目标之后就可以直接跳出。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 510;
char maps[maxn][maxn];
int n,dir[4][2] = {-1,0,0,1,1,0,0,-1};
bool vis[maxn][maxn][10],oversee[maxn][maxn][10];
struct Tar
{
int x,y;
int dir;
} tar;
struct NODE
{
int x,y;
int step;
} now,Next;
priority_queue <NODE> qu;
vector <Tar> ve;
bool operator < (const NODE& a,const NODE& b)
{
return a.step > b.step;
} void deal(int x,int y)
{
char ch = maps[x][y];
int i = x,j = y;
for(int i=0;i<4;i++)
oversee[x][y][i] = 1;
//写的很弱智
if(ch=='N')
{
oversee[i-1][j][0]=1;
oversee[i][j+1][1]=1;
oversee[i+1][j][2]=1;
oversee[i][j-1][3]=1;
}
if(ch=='W')
{
oversee[i][j-1][0]=1;
oversee[i-1][j][1]=1;
oversee[i][j+1][2]=1;
oversee[i+1][j][3]=1;
}
if(ch=='S')
{
oversee[i+1][j][0]=1;
oversee[i][j-1][1]=1;
oversee[i-1][j][2]=1;
oversee[i][j+1][3]=1;
}
if(ch=='E')
{
oversee[i][j+1][0]=1;
oversee[i+1][j][1]=1;
oversee[i][j-1][2]=1;
oversee[i-1][j][3]=1;
}
} void deal_oversee()//处理监控在各个时间的状态
{
for(int i=0; i<ve.size(); i++)
{
tar = ve[i];
int x = tar.x;
int y = tar.y;
deal(x,y);
}
} bool check(int x,int y,int step)
{
if(vis[x][y][step] || x<1 || y<1 || x>n || y>n || maps[x][y] == '#')
return false;
return true;
} void check_maps()
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
tar.x = i;
tar.y = j;
if(maps[i][j] == 'M')
{
now.x = i;
now.y = j;
now.step = 0;
}
//把有监控的位置给记录下来
if(maps[i][j] == 'N')
{
tar.dir = 0;
ve.push_back(tar);
}
if(maps[i][j] == 'E')
{
tar.dir = 1;
ve.push_back(tar);
}
if(maps[i][j] == 'S')
{
tar.dir = 2;
ve.push_back(tar);
}
if(maps[i][j] == 'W')
{
tar.dir = 3;
ve.push_back(tar);
}
}
} void init()
{
while(!qu.empty())//队列要记得清空
qu.pop();
scanf("%d",&n);
memset(vis,0,sizeof(vis));
memset(oversee,0,sizeof(oversee));
ve.clear();
for(int i=1; i<=n; i++)
scanf("%s",maps[i]+1);
check_maps();
deal_oversee();
} int bfs()
{
qu.push(now);
vis[now.x][now.y][now.step] = true;
while(!qu.empty())
{
now = qu.top();
qu.pop();
if(maps[now.x][now.y] == 'T')
return now.step;
Next = now;
Next.step++;
if(!vis[Next.x][Next.y][Next.step%4])//骑士选择不走
{
qu.push(Next);
vis[Next.x][Next.y][Next.step%4] = true;
}
for(int i=0; i<4; i++)
{
int step = now.step + 1;
int x = now.x + dir[i][0];
int y = now.y + dir[i][1];
if(!oversee[x][y][now.step%4] && !oversee[now.x][now.y][now.step%4])//当前位置和将要求的位置都没被监控看到
{
if(check(x,y,step%4))
{
Next.x = x;
Next.y = y;
Next.step = step;
vis[x][y][step%4] = true;
qu.push(Next);
}
}
else
{
Next.x = x;
Next.y = y;
Next.step = now.step + 3;//骑士选择顶着盒子走
if(check(x,y,Next.step%4))
{
vis[x][y][Next.step%4] = true;
qu.push(Next);
}
}
}
}
return -1;
} int main()
{
int t,T = 1;
scanf("%d",&t);
while(t--)
{
init();
int ans = bfs();
printf("Case #%d: ",T++);
printf("%d\n",ans);
}
return 0;
}

HDU:5040-Instrusive的更多相关文章

  1. hdu 5040 Instrusive【BFS+优先队列】

    11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...

  2. hdu 5040 Instrusive

    Instrusive Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tota ...

  3. HDU 5040 Instrusive(BFS+优先队列)

    题意比较啰嗦. 就是搜索加上一些特殊的条件,比如可以在原地不动,也就是在原地呆一秒,如果有监控也可以花3秒的时间走过去. 这种类型的题目还是比较常见的.以下代码b[i][j][x]表示格子i行j列在x ...

  4. HDU:过山车(二分图最大匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2063 题意:有m个男,n个女,和 k 条边,求有多少对男女可以搭配. 思路:裸的二分图最大匹配,匈牙利算法. 枚 ...

  5. HDU:Gauss Fibonacci(矩阵快速幂+二分)

    http://acm.hdu.edu.cn/showproblem.php?pid=1588 Problem Description Without expecting, Angel replied ...

  6. HDU:2846-Repository

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...

  7. HDU:1251-统计难题(字典树模板,动态建树,静态建树)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memor ...

  8. HDU:2767-Proving Equivalences(添边形成连通图)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 4000/2000 MS (Ja ...

  9. HDU:2255-奔小康赚大钱(KM算法模板)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Mem ...

随机推荐

  1. 053 Maximum Subarray 最大子序和

    给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大.例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4],连续子序列 [4,-1,2,1] 的和最大,为 ...

  2. guacamole 0.9.13安装与配置

    以下命令很多都需要管理权限,建议使用管理员账号执行,遇到问题可以留言. Guacamole官网文档介绍翻译:http://www.cnblogs.com/ji-yun/p/5657709.html 1 ...

  3. 收集的20个非常有用的Java程序片段

    下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...

  4. Image(支持 XML 序列化),注意C#中原生的Image类是无法进行Xml序列化的

    /// <summary> /// Image(支持 XML 序列化) /// </summary> [XmlRoot("XmlImage")] publi ...

  5. ApexSQL Log中的Redo Script跟原始SQL不一致问题

    最近遇到一个误更新数据的问题,使用ApexSQL Log做挖掘事务日志的时候,发现ApexSQL Log生成的Redo Script跟原始SQL是有区别的.它们并不是完全一致的.只是逻辑上等价而已.如 ...

  6. CentOS7.2上搭建httpbin环境

    CentOS7上搭建httpbin环境 1.安装python31)安装python3.6可能使用的依赖yum -y install openssl-devel bzip2-devel expat-de ...

  7. SQLServer 2012 Always on配置全过程

    AlwaysOn取数据库镜像和故障转移集群之长.AlwaysOn不再像故障转移集群那样需要共享磁盘,从而主副本和辅助副本可以更容易的部署到不同的地理位置:AlwaysOn还打破了镜像只能1对1的限制, ...

  8. LibreOJ #2037. 「SHOI2015」脑洞治疗仪

    线段树区间合并问题 恶心... 屠龙宝刀点击就送 #include <cstdio> #define N 200005 struct Segment { int l,r,mid,sum,l ...

  9. 洛谷 P1145 约瑟夫

    题目描述 n个人站成一圈,从某个人开始数数,每次数到m的人就被杀掉,然后下一个人重新开始数,直到最后只剩一个人.现在有一圈人,k个好人站在一起,k个坏人站在一起.从第一个好人开始数数.你要确定一个最小 ...

  10. 洛谷 P2324 [SCOI2005]骑士精神

    题目描述 输入输出格式 输入格式: 第一行有一个正整数T(T<=10),表示一共有N组数据.接下来有T个5×5的矩阵,0表示白色骑士,1表示黑色骑士,*表示空位.两组数据之间没有空行. 输出格式 ...