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. 谈对“Git”的认识与理解

    自诞生于2005年以来,Git日臻完善,在高度易用的同时,仍然保留着初期设定的目标.它的速度飞快,及其适合管理大项目,它还有着令人难以置信的非线性分支管理系统,可以应付各种复杂的项目开发需求.接着说说 ...

  2. 新的Calculator的规范作业

    附加作业题目 第三次作业 mygithub:sonnypp 这是开学来第一次写随笔,这一次的作业是对上一次作业的修改,对于上一次作业,在学长老师的帮助下,我重新修改了下代码,将.h文件分成了一个Sca ...

  3. Week2-作业1:阅读与博客

    Week2-作业1:阅读与博客 第一章 :概论 1. 原文如下: 移山公司程序员阿超的宝贝儿子上了小学二年级,老师让家长每天出30道加减法题目给孩子做.阿超想写一个小程序来做这件事,具体实现可以采用很 ...

  4. 项目冲刺Beta第一篇博客

    Beta版本冲刺计划安排 1.当天站立式会议照片: 2.工作分工: 团队成员 分工 张洪滨060  排行榜界面美化 陈敬轩059  注册成功界面美化 黄兴067  登录界面美化 林国梽068  答题界 ...

  5. Java 使用 dom4j 读取 xml文档 demo

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://www. ...

  6. There is no getter for property named 'notice' in 'class com.game.domain.Notices'

    在插入数据时报错:There is no getter for property named 'notice' in 'class com.game.domain.Notices' 四月 11, 20 ...

  7. Python 字节码是什么

    了解 Python 字节码是什么,Python 如何使用它来执行你的代码,以及知道它是如何帮到你的. 如果你曾经编写过 Python,或者只是使用过 Python,你或许经常会看到 Python 源代 ...

  8. BZOJ5101 POI2018Powódź(并查集)

    如果某个格子的积水量超过了该格子的某个挡板高度,那么挡板另一端的积水量就会与其相同.看起来是一个不断合并的过程,考虑并查集.枚举深度,维护每个连通块内的方案数,深度超过某挡板高度时,将两端的连通块合并 ...

  9. P2605 [ZJOI2010]基站选址

    题目描述 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄不超过Si的范 ...

  10. ReentrantLock详解 以及与synchronized的区别

    ReentrantLock lock = new ReentrantLock(); //参数默认false,不公平锁 ReentrantLock lock = new ReentrantLock(tr ...