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. 树莓派配置RTC时钟(DS3231,I2C接口)

    1.购买基于DS3231的RTC时钟模块,并且支持3.3V的那种 2.配置树莓派 a.打开树莓派的i2c接口 sudo raspi-config -->Interfacing Options - ...

  2. 基于MongoDB2.6版本配置MongoDB主从复制集群架构

    1:集群环境说明:mongodb1:192.168.43.10.mongodb2:192.168.43.11.mongodb3:192.168.43.12.且基于主机名相互通信/etc/hosts文件 ...

  3. fx投影效果分离

    虽然忙着花花二期三期bug.bug  ing,修改等待中突然看见一张logo.文字下面的阴影图,如下,就满脑子在想阴影到底咋做的.. 七拼八凑的尝试后大体样子是有,终究没有上图那种字体轮廓的阴影... ...

  4. 第七周PSP&进度条

    团队项目PSP 一.表格:     C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 讨论 讨论beta阶段任务 10:00 12:30 28 270 ...

  5. beta发布的评论

    1. 组名:飞天小女警 项目名:礼物挑选小工具 评价:对于我们学生来说,选礼物小工具相对来说新颖,小组添加了前十名热门礼物的推荐.发布到服务器上了,未来也有很多选择的空间可以做成礼物挑选手机app,也 ...

  6. sql server数据库中raiserror函数的用法

    server数据库中raiserror的作用就和asp.NET中的throw new Exception一样,用于抛出一个异常或错误.这个错误可以被程序捕捉到. raiserror的常用格式如下:ra ...

  7. 【SQLSERVER】动态游标的实现

    方法1: CREATE   TABLE   #tabTmp(id   int)    INSERT   #tabTmp   EXECUTE('SELECT   id   FROM   '+@Table ...

  8. Spring、SpringMVC、MyBatis整合

    项目结构: 新建web项目:File->new->Dynamic Web Project 一.准备所需jar包1. Spring框架的jar包 spring-framework-5.0.4 ...

  9. HNOI/AHOI2018题解

    作为一名高二老年选手来补一下我省去年的省选题. D1T1:寻宝游戏 按顺序给出\(n\)个\(m\)位的二进制数\(a_i\),再在最前方添一个\(0\), 给出\(q\)次询问,每次询问给出一个同样 ...

  10. 如何合理的规划jvm性能调优

    JVM性能调优涉及到方方面面的取舍,往往是牵一发而动全身,需要全盘考虑各方面的影响.但也有一些基础的理论和原则,理解这些理论并遵循这些原则会让你的性能调优任务将会更加轻松.为了更好的理解本篇所介绍的内 ...