Codeforces Round #442(Div.2)Problem D Olya and Energy Drinks(BFS)
来源:互联网 发布:surf算法 编辑:程序博客网 时间:2024/05/21 06:25
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n × m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
The first line contains three integers n, m and k (1 ≤ n, m, k ≤ 1000) — the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m) — the coordinates of the first and the last cells.
Print a single integer — the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
3 4 4....###.....1 1 3 1
3
3 4 1....###.....1 1 3 1
8
2 2 1.##.1 1 2 2
-1
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad.
【思路】
显然要用广度优先搜索的方式来做这个题目。在广搜中所有节点都是被分层次访问的,在这道题中距离刚好等于层次,所以每个节点被访问的第一次就已经可以得到起点到它的最短距离,故它只需入队一次去更新子节点即可,所以写法有些类似于SPFA。而且注意剪枝的情况:当到达某个点P的dist已经小于起点到当前点再到它的距距离,那么意味着当前点沿着P点方向的其他点不必入队,因为它们的dist都小于或等于从当前点到它们,如果是小于则可以交给下一层处理,如果是等于则不必再询问这个方向上的剩下点,这样就可以减少大量不必要的询问和更新。
【代码】
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define x first#define y secondusing namespace std;const int MAXN = 1005, INF =0x3f3f3f3f;const int DIR[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};int n, m, k, x1, y1, x2, y2;char mp[MAXN][MAXN];int dist[MAXN][MAXN];bool in[MAXN][MAXN];queue<pair<int, int> > q;bool ok(int x, int y){ return (1 <= x && x <= n && 1 <= y && y <= m);}void bfs(){ memset(in, false, sizeof(in)); pair<int, int> st, u, v; st.x = x1; st.y = y1; q.push(st); in[x1][y1] = true; while (!q.empty()) { u = q.front(); q.pop(); in[u.x][u.y] = false; for (int d = 0; d <= 3; d++) { v = u; for (int i = 1; i <= k; i++) { v.x += DIR[d][0]; v.y += DIR[d][1]; if (ok(v.x, v.y) && mp[v.x][v.y] == '.' && dist[v.x][v.y] >= dist[u.x][u.y] + 1) { dist[v.x][v.y] = dist[u.x][u.y] + 1; if (!in[v.x][v.y]) {q.push(v); in[v.x][v.y] = true;} } else break; } } }}int main(){ scanf("%d %d %d", &n, &m, &k); char buff[MAXN]; for (int i = 1; i <= n; i++) { scanf("%s", buff); for (int j = 1; j <= m; j++) mp[i][j] = buff[j - 1]; } scanf("%d %d %d %d", &x1, &y1, &x2, &y2); memset(dist, 0x3f, sizeof(dist)); dist[x1][y1] = 0; bfs(); if (dist[x2][y2] == INF) printf("-1"); else printf("%d", dist[x2][y2]); return 0;}
- Codeforces Round #442(Div.2)Problem D Olya and Energy Drinks(BFS)
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks (bfs)
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks
- Codeforces Round #442 (Div. 2) Olya and Energy Drinks(搜索 bfs 套路题)
- Codeforces Round #442 (Div. 2) 877 D. Olya and Energy Drinks BFS
- Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks
- Codeforces 877 D. Olya and Energy Drinks (bfs)
- codeforces 877 problem D Olya and Energy Drinks 【bfs剪枝】
- Codeforces Round #442 (Div. 2)-广搜&剪枝&技巧&好题-D. Olya and Energy Drinks
- Codeforces 877D Olya and Energy Drinks【思维优化Bfs】
- [codeforces] 877D. Olya and Energy Drinks (BFS)
- Codeforces 877 D Olya and Energy Drinks
- codeforces #442 div2 Olya and Energy Drinks
- Codeforces 877D. Olya and Energy Drinks BFS+并查集
- cf 877D Olya and Energy Drinks
- D. Arthur and Walls(Codeforces Round #297 (Div. 2) 搜索(BFS))
- Codeforces Round #297 (Div. 2) D Arthur and Walls(bfs)
- Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)
- C语言内存分配管理常见bug
- 【git基础】git简易入门
- (通用)深度学习环境搭建:tensorflow安装教程及常见错误解决
- Windows_jdk环境配置解释
- Raw_Socket原始套接字
- Codeforces Round #442(Div.2)Problem D Olya and Energy Drinks(BFS)
- 2017.10.24
- KVM(桥接模式)宿主机创建网桥
- hdu 4135(容斥原理)
- 关于指针的问题
- Java设计模式——结构型
- 分享Ctrlbox项目管理软件的使用心得
- 集成学习
- 03-天亮大数据系列教程之分布式计算框架Map/Reduce运行流程与应用