hdu 5040 Instrusive
Instrusive
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 596 Accepted Submission(s): 190
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.
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
If Matt cannot complete the mission, output '-1'.
3
Case #2: -1
/*
* 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的更多相关文章
- hdu 5040 Instrusive【BFS+优先队列】
11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...
- HDU 5040 Instrusive(BFS+优先队列)
题意比较啰嗦. 就是搜索加上一些特殊的条件,比如可以在原地不动,也就是在原地呆一秒,如果有监控也可以花3秒的时间走过去. 这种类型的题目还是比较常见的.以下代码b[i][j][x]表示格子i行j列在x ...
- 2014年北京网络赛 Instrusive HDU 5040 题解 优先队列
网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点, ...
- HDU 5040
http://acm.hdu.edu.cn/showproblem.php?pid=5040 题意比较难懂,有摄像头的位置是可以走的,每回合开始看做人先走摄像头再转,也就是说如果你这回合走之前没有摄像 ...
- hdu 5040 BFS 多维化处理图
http://acm.hdu.edu.cn/showproblem.php?pid=5040 跟这一题http://blog.csdn.net/u011026968/article/details/3 ...
- hdu 5040 bfs
http://acm.hdu.edu.cn/showproblem.php?pid=5040 一个人拿着纸盒子往目的地走 正常情况下一秒走一格 可以原地不动躲在盒子里 也可以套着盒子三秒走一格 ...
- 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 ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
随机推荐
- 有点难度的JS面试题
自己总结了一些JS面试题 希望能够帮助正在找工作的程序猿(●´∀`●) 1.js 实现一个函数对javascript中json 对象进行克隆 var oldObject ="sdf" ...
- Objective-C MapKit的使用-LBS简单的租车主界面demo
效果 分析 三个view:地图view.车辆信息view.车辆类型选择view 地图view:大头针的摆放,根据不同的种类显示大头针 车辆信息view:根据当前点击的大头针显示对应的车辆信息 车辆类型 ...
- 基于FPGA的VGA显示静态图片
终于熬到暑假了,记过三四周的突击带考试,终于为我的大二画上了一个完整的句号,接下来终于可以静心去做自己想做的事情了,前一阵子报了一个线上培训班,学学Sobel边缘检测,之前一直在学习图像处理,但是因为 ...
- 两百条微信小程序跳坑指南(不定时更新)
微信小程序联盟出品 跳坑textarea<二百二十三>不显示文本及textarea相关问题集合跳坑<二百一十三> background-image无法获取本地资源图片....跳 ...
- 一张图告诉你最流行的 7 个 JavaScript框架特点
欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~ AngularJ.js 由g ...
- css实现接地气的checkbox框
1.前言 我做的项目中,很少默认用原生的样式,甚至连下拉框都很少用select框,不过,原生也有原生的好处,来得快,没有什么大问题.如果是自己用html+css去拼接的话,样式会好看一点.不过应人而异 ...
- JUnit4总结
JUnit4使用要求: 测试方法必须使用@Test进行修饰 测试方法必须使用public void 进行修饰,不能带任何的参数 新建一个源代码目录来存放我们的测试代码 测试类的包应该和被测试类保持一致 ...
- 38. leetcode 405. Convert a Number to Hexadecimal
405. Convert a Number to Hexadecimal Given an integer, write an algorithm to convert it to hexadecim ...
- SSD的传输总线、传输协议、传输接口
前言:关于SSD,有众多总线类型.协议类型.接口类型,每个接口还包括不同型号,在这里花点时间全部整理一下,整理日期2017-08-08. 1.传输总线 总线就像一条公路,公路上的车好比总线上的电信号: ...
- windows下vue.js开发环境搭建教程
这篇文章主要为大家详细介绍了windows下vue.js开发环境搭建教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中 ...