D. Olya and Energy Drinks
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nm 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
output
3
input
3 4 1
....
###.
....
1 1 3 1
output
8
input
2 2 1
.#
#.
1 1 2 2
output
-1
Note

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.

题意:

n*m 网格,每秒走1——k步

不能走‘#’

从指定位置走到目标位置所需的最短时间

bfs

用并查集标记每个点上下左右第一个没有到过的点

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define turn(i,j) ((i)-1)*m+(j) const int INF=0x3f3f3f3f; int n,m,k,sx,sy,ex,ey,dis[][];
int quex[],quey[]; int faU[],faD[],faL[],faR[]; char map[][]; int find(int fa[],int i)
{
return fa[i]==i ? i : fa[i]=find(fa,fa[i]);
} void unionn(int i,int j)
{
int k=turn(i,j);
if(i!=) faU[k]=find(faU,turn(i-,j));
else faU[k]=;
if(i!=n) faD[k]=find(faD,turn(i+,j));
else faD[k]=;
if(j!=) faL[k]=find(faL,turn(i,j-));
else faL[k]=;
if(j!=m) faR[k]=find(faR,turn(i,j+));
else faR[k]=;
} int bfs()
{
for(int i=;i<=n;i++)
for(int v=;v<=m;v++)
dis[i][v]=INF;
if(map[sx][sy]=='#'||map[ex][ey]=='#')
return -;
dis[sx][sy]=;
int h=,tail=;
quex[h]=sx;
quey[h]=sy;
unionn(sx,sy);
int nowx,nowy;
while(h<tail)
{
nowx=quex[h];
nowy=quey[h++];
for(int i=,x=nowx,y=nowy;i<=k;i++)
{
y=find(faR,turn(x,y));
if(!y) break;
y%=m; if(!y) y=m;
if(y-nowy>k) break;
if(map[x][y]=='#') break;
dis[x][y]=dis[nowx][nowy]+;
unionn(x,y);
quex[tail]=x;
quey[tail++]=y;
}
for(int i=,x=nowx,y=nowy;i<=k;i++)
{
y=find(faL,turn(x,y));
if(!y) break;
y%=m; if(!y) y=m;
if(nowy-y>k) break;
if(map[x][y]=='#') break;
dis[x][y]=dis[nowx][nowy]+;
unionn(x,y);
quex[tail]=x;
quey[tail++]=y;
}
for(int i=,x=nowx,y=nowy;i<=k;i++)
{
x=find(faD,turn(x,y));
if(!x) break;
x=(x-)/m+;
if(x-nowx>k) break;
if(map[x][y]=='#') break;
if(dis[x][y]!=INF) continue;
dis[x][y]=dis[nowx][nowy]+;
unionn(x,y);
quex[tail]=x;
quey[tail++]=y;
}
for(int i=,x=nowx,y=nowy;i<=k;i++)
{
x=find(faU,turn(x,y));
if(!x) break;
x=(x-)/m+;
if(nowx-x>k) break;
if(map[x][y]=='#') break;
if(dis[x][y]!=INF) continue;
dis[x][y]=dis[nowx][nowy]+;
unionn(x,y);
quex[tail]=x;
quey[tail++]=y;
}
}
if(dis[ex][ey]==INF)
return -;
return dis[ex][ey];
} int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
scanf("%s",map[i]+);
int k;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
k=turn(i,j);
faU[k]=faD[k]=faL[k]=faR[k]=k;
}
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
std::cout<<bfs();
return ;
}

Codeforces 877 D. Olya and Energy Drinks的更多相关文章

  1. Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks

    题目链接:http://codeforces.com/contest/877/problem/D D. Olya and Energy Drinks time limit per test2 seco ...

  2. cf 442 D. Olya and Energy Drinks

    cf 442 D. Olya and Energy Drinks(bfs) 题意: 给一张\(n \times m(n <= 1000,m <= 1000)\)的地图 给出一个起点和终点, ...

  3. Olya and Energy Drinks(bfs)

    D. Olya and Energy Drinks time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. 【Codeforces Round #442 (Div. 2) D】Olya and Energy Drinks

    [链接] 我是链接,点我呀:) [题意] 给一张二维点格图,其中有一些点可以走,一些不可以走,你每次可以走1..k步,问你起点到终点的最短路. [题解] 不能之前访问过那个点就不访问了.->即k ...

  5. Codeforces 877 C. Slava and tanks

    http://codeforces.com/problemset/problem/877/C   C. Slava and tanks time limit per test 2 seconds me ...

  6. codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))

    题目链接:http://codeforces.com/contest/877/problem/E 题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现. ...

  7. Codeforces Round #442 Div.2 A B C D E

    A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...

  8. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  9. [转]Speeding Up Websites With YSlow

    本文转自:http://net.tutsplus.com/tutorials/other/speeding-up-websites-with-yslow/ We all know there are ...

随机推荐

  1. java中方法传入参数时:值传递还是址传递?

    JAVA中的数据类型有两大类型: ① 基本数据类型:逻辑型(boolean).文本型(char).整数型(byte.short.int.long).浮点型(float.double) ② 引用数据类型 ...

  2. BETA-7

    前言 我们居然又冲刺了·七 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 经过分析发现,为提升速率估测的可靠性,目前最具可改造性的参数为帧间间隔,调用参 ...

  3. go 面试题总结

    1.什么是goroutine,他与process, thread有什么区别? 2. 什么是channel,为什么它可以做到线程安全? 3. 了解读写锁吗,原理是什么样的,为什么可以做到? 4. 如何用 ...

  4. 微信小程序 功能函数 定时震动

    ffn: function () { let nnn = this.data.nnn nnn += 1; this.setData({ nnn: nnn }); if (nnn > 10) { ...

  5. MFC各种属性设置

    在使用MFC的时候经常需要对例如对话框的外观进行一些设置.MFC哪些属性的含义和设置可以参照博客: http://www.cnblogs.com/lzmfywz/archive/2012/04/20/ ...

  6. javascript中对象访问自身属性的方式

    在javascript中,通过对象的方法访问对象自身属性时,必须采用this.fieldName的方式. 原因是javascript中Function是无状态的,访问对象的属性时,必须指定当前的上下文 ...

  7. FlatBuffers初探

    我第一次知道FlatBuffers是因为Facebook写的这篇Android的技术博客文章.它主要介绍了FlatBuffers对比JSON的优势,以及Facebook Android App应用了F ...

  8. Qt——容器类(译)

    注:本文是我对Qt官方文档的翻译,错误之处还请指正. 原文链接:Container Classes 介绍 Qt库提供了一套通用的基于模板的容器类,可以用这些类存储指定类型的项.比如,你需要一个大小可变 ...

  9. Eclipse中设置作者、日期等的方式

    1.点击Windows->Preferences->Java->Code Style->Code Templates: 2.点击展开右侧的Comments选项卡,里面的选项对应 ...

  10. Jmeter—添加断言 判断接口响应数据是否符合预期

    发出请求之后,通过添加断言可以判断响应数据是否是我们的预期结果. 1 在Jmeter中发送一个状态返回200的http请求(参数故意输入错误).结果肯定是不是返回200啦. 但结果树中http请求的图 ...