Codeforces Round #442 (Div. 2) 877 D. Olya and Energy Drinks BFS
来源:互联网 发布:超级直播软件apk 编辑:程序博客网 时间:2024/05/29 04:04
题目链接: D. Olya and Energy Drinks
题目大意
一个n*m的迷宫,
思路
最普通的BFS, 但复杂度
代码
GNU C++14 Accepted 1747 ms 13600 KB
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <queue>using namespace std;const int maxn = 1e3 + 10;int n, m, k, d[maxn][maxn];int x_1, x_2, y_1, y_2;int dx[] = {0, 0, 1, -1};int dy[] = {1, -1, 0, 0};char s[maxn][maxn];typedef pair<int, int> P;int main(){ scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; ++i) scanf("%s", s[i]); scanf("%d%d%d%d", &x_1, &y_1, &x_2, &y_2); if(x_1 == x_2 && y_1 == y_2) { cout << 0 << endl; return 0; } --x_1, --x_2, --y_1, --y_2; memset(d, -1, sizeof(d)); d[x_1][y_1] = 0; queue<P> que; que.push(P(x_1, y_1)); while (!que.empty()) { P now = que.front(); que.pop(); for (int i = 0; i < 4; ++i) { for (int a = 1; a <= k; ++a) { int nx = now.first + dx[i] * a; int ny = now.second + dy[i] * a; if (0 <= nx && nx < n && 0 <= ny && ny < m && d[nx][ny] == -1 && s[nx][ny] == '.') { d[nx][ny] = d[now.first][now.second] + 1; que.push(P(nx, ny)); if (nx == x_2 && ny == y_2) { cout << d[x_2][y_2] << endl; return 0; } } else if (nx < 0 || nx >= n || ny < 0 || ny >= m || s[nx][ny] == '#') break; } } } cout << -1 << endl; return 0;}
阅读全文
0 0
- Codeforces Round #442 (Div. 2) 877 D. Olya and Energy Drinks BFS
- Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks
- 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) Olya and Energy Drinks(搜索 bfs 套路题)
- Codeforces Round #442 (Div. 2)-广搜&剪枝&技巧&好题-D. Olya and Energy Drinks
- Codeforces 877D Olya and Energy Drinks【思维优化Bfs】
- Codeforces 877 D. Olya and Energy Drinks (bfs)
- codeforces 877 problem D Olya and Energy Drinks 【bfs剪枝】
- [codeforces] 877D. Olya and Energy Drinks (BFS)
- Codeforces 877 D Olya and Energy Drinks
- Codeforces 877D. Olya and Energy Drinks BFS+并查集
- cf 877D Olya and Energy Drinks
- codeforces #442 div2 Olya and Energy Drinks
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth (BFS)
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth(BFS)
- Codeforces Round #325 (Div. 2) D bfs
- 每天一个linux命令(16):which命令
- 10.25JAVA学习
- django学习——用django-pure-pagination进行分页
- servlet的自动加载和生命周期
- 1-4·Linux中搜索命令有哪些?
- Codeforces Round #442 (Div. 2) 877 D. Olya and Energy Drinks BFS
- npm获取配置有6种方式,优先级由高到底。
- c# 控制台程序屏蔽窗口
- unity总结九
- Hibernate工具类Utils
- 百度开源项目推荐
- Linux命令行大全总结(下)
- 编译---链接过程
- 安卓组件---Notification 的使用