题目描述:

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

题目分析:

题目大意是在一片N*N区域中,你从起点’M’以每秒1格的速度到终点’T’处,其中’.’是空地,’#’不可通过,’N’,’E’,’W’,’S’是朝向四个方向的摄像头,并瞬时间每秒转动1格方向,也就是说’E’在一秒后会朝向’S’。摄像头探测的范围为摄像头所在的点以及其面对的那个位置。在摄像头区域,你可以以每3秒一格的速度前进,或是停止不前进,这样的动作都不会被摄像头发现。请问你从起点到终点最少用时几秒?若不能到达,输出-1。
这道题其实不难,但是我在看题目的时候被那么多制约条件给搞混淆了。不过冷静下来仔细一想就是一道广搜题目。具体解释结合代码来看。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>const int MAXN=505;
const int INF=0x3f3f3f3f;
int nxt[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};
using namespace std;struct sa
{int x,y,step;bool operator< (const sa &other)const{return step>other.step;}
} a;int T,n;
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int startx,starty,endx,endy;bool check(int x,int y,int time)//检查能否被摄像头看到 代码很长 也很好写
{if (map[x][y]!='.') return 0;if (x-1>=0 && map[x-1][y]!='.'){switch(time){case 0:if (map[x-1][y]=='S') return 0;break;case 1:if (map[x-1][y]=='E') return 0;break;case 2:if (map[x-1][y]=='N') return 0;break;case 3:if (map[x-1][y]=='W') return 0;break;}}if (x+1<n && map[x+1][y]!='.'){switch(time){case 0:if (map[x+1][y]=='N') return 0;break;case 1:if (map[x+1][y]=='W') return 0;break;case 2:if (map[x+1][y]=='S') return 0;break;case 3:if (map[x+1][y]=='E') return 0;break;}}if (y-1>=0 && map[x][y-1]!='.'){switch(time){case 0:if (map[x][y-1]=='E') return 0;break;case 1:if (map[x][y-1]=='N') return 0;break;case 2:if (map[x][y-1]=='W') return 0;break;case 3:if (map[x][y-1]=='S') return 0;break;}}if (y+1<n && map[x][y+1]!='.'){switch(time){case 0:if (map[x][y+1]=='W') return 0;break;case 1:if (map[x][y+1]=='S') return 0;break;case 2:if (map[x][y+1]=='E') return 0;break;case 3:if (map[x][y+1]=='N') return 0;break;}}return 1;
}void bfs(int x0,int y0)
{priority_queue<sa>q;//创建优先队列a.x=x0;a.y=y0;a.step=0;vis[a.x][a.y]=0;q.push(a);int time;int i,j;while(!q.empty()){a=q.top();if (a.x==endx  && a.y==endy)//走到终点了{printf("%d\n",a.step);break;}q.pop();a.step++;for(i=0; i<4; i++)//广搜 4个方向的遍历{a.x+=nxt[i][0];a.y+=nxt[i][1];if (map[a.x][a.y]=='#' || a.x<0 || a.y<0 || a.x>=n || a.y>=n)//边界条件{a.x-=nxt[i][0];a.y-=nxt[i][1];continue;}if (map[a.x][a.y]=='.'){for(j=0; j<3; j++)//最多停2秒 不然你还不如3秒一格走过去{time=(a.step+j-1)%4;if (check(a.x,a.y,time) && check(a.x-nxt[i][0],a.y-nxt[i][1],time)&& (a.step+j<vis[a.x][a.y])){a.step+=j;vis[a.x][a.y]=a.step;q.push(a);a.step-=j;break;}}if (j==3)//3秒1格走{a.step+=2;if (a.step<vis[a.x][a.y]){vis[a.x][a.y]=a.step;q.push(a);}a.step-=2;}}else//所在就是摄像头 也用3秒一格走{a.step+=2;if (a.step<vis[a.x][a.y]){vis[a.x][a.y]=a.step;q.push(a);}a.step-=2;}a.x-=nxt[i][0];a.y-=nxt[i][1];}}if (q.empty()) printf("-1\n");
}int main()
{scanf("%d",&T);for(int t=1; t<=T; t++){scanf("%d",&n);for(int i=0; i<n; i++){for(int j=0; j<n; j++){scanf(" %c",&map[i][j]);if (map[i][j]=='M'){startx=i,starty=j;map[i][j]='.';}if (map[i][j]=='T'){endx=i;endy=j;map[i][j]='.';}}}for(int i=0; i<n; i++)for(int j=0; j<n; j++)vis[i][j]=INF;printf("Case #%d: ",t);bfs(startx,starty);}return 0;
}

