Description

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.
 

Input

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.
 

Output

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.
 

Sample Input

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

Sample Output

16
11
 

Source

BAPC 2011

题意:求走出地图的最短时间,'#'不能走,'.'耗时一,'@'耗时K+1

思路:在BFS的基础上加上优先队列

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 510; struct Point {
int x, y, step;
bool operator< (Point const &a) const {
return step > a.step;
}
} st, ed;
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int dx[4]={1, -1, 0, 0};
int dy[4]={0, 0, 1, -1};
int n, m, k; void bfs() {
memset(vis, 0, sizeof(vis));
priority_queue<Point> q;
vis[st.x][st.y] = 1;
q.push(st);
while (!q.empty()) {
Point cur = q.top();
q.pop();
if (cur.x == 0 || cur.x == n-1 || cur.y == 0 || cur.y == m-1) {
printf("%d\n", cur.step+1);
return;
}
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (vis[nx][ny] || map[nx][ny] == '#')
continue;
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
vis[nx][ny] = 1;
Point tmp;
if (map[nx][ny] == '.') {
tmp.x = nx, tmp.y = ny;
tmp.step = cur.step + 1;
}
else if (map[nx][ny] == '@') {
tmp.x = nx, tmp.y = ny;
tmp.step = cur.step + k + 1;
}
q.push(tmp);
}
}
}
} int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++) {
scanf("%s", map[i]);
for (int j = 0; j < m; j++) {
if (map[i][j] == 'S') {
map[i][j] = '.';
st.x = i, st.y = j, st.step = 0;
}
}
}
bfs(); }
return 0;
}

最新文章

  1. Object-C内存管理-对象引用计数的特例
  2. JS 截取字符串函数
  3. ENVI/IDL中.sav和.pro文件用法的不同
  4. PHP+mysql统计排名第几位
  5. 重操JS旧业第五弹:函数
  6. 文档在线预览开源实现方案三:OpenOffice + PDFRenderer + js
  7. 转 SQL 基础--&gt; NEW_VALUE 的使用
  8. 实现UDP高效接收/响应
  9. DOM4J使用简介
  10. Microsoft Dynamics CRM 2013/2015 选项集的多选
  11. springMVC(2)---获取前段数据
  12. CentOS 7配置MariaDB允许指定IP远程连接数据库
  13. 2018-11-06 Visual Studio Code插件-英汉词典初版发布
  14. [Android] Android利用Coordinatorlayout+AppbarLayout实现折叠式布局
  15. 获取日期Date
  16. PrimeNG之TreeTable
  17. React-Router4.x中文文档
  18. echarts之简单的入门——【一】做个带时间轴的柱状统计图
  19. ubuntu服务器安装FTP服务
  20. fl包下的TransitionManager的使用

热门文章

  1. 升级Https前的可行性验证(一)
  2. android adb源码分析(1)
  3. 性能优化 BlockCanary 卡顿监测 MD
  4. 【Spark】Spark Streaming + Kafka direct 的 offset 存入Zookeeper并重用
  5. 用条件随机场CRF进行字标注中文分词(Python实现)
  6. Android利用RecognizerIntent识别语音并简单实现打电话动作
  7. 【矩阵乘】【DP】【codevs 1305】Freda的道路
  8. Sqlite的多表连接更新
  9. MFC对话框:模态对话框及其弹出过程
  10. STL 笔记(四) 迭代器 iterator