Codeforces 877 D. Olya and Energy Drinks
2 seconds
256 megabytes
standard input
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.
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.
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.
3 4 4
....
###.
....
1 1 3 1
3
3 4 1
....
###.
....
1 1 3 1
8
2 2 1
.#
#.
1 1 2 2
-1
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的更多相关文章
- 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 ...
- cf 442 D. Olya and Energy Drinks
cf 442 D. Olya and Energy Drinks(bfs) 题意: 给一张\(n \times m(n <= 1000,m <= 1000)\)的地图 给出一个起点和终点, ...
- Olya and Energy Drinks(bfs)
D. Olya and Energy Drinks time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 【Codeforces Round #442 (Div. 2) D】Olya and Energy Drinks
[链接] 我是链接,点我呀:) [题意] 给一张二维点格图,其中有一些点可以走,一些不可以走,你每次可以走1..k步,问你起点到终点的最短路. [题解] 不能之前访问过那个点就不访问了.->即k ...
- Codeforces 877 C. Slava and tanks
http://codeforces.com/problemset/problem/877/C C. Slava and tanks time limit per test 2 seconds me ...
- codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))
题目链接:http://codeforces.com/contest/877/problem/E 题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现. ...
- Codeforces Round #442 Div.2 A B C D E
A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...
- 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 ...
- [转]Speeding Up Websites With YSlow
本文转自:http://net.tutsplus.com/tutorials/other/speeding-up-websites-with-yslow/ We all know there are ...
随机推荐
- .NET WinForm下StatusStrip控件如何设置分隔线及部分子控件右对齐
ssInfo.LayoutStyle = ToolStripLayoutStyle.StackWithOverflow;//StatusStrip 控件 tsslUpdate.Alignment = ...
- /etc/tolmcat/Server.xml 实例说明
# 这是service类 <Service name="Catalina"> # 这是http连接器,响应用户请求 <Connector port=&qu ...
- Gulp插件笔记
初次接触Gulp是出于网页模块化的需要,用过之后发现这个任务管理工具有很多实用的插件,意外地好用,于是打算写下这篇笔记把用到的Gulp插件记录一下.至于想了解Gulp基本用法的同学可以去Gulp官网查 ...
- Mscomm控件安装问题 License information for TMSComm not found.
操作步骤: 1.打开delphi,菜单选择compoents->import Activex control,然后选择那个mscomm32.ocx安装即可. 2.注册MScomm控件 开始- ...
- [转帖]Kerberos简介
1. Kerberos简介 https://www.cnblogs.com/wukenaihe/p/3732141.html 1.1. 功能 一个安全认证协议 用tickets验证 避免本地保存密码 ...
- NodeJs 遍历文件夹内容 上传到服务器.并输出上传记录文件
var path = require('path'); var glob = require('glob') var fs = require('fs'); var Promise = require ...
- BZOJ 2143 飞飞侠(分层最短路)
飞飞国是一个N×M的矩形方阵,每个格子代表一个街区.然而飞飞国是没有交通工具的.飞飞侠完全靠地面的弹射装置来移动.每个街区都装有弹射装置.使用弹射装置是需要支付一定费用的.而且每个弹射装置都有自己的弹 ...
- DAY6-Python学习笔记
前记: 坚持写学习笔记今天是第六天了,今天事情有点多想起来还没写赶快补起来,学习Python已经快一个星期了,大部分的知识点已经跟着廖雪峰老师的教程了解了一下,由于自学能力不强还有很多知识点掌握不牢固 ...
- BZOJ2186 SDOI2008沙拉公主的困惑(数论)
由于n!是m!的倍数,而对于每个与m!互质且小于m!的数x,x+m!.x+2*m!……也与其互质,所以答案即为(n!/m!)*φ(m!). φ(m!)=m!*∏(1-1/pi).其中的pi即为1~m中 ...
- 点击--》java9 新特性 详解
引言: 点击-->java9 新特性 详解 点击-->java8 新特性 详解 正题: 1.局部变量var 将前端思想var关键字引入java后段,自动检测所属于类型,一种情况除外,不能为 ...