Instrusive hdu 5040 优先队列+BFS相关推荐

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

    2014北京网络赛09题,hdu 5040 这次网络赛真是惨,也怪做题策略没想好,当时切完签到题之类的水题之后,马上就去看06青蛙那题去了.结果被那只死青蛙给坑惨了T_T...搞了四小时没搞出来... ...

  2. POJ 2312 Battle City 优先队列+BFS

    相信坦克大战大家都玩过吧,本题就是根据这个游戏设计的.坦克要从起点(Y),到目的地(T),坦克不能通过钢墙(S),河(R),可以在空地在行走(E),射击破坏砖墙(B),射击砖墙时不行走且花费一个单位的 ...

  3. LeetCode 1102. 得分最高的路径(优先队列BFS/极大极小化 二分查找)

    文章目录 1. 题目 2. 解题 2.1 优先队列BFS 2.2 极大极小化 二分查找 1. 题目 给你一个 R 行 C 列的整数矩阵 A.矩阵上的路径从 [0,0] 开始,在 [R-1,C-1] 结 ...

  4. 广度优先搜索BFS进阶(一):多源BFS、优先队列BFS、双端队列BFS

    一.多源BFS 在上一篇博客:广度优先搜索BFS基础中,我们接触到的BFS均是单起点(单源)的,但是对于某一些问题,其有多个起点,此类问题我们称为多源BFS问题.先思考下面一道例题: 1.腐烂的橘子 ...

  5. Instrusive 【HDU - 5040】【2014 北京 BFS】

    题目链接 一道有着很多需要细节的地方需要注意的题,挺不错的. 这题的数据也是给的很好,然后讲一下题意吧. 题意:有一个N*N的网格,有起点M和终点T,我们从起点需要走到终点,每一步需要花费的时间是单位 ...

  6. HDU 1242 Rescue BFS+优先队列

    题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1242 #include <stdio.h> #include <stri ...

  7. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

  8. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  9. HDU - 1043 Eight(bfs打表)

    题目链接:点击查看 题目大意:八数码经典问题,给出一个3*3的矩阵,其中随机分布着1~8的数字以及一个空位(我们用x来表示空位),在整个矩阵中,每一次操作都可以将x和他附近的方块互换,问经过多少次操作 ...

最新文章

  1. 信号 频率_信号的时间域分辨率和频率域分辨率
  2. 大型网站 linux,大型网站架构演变
  3. python图片比对、自动化测试,腾讯优图及知脸(ZKface)人脸比对接口测试(python)
  4. kafka 报错:kafka server:Message was too large ,server rejected it to avoid allocation
  5. liferay mysql driver_Liferay更改数据库(oracle,mysql)
  6. DevOps案例研究|史上最能“拜客户教”的公司,是如何做到持续交付的?(第1趴)...
  7. [剑指offer][JAVA]面试题[第23题][合并K个排序链表][分治][优先队列]
  8. Spark Streaming 技术看点!
  9. opencv学习笔记3
  10. javascript进阶课程--第一章--函数
  11. HDU 5045 Contest
  12. python 输出文件中返回码为200的接口的平均响应时间_Django查看响应时间问题
  13. 终于可以和 QQ 彻底说再见了!
  14. CefSharp中文帮助文档
  15. Clion解决c++源文件多个编译运行
  16. 图文演示戴尔win10重装系统步骤
  17. 基于CNN实现垃圾分类案例
  18. iframe标签有什么用
  19. 无线网络技术导论笔记(第六讲)
  20. App在appstore下架的方法

热门文章

  1. 一分钟批量查询邮政快递的教程
  2. 国内几款接口管理平台,使用体验分析对比;总有一款是你想要的!
  3. 可变数据之流水号数据的批量打印
  4. java函数是什么意思啊_[求助]请问setHorizontalAlignment是什么意思!什么函数来
  5. 大数据催生智慧园区_“大数据”催生智慧园区
  6. oracle校验社会统一信用代码(菜J笔记)
  7. Javaweb微专业第二十讲-----发送邮件(预告)
  8. 弱监督目标检测算法论文阅读(四)Localizing Common Objects Using Common Component Activation Map
  9. 计算机主机外设接口,计算机机系统中,所有外设都必须通过接口连接到主机上。...
  10. +50dB/-50dB 是什么概念.