题目链接: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. #10:wannanewtry——6

    HDU3401,列完转移方程拆分一下,正着.反着跑优先队列优化代表买或卖.初始化不大会搞…… #include <bits/stdc++.h> using namespace std; c ...

  2. 在 Linux 环境直接复移动硬盘上的 GRUB

    手头有一块用了 10 年的旧移动硬盘,其中安装了 Debian 系统,从低版本一直升级到现在的 9 已经用了很长时间.前不久正连着那块硬盘跑着 Debian 修改文件的时候,由于一个本可避免的意外震动 ...

  3. Crusher Django 学习笔记2 基本url配置

    http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial2-conf-basic-url.html 顺便学习一下FQ Cr ...

  4. 02.Javascript——入门一些方法记录之Object

    var xiaoming = { name: '小明', birth: 1990, school: 'No.1 Middle School', height: 1.70, weight: 65, sc ...

  5. 运行nodejs项目报Process finished with exit code 1 错误

    在项目中,明明在别人的机子上项目可以运行,但是复制到自己的电脑就无法就无法启动.报Process finished with exit code 1错误,也没提示错误地方.自己倒腾了很久总结了几个解决 ...

  6. Java GUI设置图标

    ImageIcon是Icon接口的一个实现类. ImageIcon类的构造函数: ImageIcon() ImageIcon(String filename)   //本地图片文件 ImageIcon ...

  7. 洛谷 P1330 封锁阳光大学

    题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构成的无向图,N个点之间由M ...

  8. COGS 36. 求和问题

    时间限制:1.2 s   内存限制:128 MB [问题描述]     在一个长度为n的整数数列中取出连续的若干个数,并求它们的和. [输入格式]     输入由若干行组成,第一行有一个整数n    ...

  9. windows/Linux 常用命令

    windows 文件操作命令 cd 切换文件目录 dir 显示文件目录内容 md 创建文件夹 rd 删除文件夹 copy 拷贝文件 move 移动文件 del 删除文件 replace 替换文件 mk ...

  10. window10系统安装Ubuntu18.04系统

    写这篇博客整理一下使用虚拟机安装Ubuntu系统,一般常用的虚拟机有VMware以及VirtualBox.鉴于方便,博主用的是virtualbox,虽然不是很美观,但简洁,且完全免费,且不需要在自己配 ...