Instrusive

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

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
 
 
题意:一张图,给出起点终点,障碍物以及摄像头,摄像头每秒钟转90度,求到达终点的最短时间。
思路:一开始想着建立500*500*4,用spfa,常数写搓果断TLE,后来改2m步后自动break,涉险400ms过关。后来发现×4的状态可以去掉,改完之后去掉break优化700ms。再改用优先队列,170ms。再改用各种方法,都没有能在170ms内跑完的。尽力了,不改了。下面贴代码。
 /*
* Author: Joshua
* Created Time: 2014年09月21日 星期日 14时19分18秒
* File Name: 1009.cpp
*/
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 520
#define inf 0x7f7f7f7f
typedef long long LL;
int T,n,sx,sy,ex,ey,kase;
char map[maxn][maxn];
int dir[][]={{-,},{,},{,},{,-}};
int ce[maxn][maxn];
int xm=;
int d[maxn*maxn];
bool f[maxn*maxn]; struct cmp
{
bool operator()(int a,int b)
{
return d[a]>d[b]; }
}; void init()
{
scanf("%d",&n); scanf ("\n");
for (int i=;i<n;++i)
memset(ce[i],-,*n);
for (int i=;i<n;++i)
{
scanf("%s",map[i]);
for (int j=;j<n;++j)
{
if (map[i][j]=='M') sx=i,sy=j,map[i][j]='.';
if (map[i][j]=='T') ex=i,ey=j,map[i][j]='.';
if (map[i][j]=='N') ce[i][j]=,map[i][j]='.';
if (map[i][j]=='E') ce[i][j]=,map[i][j]='.';
if (map[i][j]=='S') ce[i][j]=,map[i][j]='.';
if (map[i][j]=='W') ce[i][j]=,map[i][j]='.';
}
}
int temp;
if (n<=)
{
for (int i=;i<n;++i)
for (int j=;j<n;++j)
{
temp=(i<<)+j;
d[temp]=inf;
f[temp]=false;
}
}
else
{
memset(d,0x7f,sizeof(d));
memset(f,,sizeof(f));
}
} void update(int x,int y,int dir,priority_queue<int, vector<int>,cmp> &q)
{
int temp=(x<<)+y;
if (d[temp]>dir)
{
d[temp]=dir;
q.push(temp);
}
} inline int fabs(const int &x)
{
if (x< ) return -x;
return x;
} bool find(int tx,int ty,int tt )
{
int xx,yy;
if (~ce[tx][ty]) return true;
for (int i=;i<;++i)
{
xx=tx+dir[i][];
yy=ty+dir[i][];
if (xx< || xx>=n || yy< || xx>=n) continue;
if (!(~ce[xx][yy])) continue;
if ( fabs( ((ce[xx][yy]+tt)&)-i)==) return true;
}
return false;
} void solve()
{
int temp,tx,ty,tt,xx,yy,dis,end=(ex<<)+ey;
bool flag;
priority_queue<int,vector<int>,cmp> q;
update(sx,sy,,q);
while (!q.empty())
{
temp=q.top();
q.pop();
if (f[temp]) continue;
if (temp==end) break;
f[temp]=true;
tx=(temp>>)&xm;
ty=temp&xm;
dis=d[temp];
tt=dis&;
for (int i=;i<;++i)
{
flag=find(tx,ty,tt+i);
for (int l=;l<;++l)
{
xx=tx+dir[l][];
yy=ty+dir[l][];
if (xx< || xx>=n || yy< || yy>=n) continue;
if (map[xx][yy]=='#') continue;
if (flag || find(xx,yy,tt+i)) update(xx,yy,dis+i+,q);
else update(xx,yy,dis++i,q);
}
}
}
int ans=inf;
if (d[end]<ans)
ans=d[end];
if (ans==inf) ans=-;
printf("Case #%d: %d\n",kase,ans);
} int main()
{
scanf("%d",&T);
kase=;
while (T--)
{
kase++;
init();
solve();
}
return ;
}

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(BFS+优先队列)

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

  3. 2014年北京网络赛 Instrusive HDU 5040 题解 优先队列

    网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点, ...

  4. HDU 5040

    http://acm.hdu.edu.cn/showproblem.php?pid=5040 题意比较难懂,有摄像头的位置是可以走的,每回合开始看做人先走摄像头再转,也就是说如果你这回合走之前没有摄像 ...

  5. hdu 5040 BFS 多维化处理图

    http://acm.hdu.edu.cn/showproblem.php?pid=5040 跟这一题http://blog.csdn.net/u011026968/article/details/3 ...

  6. hdu 5040 bfs

    http://acm.hdu.edu.cn/showproblem.php?pid=5040 一个人拿着纸盒子往目的地走  正常情况下一秒走一格  可以原地不动躲在盒子里  也可以套着盒子三秒走一格 ...

  7. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  8. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  9. HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

随机推荐

  1. 51nod_1417:天堂里的游戏

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1417 假设 ans=n/m,C=(A+B)/2 若出正面 ...

  2. MySql的事务隔离级别

    一,未提交读 顾名思义,未提交读就是能够读取到事务尚未提交所产生的数据.这种隔离方式会产生一种问题就是“脏读”. 脏读: 比方说有两个事务A B   在A事务里面将数据的id更改为2,但是A事务尚未提 ...

  3. php日期格式转换

    post过来的日期格式是2016-5-09,数据库表中日期数据类型只能用nvarchar(MAX),其他date.datatime都对前面表单的日历展示有影响.那么在做sql语句搜索前需要对日期格式进 ...

  4. 【HTML】dl dt dd

    摘要 看到没怎么使用过的html 标签,记录下 定义 dl 类似于 ul ,无任何样式,自定义列表容器, ul 为无序列表容器,ol 为有序列表容器 dt dd 类似于 li ,无任何样式,为帮助实现 ...

  5. 解决input[type=file]打开时慢、卡顿问题

    经过测试发现,在mac里面safari.Firefox.Chrome(opera不知道为啥老闪退)都没有卡顿问题 在windows里面,Firefox不卡顿,只有Chrome卡顿. 然而,这个插件是从 ...

  6. ue4粒子实现流血效果

    ---恢复内容开始--- 动作/射击游戏中,击中角色时常常伴随着血花效果,增强打击感的同时,也方便了玩家对命中与否的判断. 血液效果分两块,首先是受伤部位在受击瞬间产生血雾粒子,然后在身体.地面.墙面 ...

  7. MySql 求一段时间范围内的每一天,每一小时,每一分钟

    平常经常会求一段时间内的每一天统计数据,或者每一时点的统计数据.但是mysql本身是没有直接获取时点列表的函数或表.下面是自己用到的一些方法,利用临时变量和一个已存在的比较多数据(这个需要根据实际情况 ...

  8. Redis-简单实现星形主从配置

    高级参考(https://www.zhihu.com/question/21419897) 简单应用场景 现在配置redis 星形 集群, 有三台服务器, 怎样实现? 复制redis.conf两份, ...

  9. CentOS 7 安装Subversion, 并用Nginx代理

    环境:CentOS 7.3.1611 分三步:第一步:安装subversion第二步:安装httpd第三步:安装nginx 操作步骤: 安装subversion, 命令 -> yum -y in ...

  10. PyCharm中Directory与Python package的区别

    对于Python而言,有一点是要认识明确的,python作为一个相对而言轻量级的,易用的脚本语言(当然其功能并不仅限于此,在此只是讨论该特点),随着程序的增长,可能想要把它分成几个文件,以便逻辑更加清 ...