cf 877D Olya and Energy Drinks
来源:互联网 发布:java源码下载 编辑:程序博客网 时间:2024/06/08 10:14
一 原题
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 from1 tok 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.
二 分析
三 代码
#include<iostream>#include<string>#include<queue>using namespace std;#define PII pair<int, int>#define mp make_pairconst int maxn = 1005;const int inf = 1000 * 1000 + 5;const int dx[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int n, m, k, x1, y1, x2, y2, dis[maxn][maxn];string s[maxn];inline bool ok(int x, int y) {return (x >= 0) && (x < n) && (y >= 0) && (y < m) && (s[x][y] == '.');}void bfs() {queue<PII> q;q.push(mp(x1, y1));dis[x1][y1] = 0;while(!q.empty()) {int x = q.front().first;int y = q.front().second;q.pop();for(int i = 0; i < 4; i++) {for(int j = 1; j <= k; j++) {int nx = x + j * dx[i][0];int ny = y + j * dx[i][1];if(!ok(nx, ny) || dis[nx][ny] < dis[x][y] + 1) break;if(dis[nx][ny] == inf) dis[nx][ny] = dis[x][y] + 1, q.push(mp(nx, ny));}}}}int main() {ios::sync_with_stdio(false);cin >> n >> m >> k;for(int i = 0; i < n; i++)cin >> s[i];cin >> x1 >> y1 >> x2 >> y2;x1--; y1--; x2--; y2--;for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)dis[i][j] = inf;bfs();if(dis[x2][y2] == inf)cout << "-1" << endl;elsecout << dis[x2][y2] << endl;return 0;}
cf上Farhod_Farmon用set的解法:
#include <bits/stdc++.h>#define fi first#define se second#define fin(s) freopen( s, "r", stdin );#define fout(s) freopen( s, "w", stdout );const long long N = 1010;const long long Q = 100100;const long long mod = 998244353;const long long block = 500;using namespace std;int n, m, k;char c[N][N];set < int > a[N], b[N];queue < pair < int, pair < int, int > > > q;void add(int x, int y, int g){ q.push({g, {x, y}}); a[x].erase(y); b[y].erase(x);}void solve(){ cin >> n >> m >> k; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cin >> c[i][j]; a[i].insert(j); b[j].insert(i); } } int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; add(x1, y1, 0); while(!q.empty()){ int x = q.front().se.fi, y = q.front().se.se, f = q.front().fi; q.pop(); if(x == x2 && y == y2){ cout << f << "\n"; return; } auto it = a[x].lower_bound(y); while(it != a[x].end() && *it - y <= k && c[x][*it] == '.'){ add(x, *it, f + 1); it = a[x].lower_bound(y); } it = a[x].lower_bound(y); while(it != a[x].begin() && y - *(--it) <= k && c[x][*it] == '.'){ add(x, *it, f + 1); it = a[x].lower_bound(y); } it = b[y].lower_bound(x); while(it != b[y].end() && *it - x <= k && c[*it][y] == '.'){ add(*it, y, f + 1); it = b[y].lower_bound(x); } it = b[y].lower_bound(x); while(it != b[y].begin() && x - *(--it) <= k && c[*it][y] == '.'){ add(*it, y, f + 1); it = b[y].lower_bound(x); } } cout << -1 << "\n";}bool mtest = false; int main(){ ios_base::sync_with_stdio(0); int TE = 1; if(mtest) cin >> TE; while(TE--) solve(); return 0;}
- cf 877D Olya and Energy Drinks
- Codeforces 877 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 Round #877 (Div. 2) D. Olya and Energy Drinks
- [codeforces] 877D. Olya and Energy Drinks (BFS)
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks
- Codeforces Round #442 (Div. 2) 877 D. Olya and Energy Drinks BFS
- Codeforces 877D. Olya and Energy Drinks BFS+并查集
- codeforces #442 div2 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)-广搜&剪枝&技巧&好题-D. Olya and Energy Drinks
- Codeforces Round #442 (Div. 2) Olya and Energy Drinks(搜索 bfs 套路题)
- CodeForces 305D Olya and Graph
- CF D. Tanya and Password
- CF D. Fox And Jumping
- bzoj 3738: [Ontak2013]Kapitał
- Spring学习01
- python的pymysql
- FetchMode和FetchType
- Number of Islands
- cf 877D Olya and Energy Drinks
- 【大数据】开发环境搭建(四):各组件搭建步骤
- PHP中的单例模式
- linux 之 实时监控脚本
- spring-cloud 注册中心eureka环境搭建
- Mybatis中javaType和jdbcType对应关系
- 布尔表达式内部鹅腿悬赏查错
- 博客开通啦
- java序列化和反序列化