Codeforces 877D - Olya and Energy Drinks 【简单BFS变形】
2017-10-24 21:09
459 查看
D. Olya and Energy Drinks
time limit per test 2
seconds
memory limit per test 256
megabytes
Olya loves energy drinks. She loves them so much that her roomis
full of empty cans from energy drinks.
Formally, her room can be represented as a field ofn × m
cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can runk
meters per second. Each second shechooses one of the four directions (up, down, left or right) and runs from1
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 ifshe moves optimally?
It's guaranteed that cells(x1, y1)
and (x2, y2)
are empty. These cells can coincide.
Input
The first line contains three integersn,m
and k
(1 ≤ n, m, k ≤ 1000)
—the sizes of the room and Olya's speed.
Thenn
lines follow containing m
characters each, the i-th
of them contains on j-th
position "#",
if the cell (i, j)
is littered withcans, and "."otherwise.
The last line contains four integersx1, 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 toget
from (x1, y1)
to (x2, y2).
If it's impossible to get from(x1, y1)
to (x2, y2),
print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run3
meters to the right in the firstsecond, 2meters
down in the second second and 3
meters to the left in the thirdsecond.
In second sample Olya should run to the right for3
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.
【题意】
给出一张n*m的地图,给定起点和终点,一个人从起点出发,每一秒内能向一个方向走1~k中的任意步数,问从起点到终点最少需要花费的时间,如果不能到达,则输出-1。
【思路】
这道题跟简单的走迷宫唯一不一样的就是每一秒可以走多步,那么我们只要去枚举每个方向及在该方向上走的步数。
需要注意的是,为了防止TLE,我们仍需要用vis数组来标记,但是之前的是标记该位置是否经过。现在需要保存该位置能否从四个方向到达的情况,这里我们用二进制来表示这个状态。
其他细节见代码。
time limit per test 2
seconds
memory limit per test 256
megabytes
Olya loves energy drinks. She loves them so much that her roomis
full of empty cans from energy drinks.
Formally, her room can be represented as a field ofn × m
cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can runk
meters per second. Each second shechooses one of the four directions (up, down, left or right) and runs from1
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 ifshe moves optimally?
It's guaranteed that cells(x1, y1)
and (x2, y2)
are empty. These cells can coincide.
Input
The first line contains three integersn,m
and k
(1 ≤ n, m, k ≤ 1000)
—the sizes of the room and Olya's speed.
Thenn
lines follow containing m
characters each, the i-th
of them contains on j-th
position "#",
if the cell (i, j)
is littered withcans, and "."otherwise.
The last line contains four integersx1, 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 toget
from (x1, y1)
to (x2, y2).
If it's impossible to get from(x1, y1)
to (x2, y2),
print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run3
meters to the right in the firstsecond, 2meters
down in the second second and 3
meters to the left in the thirdsecond.
In second sample Olya should run to the right for3
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.
【题意】
给出一张n*m的地图,给定起点和终点,一个人从起点出发,每一秒内能向一个方向走1~k中的任意步数,问从起点到终点最少需要花费的时间,如果不能到达,则输出-1。
【思路】
这道题跟简单的走迷宫唯一不一样的就是每一秒可以走多步,那么我们只要去枚举每个方向及在该方向上走的步数。
需要注意的是,为了防止TLE,我们仍需要用vis数组来标记,但是之前的是标记该位置是否经过。现在需要保存该位置能否从四个方向到达的情况,这里我们用二进制来表示这个状态。
其他细节见代码。
#include <cstdio> #include <queue> #include <cstring> #include <algorithm> using namespace std; #define mst(a,b) memset((a),(b),sizeof(a)) #define rush() int T;scanf("%d",&T);while(T--) typedef long long ll; const int maxn = 1005; const ll mod = 1e9+7; const int INF = 0x3f3f3f3f; const double eps = 1e-9; const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int n,m,k; int sx,sy,ex,ey; int vis[maxn][maxn]; char mp[maxn][maxn]; int dis[maxn][maxn]; struct node { int x,y; }now,nex; bool inbound(node a) { return a.x>=1&&a.x<=n&&a.y>=1&&a.y<=m; } void bfs() { queue<node>q; now.x=sx; now.y=sy; q.push(now); vis[sx][sy]=(1<<4)-1; //起点只经过一次就好 while(q.size()) { now=q.front(); q.pop(); if(now.x==ex&&now.y==ey) return; //搜到终点 for(int i=0;i<4;i++) //枚举方向 for(int j=1;j<=k;j++) //枚举走的步数 { nex.x=now.x+dir[i][0]*j; nex.y=now.y+dir[i][1]*j; if(!inbound(nex)||mp[nex.x][nex.y]=='#') break; //该位置不合法 if(vis[nex.x][nex.y]&(1<<i)) break; //从这个方向已经走过该位置 int flag=0; if(!vis[nex.x][nex.y]) flag=1; //搜到没有走过的点 vis[nex.x][nex.y]|=(1<<i); //更新状态 if(flag) { dis[nex.x][nex.y]=dis[now.x][now.y]+1; q.push(nex); } } } } int main() { scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) { scanf("%s",mp[i]+1); } scanf("%d%d%d%d",&sx,&sy,&ex,&ey); bfs(); if(vis[ex][ey]) printf("%d\n",dis[ex][ey]); else puts("-1"); return 0; }
相关文章推荐
- ZOJ1649/HDU1242简单的BFS加上一点小的变形
- 坦克大战(bfs简单变形)
- Codeforces 390C Inna and Candy Boxes RMQ简单变化 或 前缀和dp一下
- CodeForces 689B BFS-Mike and Shortcuts
- codeforces 676D (bfs 模拟)
- HDU 1253(简单BFS)
- CodeForces - 540C Ice Cave(BFS)
- CodeForces - 574A Bear and Elections 简单的优先队列的使用
- hdu 1495 非常可乐(简单的的bfs)
- Codeforces 340D Bubble Sort Graph【LIS变形】
- CodeForces - 710E (简单dp)
- POJ 3669 简单BFS
- POJ Find The Multiple 简单BFS
- HDU 1548 A strange lift (简单BFS)
- Codeforces 489C 简单构造
- bfs简单题 迷宫问题
- hdu 1026 Ignatius and the Princess I(简单的bfs+输出路径)(WA的代码)
- hdu 1312简单bfs
- CodeForces 25A IQ test 唯一奇偶(简单的题目)
- CodeForces - 450D Jzzhu and Cities spfa变形