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率感 ...
随机推荐
- Day13 SQLAlchemy连表操作和堡垒机
一.数据库操作 1.创建表.插入数据和一对多查询 #!/usr/bin/env python # -*- coding: utf-8 -*- # Author: wanghuafeng from sq ...
- IOS各类问题
1.The resource could not be loaded because the App Transport Security policy requires the use of a s ...
- A Statistical View of Deep Learning (II): Auto-encoders and Free Energy
A Statistical View of Deep Learning (II): Auto-encoders and Free Energy With the success of discrimi ...
- angularjs学习总结(~~很详细的教程)
1 前言 前端技术的发展是如此之快,各种优秀技术.优秀框架的出现简直让人目不暇接,紧跟时代潮流,学习掌握新知识自然是不敢怠慢. AngularJS是google在维护,其在国外已经十分火热,可是国内的 ...
- 用Java实现非阻塞通信
用ServerSocket和Socket来编写服务器程序和客户程序,是Java网络编程的最基本的方式.这些服务器程序或客户程序在运行过程中常常会阻塞.例如当一个线程执行ServerSocket的acc ...
- 想做Android Wear开发?你得先搞明白这四件事
手环和手表的腕上穿戴之争,随着Apple Watch发布和Android Wear不断完善而告一段落.尽管续航上略有缺陷,但手表以其类似手机可扩展的生态环境赢得了众多巨头的支持. Google曾透露, ...
- bzoj3339 bzoj3585
两题本质是一样,只不过3585要离散化这种不修改,不强制的问题,显然先考虑离线算法这道题的思路和bzoj1878非常像考虑到如果只是求每个前缀的mex,我们是很容易扫一遍就得出来的我们设为这个位置的m ...
- -_-#【jsonp】cache
Cache jQuery’s JSONP Calls <script src="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.3 ...
- HDOJ 1215 七夕节
Problem Description 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!" ...
- tessilstrona
Untitled Document