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. unix网络编程——I/O多路复用之epoll

    1. 基本概念 当程序进行IO时,如果数据尚未准备好,那么IO将处于阻塞状态.当某个进程有多个打开的文件,比如socket,那么其后的所有准备好读写的文件将受到阻塞的影响而不能操作.不借助线程,单一进 ...

  2. Navicat Premium 连接Oracle 数据库

    昨天开始工作的时候听同事说:Navicat可以连各种数据库,包括Oracle,头一次听说!!!很是尴尬.现在记录一下怎么用Navicat连接Oracle.最重要的是,Navicat只支持32的Orac ...

  3. Unity3D游戏开发——收集当前关卡游戏中分散的物件

    运用场景 许多游戏中会有一些供玩家拾起的物件,例如装备.血包.道具等.当玩家与物件进行碰撞后,则会进入仓库. 本篇介绍了简单的碰撞过程. 原理 基本的碰撞机制,用到OnTriggerEnter()碰撞 ...

  4. 旧文备份:CANopen协议中SDO服务

    SDO是服务数据对象接口(Service Data Obiect)的缩写,顾名思义提供服务数据的访问接口,服务数据就是一些实时性要求不高的数据,一般是指节点配置参数,因此,SDO一般用来配置和获得节点 ...

  5. WebDriver 工作原理

    WebDriver是W3C的一个标准,由Selenium主持. 具体的协议标准可以从http://code.google.com/p/selenium/wiki/JsonWireProtocol#Co ...

  6. dotnet core sdk 2.1 在centos下的安装

    1. 安装微软的仓库 rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm 2. 修改仓库 ...

  7. (转)web开发流程

    a.项目经理与公司决策层的沟通,以确定这个需求有没有足够的人手和可行性去实现,以及与现有产品的依存关系. b.公司决策层与市场/策划部门的交流,这个过程将进行的相当充分,并且是反复.长期的,它致力于从 ...

  8. 2013成都网赛1010 hdu 4737 A Bit Fun

    题意:定义f(i, j) = ai|ai+1|ai+2| ... | aj (| 指或运算),求有多少对f(i,j)<m.1 <= n <= 100000, 1 <= m &l ...

  9. 02.java并发编程之原子性操作

    一.原子性操作 1.ThreadLocal 不同线程操作同一个 ThreadLocal 对象执行各种操作而不会影响其他线程里的值 注意:虽然ThreadLocal很有用,但是它作为一种线程级别的全局变 ...

  10. 利用Eric+Qt Designer编写倒计时时钟

    [前言]前几日通过编写命令行通讯录,掌握了Python的基本语法结构,于是开始向更高水平冲击,利用Eric与Qt Designer 编写一个带界面的小程序.本次实操中也确实遇到了不少问题,通过学习也都 ...