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 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.
解题心得:
- 就是一个bfs只不过加了几个剪枝,可以使用spfa,记录到达每一个点所用的最小的时间,如果走到该点大于了该点的最小的时间就跳过。
- 也可以写一个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的更多相关文章
- Codeforces Round #524 (Div. 2) D. Olya and magical square
D. Olya and magical square 题目链接:https://codeforces.com/contest/1080/problem/D 题意: 给出一个边长为2n的正方形,每次可以 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- bio,nio,aio简介
https://mp.weixin.qq.com/s/F9WwcsFdCOwOeeDc0oc98w——<BIO,NIO,AIO 总结>总结和对比这三种的区别 https://www.jia ...
- 《JavaScript设计模式》笔记之第一、二章:富有表现力的JavaScript 和 接口
第一章 创建一个类 方法一: var Anim = function() { ... }; Anim.prototype.start = functi ...
- CentOS7.2 yum安装报错
1.yum源repodata配置文件repomd.xml无法找到: Couldn't open file /mnt/cdrom/repodata/repomd.xml 先找到repomd.xml的路径 ...
- 开源GIS-对比GeoServer和MapServer
1 主流组合: 基于C.C++系列的:Mapserver(服务器)+QGIS(桌面软件)+PostGIS(数据库)+Openlayers(JS)/ openscale (FLex)(浏览器客户端) ...
- IP-XACT IP IEEE交换格式
1 What is IP-XACT? IP-XACT is an XML format that defines and describes electronic components a ...
- C++拾遗(一)——变量和基本类型
今天看到一个小小的算法,交换两个数却不引入中间变量,想了下没什么思路.看了答案是这样: int a, b; a = a + b; b = a - b; a = a - b; 感觉还是挺有想法的,借此也 ...
- c#网站对服务器的数据库的备份与还原
在网站上备份数据库 1.前台页面 <div> 需要备份的文件名:<asp:TextBox ID="bfFile" runat="server" ...
- _T(x) _TEXT(x) L 代表什么?
首先 <tchar.h>中 #ifdef _UNICODE .... #define __T(x) L ## x //替换 #else /* ndef _UNICODE ...
- UVA1663 Purifying Machine (匈牙利算法,二分图最大匹配)
模版集合个数减少是因为匹配串集合中没被匹配过的一对串匹配了.所以就是找一个二分图最大匹配. 因为集合X和Y是不好分开的,但是可以直接跑,两个集合都会跑一遍,所以一个匹配会被算两次,返回的时候除以2就行 ...
- 2018 北京区域赛 I - Palindromes (找规律)
题目 HihoCoder - 1878 题目大意 给出k,让求出第k个回文数(k的“长度”不超过1e5) 题解 之前做过类似的题,是统计各阶段的数找到第K个回文数,但这里K太大,需要寻找新的方法. 打 ...