hdu4198-优先队列+bfs
来源:互联网 发布:hello树先生知乎 编辑:程序博客网 时间:2024/04/29 01:24
Description
Unfortunately the harbour isn't just a straight path to open sea. To protect the city from evil pirates, the entrance of the harbour is a kind of maze with drawbridges in it. Every bridge takes some time to open, so it could be faster to take a detour. Your task is to help captain Clearbeard and the fastest way out to open sea.
The pirates will row as fast as one minute per grid cell on the map. The ship can move only horizontally or vertically on the map. Making a 90 degree turn does not take any extra time.
Input
1. One line with three integers, h, w (3 <= h;w <= 500), and d (0 <= d <= 50), the height and width of the map and the delay for opening a bridge.
2.h lines with w characters: the description of the map. The map is described using the following characters:
―"S", the starting position of the ship.
―".", water.
―"#", land.
―"@", a drawbridge.
Each harbour is completely surrounded with land, with exception of the single entrance.
Output
Sample Input
26 5 7######S..##@#.##...##@####.###4 5 3######S#.##@..####@#
Sample Output
1611
题意;这道题的题意是叫你用最短的时间走出迷宫走向大海;S表示起点,走到@时间加上d+1;
走到 "."时间加1,’#‘表示墙,
注意的是最后结果还要+1,因为你只是走出了迷宫还没有走出大海;
思路:就是一道比较简单的bfs,由于出口可能有很多,所以要用优先队列来做;
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
#define nn 609
struct node
{
int x,y;
int step;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
};
int sx,sy;
int h,w,d;
int use[nn][nn];
char ma[nn][nn];
int dir[4][2]={{1,0},
{-1,0},
{0,1},
{0,-1}
};
priority_queue<node> que;
void bfs()
{
int i,j;
memset(use,0,sizeof(use));
node st,en;
int tx,ty;
int ff;
st.x=sx;
st.y=sy;
st.step=0;
while(!que.empty())
que.pop();
use[sx][sy]=1;
que.push(st);
while(!que.empty())
{
en=que.top();
que.pop();
if(en.x==0||en.x==h-1||en.y==0||en.y==w-1)
{
printf("%d\n",en.step+1);
return;
}
for(i=0;i<4;i++)
{
tx=en.x+dir[i][1];
ty=en.y+dir[i][0];
if(tx>=0 && tx<h && ty>=0&& ty<w && ma[tx][ty]!='#'&&use[tx][ty]==0)
{
use[tx][ty]=1;
if(ma[tx][ty]=='.')
st.step=en.step+1;
else if(ma[tx][ty]=='@')
st.step=en.step+d+1; //卧槽,我把这里的st写成en了。然后我错了一个下午,55555555
st.x=tx;
st.y=ty;
que.push(st);
}
}
}
}
int main()
{
int i,j;
int t;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d %d %d",&h,&w,&d);
for(i=0;i<h;i++)
{
for(j=0;j<w;j++)
{
cin>>ma[i][j];
if(ma[i][j]=='S')
{
sx=i;
sy=j;
ma[i][j]='.';
}
}
}
bfs();
}
}
}
- HDU4198-BFS+优先队列
- hdu4198-优先队列+bfs
- hdu4198(BFS+优先级队列)
- HDU4198 Quick out of the Harbour bfs+优先队列
- 优先队列+BFS
- hdu1242优先队列BFS
- acmdream1191 bfs+优先队列
- Hdu2822Dogs bfs+优先队列
- BFS+队列优先
- 优先队列+BFS
- hdu5040 优先队列+bfs
- HDU1026 bfs+优先队列
- hdu1071Nightmare(BFS+优先队列)
- HDU2026 BFS+优先队列
- HDOJ1026 优先队列bfs
- HDU1242 BFS+优先队列
- BFS-优先队列
- 优先队列+bfs 模板
- Java实现的二分查找算法
- JAVAWEB项目如何实现验证码
- 新闻发布系统,防火墙关了吗?
- SQL查询
- 1.cpp
- hdu4198-优先队列+bfs
- Android dex分包方案
- nginx反向代理批量实现https协议访问
- 主板
- JDBC工具类
- 网站常见错误代码解释
- iOS7 禁止某一个界面手势放回
- 消除类游戏案例:Sushi Crush(三)
- UML系列图--用例图