题目链接: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. Jmeter----小技巧(3)

    1.关闭启动Jmeter时出现的面板 说明:启动Jmeter的同时往往会打开一个下图所示的面板,经常不小心关掉后,导致Jmeter停止运行,偶尔写了好久的脚本没来得及保存就没了,烦恼不已 怎样避免如上 ...

  2. Java内置锁和简单用法

    一.简单的锁知识 关于内置锁 Java具有通过synchronized关键字实现的内置锁,内置锁获得锁和释放锁是隐式的,进入synchronized修饰的代码就获得锁,走出相应的代码就释放锁. jav ...

  3. Mysql5.7安装错误处理与主从同步及!

    basedir=/iddbs/mysql-5.7.16 datadir=/iddbs/mysql5.7/data3306 一.自定义Mysql.5.7版本免编译安装: 1.Db-server1安装前期 ...

  4. Vue全家桶开发笔记

    state 中没有属性的情况下,新增属性不会触发mutations修改. 例: commit('change', { c: 3, d: 4, }); state: { test: { a: 1, b: ...

  5. js将数字转换为带有单位的中文表示

    好不容易找到了, 实测可行, 记录一下.  到时候调用方法   addChineseUnit ,  其他两个方法在addChineseUnit中有调用 /** * 为数字加上单位:万或亿 * * 例如 ...

  6. jQuery选择器之样式

    .attr()与.removeAttr() 每个元素都有一个或者多个特性,这些特性的用途就是给出相应元素或者其内容的附加信息.如:在img元素中,src就是元素的特性,用来标记图片的地址. 操作特性的 ...

  7. sqlite总结1

    I Shell下命令行程序CLP I .help II 命令的简写 .e = .quit .s .h = .help II 数据库管理 A 创建数据库 1 CREATE TABLE id_name(i ...

  8. iOS Block的本质(二)

    iOS Block的本质(二) 1. 介绍引入block本质 通过上一篇文章Block的本质(一)已经基本对block的底层结构有了基本的认识,block的底层就是__main_block_impl_ ...

  9. 【Web应用-Web作业】Web 作业无法直接运行 jar 文件

    问题描述 在经典管理门户中将直接压缩的 jar 文件打包为 zip 包,上传到 web 作业时报错. 解决方法 jar 文件的运行需要依托于 java 进程,所以在运行 jar 文件时,我们都会以格式 ...

  10. leecode 旋转数组

    描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋 ...