题目链接:http://codeforces.com/contest/877/problem/D


D. Olya and Energy Drinks

time limit per test2 seconds

memory limit per test256 megabytes

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 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.

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.

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.


解题心得:

  1. 就是一个bfs只不过加了几个剪枝,可以使用spfa,记录到达每一个点所用的最小的时间,如果走到该点大于了该点的最小的时间就跳过。
  2. 也可以写一个A*的算法,用优先队列,用当前点到达终点的曼哈顿距离和到达该点已经使用了的时间的和作为预算,先得到一个到达终点相对小的答案,如果之后的点大于了当前答案,直接跳过,如果比当前的答案更优则直接替换。

spfa:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
char maps[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
int dist[maxn][maxn];
struct node
{
int x,y;
} now,Next,aim;
queue <node> qu; bool checke(int x,int y,int n,int m)
{
if(x<0 || y<0 || x>=n || y>=m)
return false;
return true;
} void pre_maps(int n)
{
for(int i=0; i<n; i++)
scanf("%s",maps[i]);
} void BFS(int n,int m,int dis)
{
qu.push(now);
while(!qu.empty())
{
now = qu.front();
qu.pop();
if(vis[now.x][now.y])
continue;
else
vis[now.x][now.y] = true;
for(int i=0; i<4; i++)
{
for(int j=1; j<=dis; j++)
{
int x = now.x + dir[i][0]*j;
int y = now.y + dir[i][1]*j;
if(x<0 || y<0 || x>=n || y>=m)
break;
if(maps[x][y] == '#')
break;
if(dist[x][y] <= dist[now.x][now.y])
break;
else
dist[x][y] = dist[now.x][now.y] + 1;
if(vis[x][y])
break;
Next.x = x;
Next.y = y;
qu.push(Next);
}
}
}
} int main()
{
int n,m,dis;
scanf("%d%d%d",&n,&m,&dis);
pre_maps(n);
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
now.x = x1-1;
now.y = y1-1;
aim.x = x2-1;
aim.y = y2-1;
memset(dist,0x7f,sizeof(dist));
dist[now.x][now.y] = 0;
BFS(n,m,dis);
if(dist[aim.x][aim.y] == 0x7f7f7f7f)
printf("-1");
else
printf("%d",dist[aim.x][aim.y]);
}

A*(利用曼哈顿距离)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1100;
char maps[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
int n,m,k;
int ans = 0x7f7f7f7f;
struct node
{
int x,y,step,dis;
} now,aim,Next; bool operator < (const node &a,const node &b)
{
return a.dis>b.dis;
} void pre_maps()
{
for(int i=0; i<n; i++)
scanf("%s",maps[i]);
} bool check(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m || maps[x][y] == '#')
return true;
return false;
} void bfs()
{
priority_queue<node> qu;
qu.push(now);
vis[now.x][now.y] = true;
while(!qu.empty())
{
now = qu.top();
qu.pop();
for(int i=0; i<4; i++)
{
for(int j=1; j<=k; j++)
{
int x = now.x + dir[i][0]*j;
int y = now.y + dir[i][1]*j;
if(check(x,y))
break;
if(vis[x][y])
continue;
Next.x = x;
Next.y = y;
Next.step = now.step + 1;
if(x == aim.x && y == aim.y)
{
if(Next.step < ans)
ans = Next.step;
}
else
{
Next.dis = abs(x-aim.x) + abs(y-aim.y) + Next.step;
if(Next.dis > ans)
continue;
vis[x][y] = true;
qu.push(Next);
}
}
}
}
return ;
} int main()
{
scanf("%d%d%d",&n,&m,&k);
pre_maps();
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1--;
x2--;
y1--;
y2--;
if(x1 == x2 && y1 == y2)
{
printf("0");
return 0;
}
now.x = x1;
now.y = y1;
aim.x = x2;
aim.y = y2;
now.dis = abs(now.x-x2)+abs(now.y-y2);
now.step = 0;
bfs();
if(ans == 0x7f7f7f7f)
printf("-1");
else
printf("%d",ans);
return 0;
}

Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks的更多相关文章

  1. Codeforces Round #524 (Div. 2) D. Olya and magical square

    D. Olya and magical square 题目链接:https://codeforces.com/contest/1080/problem/D 题意: 给出一个边长为2n的正方形,每次可以 ...

  2. Codeforces Round #877 (Div. 2) E. Danil and a Part-time Job

    E. Danil and a Part-time Job 题目链接:http://codeforces.com/contest/877/problem/E time limit per test2 s ...

  3. Codeforces Round #877 (Div. 2) B. - Nikita and string

    题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...

  4. Codeforces 877 D. Olya and Energy Drinks

    http://codeforces.com/contest/877/problem/D   D. Olya and Energy Drinks time limit per test 2 second ...

  5. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  6. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  7. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  8. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  9. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

随机推荐

  1. springMVC-RESTful支持

    RESTful支持 什么是restful? Restful就是一个资源定位及资源操作的风格.不是标准也不是协议,只是一种风格,是对http协议的诠释. 资源定位:互联网所有的事物都是资源,要求url中 ...

  2. 使用express+mongoDB搭建多人博客 学习(6)发表文章

    发表文章 1.在modules文件夹下新建post.js var mongodb=require("./db"); function Post(name,title,post){ ...

  3. DiscuzX2.5密码错误次数过多,请 15 分钟后重新登录的修改办法

    source\function function_login.php $return = (!$login || (TIMESTAMP - $login['lastupdate'] > )) ? ...

  4. datetimepicker 插件位置问题解决经验

    使用dadetimepicker进行时间选择是个很不错的选择,但是美中不足的是该插件在chrome中显示弹框的时候有时会出现位置错位的现象,而在IE中则没有这种现象,视图如图1 图1 查阅了网上的资料 ...

  5. dos命令-环境变量-数据类型-命名规范

    JAVA第一天笔记--dos命令-环境变量-数据类型-命名规范 1.能够阐述JDK和JRE之间区别 JDK(Java Development Kit)是提供给开发人员使用的JAVA开发工具包(java ...

  6. 前端开发神器 - Brackets

    做了几年的 .Net 项目开发,后来公司转 Java 语言开发,Java 做了还没一年,公司准备前后端分离开发,而我被分到前端! Brackets是一款基于web(html+css+js)开发的web ...

  7. iOS --runtime理解

    iOS~runtime理解 Runtime是想要做好iOS开发,或者说是真正的深刻的掌握OC这门语言所必需理解的东西.最近在学习Runtime,有自己的一些心得,整理如下,一为 查阅方便二为 或许能给 ...

  8. ios 自定义消息提示框

    自定义提示框,3秒钟后自动消失.如上图显示效果. 提示框加载代码: - (void)viewDidLoad { [super viewDidLoad]; //将view背景颜色变更为黄色 self.v ...

  9. Android的开机流程及对应源码位置分析

    1.系统引导bootloader 1)源码:bootable/bootloader/* 2)说明:加电后,CPU将先执行bootloader程序,此处有三种选择 a)开机按Camera+Power启动 ...

  10. IOS开发之----详解在IOS后台执行

    文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后台运行一小段时间. 还有三种类型的可以运行在后以,1.音乐2.location 3.voip 文二 ...