Codeforces 877 D. Olya and Energy Drinks (bfs)
来源:互联网 发布:大连市优化营商环境 编辑:程序博客网 时间:2024/05/17 01:38
Description
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.
Input
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.
Output
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.
Examples input
3 4 4....###.....1 1 3 1
Examples output
3
题意
从
思路
每次把可以走到的点加入队列 bfs 即可,注意优化剪枝,如同一个点只计算一次。
AC 代码
#include<bits/stdc++.h>#define IO ios::sync_with_stdio(false);\ cin.tie(0);\ cout.tie(0);#define inf 0x7f7f7f7fusing namespace std;typedef __int64 LL;const int maxm = 1e3+10;const int maxn = 1e7+10;const int mv[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};struct node{ int x; int y; int t; node() {} node(int xx,int yy,int tt) { x = xx; y = yy; t = tt; }} g[maxn];char c[maxm][maxm];int f[maxm][maxm];int n,m,k;int x1,x2,y1,y2;queue<node>que;void init(){ for(int j=0; j<4; j++) for(int i=1; i<=k; i++) { int xx = x1+mv[j][0]*i; int yy = y1+mv[j][1]*i; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&c[xx][yy]=='.') { f[xx][yy]=1; que.push(node(xx,yy,1)); } else break; }}void bfs(){ if(x1==x2&&y1==y2) { puts("0"); return; } init(); while(!que.empty()) { node s=que.front(); que.pop(); if(f[x2][y2]) { printf("%d\n",f[x2][y2]); return; } for(int j=0; j<4; j++) for(int i=1; i<=k; i++) { int xx = s.x+mv[j][0]*i; int yy = s.y+mv[j][1]*i; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&c[xx][yy]=='.') { if(f[xx][yy]==0) { f[xx][yy]=s.t+1; que.push(node(xx,yy,s.t+1)); } } else break; } } puts("-1");}int main(){ scanf("%d%d%d%*c",&n,&m,&k); for(int i=1; i<=n; i++) gets(c[i]+1); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); bfs(); return 0;}
- Codeforces 877 D. Olya and Energy Drinks (bfs)
- Codeforces 877D 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 Round #877 (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 Round #442(Div.2)Problem D Olya and Energy Drinks(BFS)
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks (bfs)
- cf 877D Olya and Energy Drinks
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks
- Codeforces Round #442 (Div. 2) Olya and Energy Drinks(搜索 bfs 套路题)
- codeforces #442 div2 Olya and Energy Drinks
- Codeforces Round #442 (Div. 2)-广搜&剪枝&技巧&好题-D. Olya and Energy Drinks
- CodeForces 305D Olya and Graph
- CodeForces 525D D. Arthur and Walls(BFS)
- CodeForces - 525D Arthur and Walls(bfs)
- banner的使用_
- Android关于OOM的解决方案
- 简单工厂模式的基本运用
- Java数据结构与算法解析(八)——伸展树
- 社交网络图挖掘5--图的邻居性质
- Codeforces 877 D. Olya and Energy Drinks (bfs)
- 从零开始实现主成分分析(PCA)算法
- android中图片水印换行
- Java数据结构与算法解析(九)——B树
- ASP.NET WebService如何去掉asmx后缀
- 对map集合进行排序
- 网络架构技术演化之主流架构介绍(转自阿里巴巴技术官网)
- libxmljs, v8引擎的libxml绑定
- 给定俩个字符串,只包含二进制。求字符串的和