HDU 5040 Instrusive(BFS+优先队列)
题意比较啰嗦。
就是搜索加上一些特殊的条件,比如可以在原地不动,也就是在原地呆一秒,如果有监控也可以花3秒的时间走过去。
这种类型的题目还是比较常见的。以下代码b[i][j][x]表示格子i行j列在x时刻有监控照的到。因为只有4个方向,所以只需要时间对4取模就行。具体细节见代码。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = ;
const int dx[] = {, , , -, }, dy[] = {, -, , , };
char mp[maxn][maxn];
int b[maxn][maxn][];
int T, n, kase;
bool vis[maxn][maxn][];
struct Node {
int x, y, step;
bool operator < (const Node &b) const {
return step > b.step;
}
};
Node tar;
int bfs(int x, int y)
{
memset(vis, false, sizeof(vis));
priority_queue<Node> Q;
Node cur, nex;
cur.x = x; cur.y = y; cur.step = ;
vis[x][y][] = true;
Q.push(cur);
while (!Q.empty())
{
cur = Q.top(); Q.pop();
if (tar.x == cur.x && tar.y == cur.y) return cur.step;
for (int i = ; i < ; i++)//最后一个状态是可以呆在原地不动
{
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (nx < || ny < || nx >= n || ny >= n) continue;
if (mp[nx][ny] == '#') continue;
nex = cur;
if (b[nx][ny][cur.step % ] || b[cur.x][cur.y][cur.step % ])//如果下一个位置有监控或者当前这个位置监控的到
{
if (nx == cur.x && ny == cur.y && !vis[nx][ny][(cur.step + ) % ])//如果当前这个位置监控的到,
{
nex.step++;
vis[nx][ny][nex.step % ] = true;
Q.push(nex);
}
else if (!vis[nx][ny][(cur.step + ) % ])//如果下一个监控的到
{
nex.x = nx; nex.y = ny;
nex.step += ;
vis[nx][ny][nex.step % ] = true;
Q.push(nex);
}
}
else if (!vis[nx][ny][(cur.step + ) % ])//否则直接走
{
nex.x = nx; nex.y = ny; nex.step++;
vis[nx][ny][nex.step % ] = true;
Q.push(nex);
}
}
}
return -;
}
int main()
{
scanf("%d", &T);
while (T--)
{
int x, y;
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%s", mp[i]);
memset(b, , sizeof(b));
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
if (mp[i][j] == 'M')
{
x = i; y = j;
}
else if (mp[i][j] == 'N')
{
b[i][j][] = b[i][j][] = b[i][j][] = b[i][j][] = ;//它本身这个位置不管任意时刻都被监控到
if (i - >= ) b[i - ][j][] = ;//表示紧挨着它上面的那个在1秒的时候被监控到
if (j + < n) b[i][j + ][] = ;//紧挨着它右边的在第2秒的时候,意思就是下一秒
if (i + < n) b[i + ][j][] = ;//下两秒
if (j - >= ) b[i][j - ][] = ;//下三秒在忘左边的位置被监控的到
}
else if (mp[i][j] == 'E')
{
b[i][j][] = b[i][j][] = b[i][j][] = b[i][j][] = ;
if (i - >= ) b[i - ][j][] = ;
if (j + < n) b[i][j + ][] = ;
if (i + < n) b[i + ][j][] = ;
if (j - >= ) b[i][j - ][] = ;
}
else if (mp[i][j] == 'S')
{
b[i][j][] = b[i][j][] = b[i][j][] = b[i][j][] = ;
if (i - >= ) b[i - ][j][] = ;
if (j + < n) b[i][j + ][] = ;
if (i + < n) b[i + ][j][] = ;
if (j - >= ) b[i][j - ][] = ;
}
else if (mp[i][j] == 'W')
{
b[i][j][] = b[i][j][] = b[i][j][] = b[i][j][] = ;
if (i - >= ) b[i - ][j][] = ;
if (j + < n) b[i][j + ][] = ;
if (i + < n) b[i + ][j][] = ;
if (j - >= ) b[i][j - ][] = ;
}
else if (mp[i][j] == 'T')
{
tar.x = i; tar.y = j;
}
}
}
printf("Case #%d: %d\n", ++kase, bfs(x, y));
}
return ;
}
HDU 5040 Instrusive(BFS+优先队列)的更多相关文章
- hdu 5040 Instrusive【BFS+优先队列】
11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- hdu 5040 Instrusive
Instrusive Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tota ...
- HDU——1242Rescue(BFS+优先队列求点图最短路)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 2014年北京网络赛 Instrusive HDU 5040 题解 优先队列
网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点, ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
随机推荐
- Html5游戏框架createJs的简单用法
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢!http://www.it165.net/pro/html/201403/11105.html 楼主记忆力不好,最近刚好用了一下create ...
- Ueditor之SAE移植
新浪SAE环境下使用UEditor http://www.cnblogs.com/zjzhome/p/3815460.html?utm_source=tuicool 在SAE上使用Ueditor的图片 ...
- Python自动化运维之26、Web框架本质、MVC与MTV
一.Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:ut ...
- openerp import namespace
# If True, the Python modules inside the openerp namespace are made available# without the 'openerp. ...
- [walkthrough] 在Asp.net MVC6 RC里使用NLog,并且把配置集成到config.json
说明一下:本文基于随visual studio 2015 RC公开的DNX1.0.0-beta4,git上最新的aspnet的开发版本已经发生了很大变化. 首先,理论部分看[汤姆大叔的博客] 解读AS ...
- java-web-j2e学习建议路线
JAVA学习之路(2) 首先要明白Java体系设计到得三个方面:J2SE,J2EE,J2ME(KJAVA).J2SE,Java 2 Platform Standard Edition,我们经常说 ...
- 【Linux】常用命令 lsof查看打开的文件
Linux系统把软硬件都抽象成文件,所以通过文件可以追踪到很多重要信息,如读取的配置文件.打开的端口等. 下面是常见的用法: 默认测试文件名为text.txt 1,显示打开text.txt的进程: l ...
- BZOJ 3223 文艺平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 ...
- java 内存 垃圾回收调优
要了解Java垃圾收集机制,先理解JVM内存模式是非常重要的.今天我们将会了解JVM内存的各个部分.如何监控以及垃圾收集调优. Java(JVM)内存模型 正如你从上面的图片看到的,JVM内存被分成多 ...
- Robot Motion
Description A robot has been programmed to follow the instructions in its path. Instructions for the ...