2015
05-23

# Quick out of the Harbour

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.

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.

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.

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

16
11

BFS时使用优先级队列

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

const int maxn = 505;
int cas, h, w, d;
int sx, sy, tx, ty;
char map[maxn][maxn];
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

struct Pos
{
int x, y, t;
friend bool operator < (Pos p1, Pos p2)
{
return p1.t > p2.t;
}
};

bool inMap(int x, int y)
{
return (x >= 0 && x < h && y >= 0 && y < w);
}

int bfs()
{
priority_queue<Pos> Q;
Pos cur, nxt;
cur.x = sx; cur.y = sy; cur.t = 0;
Q.push(cur);
map[sx][sy] = '#';

while (!Q.empty())
{
cur = Q.top();
Q.pop();
if (cur.x == tx && cur.y == ty)
return cur.t + 1;
for (int i = 0; i < 4; ++i)
{
nxt.x = cur.x + dir[i][0];
nxt.y = cur.y + dir[i][1];
if (inMap(nxt.x, nxt.y) && map[nxt.x][nxt.y] != '#')
{
if (map[nxt.x][nxt.y] == '.')
nxt.t = cur.t + 1;
else nxt.t = cur.t + d + 1;
map[nxt.x][nxt.y] = '#';
Q.push(nxt);
}
}
}
}

int main()
{
scanf("%d", &cas);
while (cas--)
{
scanf("%d %d %d", &h, &w, &d);
for (int i = 0; i < h; ++i)
scanf("%s", map[i]);
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; ++j)
{
if (map[i][j] == 'S')
sx = i, sy = j;
if (i == 0 || i == h - 1 || j == 0 || j == w - 1)
if (map[i][j] != '#')
tx = i, ty = j;
}
}
printf("%d\n", bfs());
}
return 0;
}