您的位置:首页 > 产品设计 > UI/UE

hdu 4198 Quick out of the Harbour

2014-09-24 22:48 417 查看

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1259 Accepted Submission(s): 507



[align=left]Problem Description[/align]
Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect and repair the ship. Now, a few days later, the pirates are getting landsick(Pirates get landsick when they don't get enough of the ships' rocking
motion. That's why pirates often try to simulate that motion by drinking rum.). Before all of the pirates become too sick to row the boat out of the harbour, captain Clearbeard decided to leave the harbour as quickly as possible.

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.

[align=left]Input[/align]
The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:

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.

[align=left]Output[/align]
For every test case in the input, the output should contain one integer on a single line: the travelling time of the fastest route to open sea. There is always a route to open sea. Note that the open sea is not shown on the map, so
you need to move outside of the map to reach open sea.

[align=left]Sample Input[/align]

2
6 5 7
#####
#S..#
#@#.#
#...#
#@###
#.###
4 5 3
#####
#S#.#
#@..#
###@#


[align=left]Sample Output[/align]

16
11

题意:给出一个矩阵h*w,起始点是"S","#"不能走,且矩阵四周用#围起来,边界不是"#"的地方代表出口,遇到“.”的话时间加一,遇到“@”的话时间加(d+1),求从起点离开这个矩阵的最小时间。

思路:bfs + 优先队列

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int d[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int map[51][51][51],A,B,C;
struct node
{
int x,y,z,step;
};
int bfs()
{
node a,c,nc;
a.x=a.y=a.z=a.step=0;
queue <node> q;
q.push(a);
while(!q.empty())
{
c=q.front();
q.pop();
map[c.x][c.y][c.z]=-1;
if(c.x==A-1 && c.y==B-1 && c.z==C-1)   return c.step;
for(int i=0;i<6;i++)
{
nc.x=c.x+d[i][0];
nc.y=c.y+d[i][1];
nc.z=c.z+d[i][2];
nc.step=c.step+1;
if(nc.x>=0 && nc.x<A && nc.y>=0 && nc.y<B && nc.z>=0 && nc.z<C && map[nc.x][nc.y][nc.z]==0)
{
q.push(nc);
map[nc.x][nc.y][nc.z]=-1;
}
}
}
return -1;
}
int main()
{
int N,time;
scanf("%d",&N);
while(N--)
{
scanf("%d%d%d%d",&A,&B,&C,&time);
for(int i=0;i<A;i++)
for(int j=0;j<B;j++)
for(int k=0;k<C;k++)
scanf("%d",&map[i][j][k]);
int min=bfs();
if(min<=time)  printf("%d\n",min);
else printf("-1\n");
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: