hdoj_4198Quick out of the Harbour
来源:互联网 发布:程序员和产品经理 编辑:程序博客网 时间:2024/05/21 15:46
Quick out of the Harbour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 692 Accepted Submission(s): 306
Problem 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.
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.
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
26 5 7######S..##@#.##...##@####.###4 5 3######S#.##@..####@#
Sample Output
1611
#include <cstring>#include <queue>#include <cstdio>#include <iostream>using namespace std;#pragma warning(disable : 4996)const int MAXN = 505;char maps[MAXN][MAXN];bool visited[MAXN][MAXN];int moves[4][2] = {-1,0,1,0,0,-1,0,1};int m, n, d;typedef struct Point{int x;int y;int step;bool operator < (const Point &p) const{return step > p.step;}}Point;priority_queue<Point>Q;int BFS(int x, int y){while(!Q.empty()){Q.pop();}Point pre, next;pre.x = x;pre.y = y;pre.step = 0;Q.push(pre);visited[pre.x][pre.y] = true;while(!Q.empty()){pre = Q.top();Q.pop();if(pre.x == 0 || pre.y == 0 || pre.x == n - 1 || pre.y == m - 1) {pre.step++;return pre.step;}for(int i = 0; i < 4; i++){next.x = pre.x + moves[i][0];next.y = pre.y + moves[i][1];if(next.x >= 0 &&next.x < n && next.y >= 0 && next.y < m && !visited[next.x][next.y] && maps[next.x][next.y] != '#'){visited[next.x][next.y] = true;if(maps[next.x][next.y] == '.'){next.step = pre.step + 1;}else{next.step = pre.step + d + 1;}Q.push(next);}}}return 0;}int main(){freopen("in.txt","r",stdin);int t, x, y;scanf("%d", &t);while (t--){scanf("%d %d %d", &n, &m, &d);for (int i = 0; i < n; i++){scanf("%s", maps[i]);}for (int i = 0 ; i < n; i++){for(int j = 0; j < m; j++){if(maps[i][j] == 'S'){x = i;y = j;break;}}}memset(visited, false, sizeof(visited));printf("%d\n", BFS(x, y));}return 0;}
- hdoj_4198Quick out of the Harbour
- hdoj 4198 Quick out of the Harbour
- hdu 4198 Quick out of the Harbour
- hdu 4198 Quick out of the Harbour
- hdu 4198 Quick out of the Harbour
- hdu 4198 Quick out of the Harbour
- hdu 【4198】Quick out of the Harbour
- HDU 4198Quick out of the Harbour
- HDU 4198 Quick out of the Harbour (优先队列BFS)
- hdoj 4198 Quick out of the Harbour 优先队列 + bfs
- HDU 4198 Quick out of the Harbour (bfs)
- HDU 4198 Quick out of the Harbour(优先队列)
- HDU - 4198 Quick out of the Harbour (BFS+优先队列)
- HDU_4198_Quick out of the Harbour(优先队列BFS)
- hdu4198——Quick out of the Harbour
- HDOJ 4198 Quick out of the Harbour(BFS)
- hrbust 1434 Quick out of the Harbour【Bfs+优先队列】
- HDU4198 Quick out of the Harbour bfs+优先队列
- MongoDB分片实战(三):性能和优化
- JAVA开发JDBC连接数据库
- android面试题
- 模板编译模型
- HTML之Position用法
- hdoj_4198Quick out of the Harbour
- android开发之AsyncTask的用法
- vb中产生随机数经典实例分析
- 应用程序发布更新网站,更新单个文件
- 图的邻接矩阵和邻接表
- [Json]json-lib简单处理java对象变为json对象
- linux Makefile configure里面 += := -= 这些符号的意思?
- jquery.validate.js表单验证
- Linux下JDK中文字体乱码