hdu 4198 Quick out of the Harbour
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4198
Quick out of the Harbour
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
bfs+优先队列。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<queue> 8 #include<map> 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::find; 13 using std::sort; 14 using std::map; 15 using std::pair; 16 using std::vector; 17 using std::multimap; 18 using std::priority_queue; 19 #define pb(e) push_back(e) 20 #define sz(c) (int)(c).size() 21 #define mp(a, b) make_pair(a, b) 22 #define all(c) (c).begin(), (c).end() 23 #define iter(c) decltype((c).begin()) 24 #define cls(arr,val) memset(arr,val,sizeof(arr)) 25 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 26 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 28 const int N = 510; 29 typedef unsigned long long ull; 30 bool vis[N][N]; 31 char G[N][N]; 32 int H, W, C, Sx, Sy, Dx, Dy; 33 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 }; 34 struct Node { 35 int x, y, s; 36 Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {} 37 inline bool operator<(const Node &a) const { 38 return s > a.s; 39 } 40 }; 41 int bfs() { 42 priority_queue<Node> q; 43 q.push(Node(Sx, Sy, 0)); 44 vis[Sx][Sy] = true; 45 while (!q.empty()) { 46 Node t = q.top(); q.pop(); 47 if (t.x == Dx && t.y == Dy) return t.s; 48 rep(i, 4) { 49 int x = t.x + dx[i], y = t.y + dy[i]; 50 if (x < 0 || x >= H || y < 0 || y >= W) continue; 51 if (G[x][y] == '#' || vis[x][y]) continue; 52 if (G[x][y] == '@') q.push(Node(x, y, t.s + C + 1)); 53 else if (G[x][y] == '.') q.push(Node(x, y, t.s + 1)); 54 vis[x][y] = true; 55 } 56 } 57 return -1; 58 } 59 int main() { 60 #ifdef LOCAL 61 freopen("in.txt", "r", stdin); 62 freopen("out.txt", "w+", stdout); 63 #endif 64 int t; 65 scanf("%d\n", &t); 66 while (t--) { 67 scanf("%d %d %d", &H, &W, &C); 68 rep(i, H) { 69 scanf("%s", G[i]); 70 rep(j, W) { 71 vis[i][j] = false; 72 if (G[i][j] == 'S') Sx = i, Sy = j; 73 if (i == H - 1 || i == 0 || j == 0 || j == W - 1) { 74 if (G[i][j] != '#') Dx = i, Dy = j; 75 } 76 } 77 } 78 printf("%d\n", bfs() + 1); 79 } 80 return 0; 81 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