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;
}
相關文章
- 【BFS+優先佇列】HDU 3442 Three Kingdoms佇列
- acmdream1191 bfs+優先佇列ACM佇列
- UVALive 6665 Dragon’s Cruller (BFS + 優先佇列+hash)Go佇列
- HDU 1026(優先佇列+BFS+前驅記錄)佇列
- PHP優先佇列PHP佇列
- 堆--優先佇列佇列
- 優先佇列 (轉)佇列
- 淺談優先佇列佇列
- STL 優先佇列 用法佇列
- 堆與優先佇列佇列
- 堆和優先佇列佇列
- 優先佇列和堆排序佇列排序
- 堆排序與優先佇列排序佇列
- Java優先佇列(PriorityQueue)示例Java佇列
- 01揹包優先佇列優化佇列優化
- 棧,佇列,優先順序佇列簡單介面使用佇列
- Redis實現任務佇列、優先順序佇列Redis佇列
- NO GAME NO LIFE(優先佇列/最小堆)GAM佇列
- 優先佇列的比較器佇列
- 封裝優先順序佇列封裝佇列
- 二叉堆優先佇列佇列
- POJ 3253 Fence Repair 優先佇列AI佇列
- 堆——神奇的優先佇列(上)佇列
- 優先佇列的效能測試佇列
- 佇列 優先順序佇列 python 程式碼實現佇列Python
- 演算法面試(三) 優先佇列演算法面試佇列
- STL優先佇列最小堆最大堆佇列
- STL醜數(set+優先佇列)佇列
- 【圖論】拓撲排序+優先佇列圖論排序佇列
- 1007(優先佇列)佇列
- POJ 1724 ROADS(優先佇列+spfa)佇列
- POJ2431 Expedition (優先佇列)佇列
- POJ 2051(最小堆/優先佇列)佇列
- 三、資料結構演算法-棧、佇列、優先佇列、雙端佇列資料結構演算法佇列
- hdu5336 多校聯合第四場1010 模擬+bfs優先佇列佇列
- .NET 6 優先佇列 PriorityQueue 實現分析佇列
- Java優先順序佇列DelayedWorkQueue原理分析Java佇列
- C++ STL 優先佇列 (priority_queue)C++佇列