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;
}