hdu5040 優先佇列+bfs
http://acm.hdu.edu.cn/showproblem.php?pid=5040
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.
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
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'.
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
/**
hdu5040 優先佇列+bfs
題目大意:給定一個n*n的圖,從M走到T,#不能走,有一些照相機如果被照相機照住可以3秒一格的速度繼續走或者停留1秒,照相機
只能找到其所在位置和它朝向的相鄰一個格,並且沒秒順時針旋轉90度,問最短的時間
解題思路:b[i][j][t%4]表示在ij點t秒時格子是否被照到,然後優先佇列bfs就可以搞定了
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=555;
int n,xx1,xx2,yy1,yy2,vis[maxn][maxn][5],b[maxn][maxn][5];
int dx[][2]= {-1,0,1,0,0,-1,0,1,0,0};
char a[maxn][maxn];
struct note
{
int x,y,t;
bool operator <(const note &other)const
{
return t>other.t;
}
};
int bfs()
{
// printf("%d %d,%d %d\n",xx1,yy1,xx2,yy2);
memset(vis,0,sizeof(vis));
priority_queue<note>q;
note tmp;
tmp.x=xx1,tmp.y=yy1,tmp.t=0;
q.push(tmp);
vis[xx1][yy1][0]=1;
while(!q.empty())
{
tmp=q.top();
int x=tmp.x;
int y=tmp.y;
int t=tmp.t;
// printf("t==%d\n",t);
q.pop();
if(x==xx2&&y==yy2)return t;
for(int i=0; i<5; i++)
{
int xx=x+dx[i][0];
int yy=y+dx[i][1];
if(xx<0||yy>=n||xx>=n||yy<0||a[xx][yy]=='#')continue;
if(b[xx][yy][t%4]||b[x][y][t%4])
{
if(xx==x&&yy==y&&!vis[xx][yy][(t+1)%4])///原地不動呆1秒
{
vis[xx][yy][(t+1)%4]=1;
tmp.x=xx,tmp.y=yy,tmp.t=t+1;
q.push(tmp);
}
else if(!vis[xx][yy][(t+3)%4])///3秒一格走一格
{
vis[xx][yy][(t+3)%4]=1;
tmp.x=xx,tmp.y=yy,tmp.t=t+3;
q.push(tmp);
}
}
else if(!vis[xx][yy][(t+1)%4])///1秒一格走一格
{
vis[xx][yy][(t+1)%4]=1;
tmp.x=xx,tmp.y=yy,tmp.t=t+1;
q.push(tmp);
}
}
}
return -1;
}
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
memset(b,0,sizeof(b));
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%s",a[i]);
for(int j=0; j<n; j++)
{
if(a[i][j]=='N')
{
b[i][j][0]=b[i][j][1]=b[i][j][2]=b[i][j][3]=1;
if(i-1>=0)b[i-1][j][0]=1;
if(j+1<n)b[i][j+1][1]=1;
if(i+1<n)b[i+1][j][2]=1;
if(j-1>=0)b[i][j-1][3]=1;
}
else if(a[i][j]=='E')
{
b[i][j][0]=b[i][j][1]=b[i][j][2]=b[i][j][3]=1;
if(i-1>=0)b[i-1][j][3]=1;
if(j+1<n)b[i][j+1][0]=1;
if(i+1<n)b[i+1][j][1]=1;
if(j-1>=0)b[i][j-1][2]=1;
}
else if(a[i][j]=='S')
{
b[i][j][0]=b[i][j][1]=b[i][j][2]=b[i][j][3]=1;
if(i-1>=0)b[i-1][j][2]=1;
if(j+1<n)b[i][j+1][3]=1;
if(i+1<n)b[i+1][j][0]=1;
if(j-1>=0)b[i][j-1][1]=1;
}
else if(a[i][j]=='W')
{
b[i][j][0]=b[i][j][1]=b[i][j][2]=b[i][j][3]=1;
if(i-1>=0)b[i-1][j][1]=1;
if(j+1<n)b[i][j+1][2]=1;
if(i+1<n)b[i+1][j][3]=1;
if(j-1>=0)b[i][j-1][0]=1;
}
else if(a[i][j]=='T')
{
xx2=i,yy2=j;
}
else if(a[i][j]=='M')
{
xx1=i,yy1=j;
}
}
}
printf("Case #%d: %d\n",++tt,bfs());
}
return 0;
}
相關文章
- PHP優先佇列PHP佇列
- STL 優先佇列 用法佇列
- 堆與優先佇列佇列
- 淺談優先佇列佇列
- 優先佇列和堆排序佇列排序
- 01揹包優先佇列優化佇列優化
- 棧,佇列,優先順序佇列簡單介面使用佇列
- 佇列 優先順序佇列 python 程式碼實現佇列Python
- 優先佇列的比較器佇列
- 封裝優先順序佇列封裝佇列
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- 2018 北京賽區網路預選賽 A. Saving Tang Monk II(BFS+優先佇列)佇列
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- leetcode621——優先佇列的思路LeetCode佇列
- STL優先佇列最小堆最大堆佇列
- bfs廣度優先搜尋
- 手擼優先佇列——二叉堆佇列
- C++ STL 優先佇列 (priority_queue)C++佇列
- .NET 6 優先佇列 PriorityQueue 實現分析佇列
- 二叉堆實現優先佇列佇列
- Java優先順序佇列DelayedWorkQueue原理分析Java佇列
- 演算法面試(三) 優先佇列演算法面試佇列
- bfs 與優先佇列————洛谷p1126(歷經兩個小時總算AC了,哭暈)佇列
- 【堆】【優先佇列】[NOIP2004]合併果子佇列
- Facebook的分散式優先順序佇列FOQS分散式佇列
- 最詳細版圖解優先佇列(堆)圖解佇列
- 堆、堆排序和優先佇列的那些事排序佇列
- 8.13(優先佇列貪心維護+打表找規律+對頂堆優先佇列+DFS減枝+貪心dp)佇列
- 【Dijkstra演算法】未優化版+優先佇列優化版演算法優化佇列
- codeforces round 974(div.3)E(優先佇列實現dijstra演算法,devc++的優先佇列用greater報錯)佇列JS演算法devC++
- CodeForces - 960B:Minimize the error(優先佇列+貪心)Error佇列
- Sunscreen POJ - 3614(防曬油) 貪心-優先佇列佇列
- [USACO 2009 Feb Gold] Fair Shuttle (貪心+優先佇列)GoAI佇列
- Python3 執行緒優先順序佇列( Queue)Python執行緒佇列
- MaxHeap 最大堆 MinHeap 最小堆 PriorityQueue 優先佇列實現佇列
- CSP之公共鑰匙盒(模擬、排序、優先佇列)排序佇列
- 基本演算法——深度優先搜尋(DFS)和廣度優先搜尋(BFS)演算法
- RMQ——支援合併和優先順序的訊息佇列MQ佇列
- 資料結構之PHP(最大堆)實現優先佇列資料結構PHP佇列