Codeforces 877D - Olya and Energy Drinks 【简单BFS变形】


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数组来标记,但是之前的是标记该位置是否经过。现在需要保存该位置能否从四个方向到达的情况,这里我们用二进制来表示这个状态。


其他细节见代码。


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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值