Codeforces Round #442(Div.2)Problem D Olya and Energy Drinks(BFS)

D. Olya and Energy Drinks
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
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.

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 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.

【思路】

显然要用广度优先搜索的方式来做这个题目。在广搜中所有节点都是被分层次访问的,在这道题中距离刚好等于层次,所以每个节点被访问的第一次就已经可以得到起点到它的最短距离,故它只需入队一次去更新子节点即可,所以写法有些类似于SPFA。而且注意剪枝的情况:当到达某个点P的dist已经小于起点到当前点再到它的距距离,那么意味着当前点沿着P点方向的其他点不必入队,因为它们的dist都小于或等于从当前点到它们,如果是小于则可以交给下一层处理,如果是等于则不必再询问这个方向上的剩下点,这样就可以减少大量不必要的询问和更新。
 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>

using namespace std;

int n,m,k;
char mp[1005][1005];
int dis[1005][1005];
int x3,y3,x2,y2;
int inf=1e8;

struct node
{
    int x,y;
};

int dir[4][2]={0,1,0,-1,1,0,-1,0};

bool check(int x,int y)
{
    if(x>=0 && x<n &&y>=0 && y<m)
    {
        if(mp[x][y]=='.')
            return true;
    }
    return false;
}

void bfs()
{
    node sta;
    sta.x=x3;sta.y=y3;
    dis[x3][y3]=0;
    queue<node> q;
    q.push(sta);

    while(!q.empty())
    {
        node cur=q.front();
        q.pop();

        for(int i=0;i<4;i++)
        {
            for(int j=1;j<=k;j++)
            {
                int xx=cur.x+dir[i][0]*j;
                int yy=cur.y+dir[i][1]*j;
                if(check(xx,yy) && dis[xx][yy]>=(dis[cur.x][cur.y]+1))
                {
                    if(dis[xx][yy]>(dis[cur.x][cur.y]+1)){
                    dis[xx][yy]=(dis[cur.x][cur.y]+1);
                    node nex;
                    nex.x=xx;nex.y=yy;
                    q.push(nex);
                    }
                }
                else
                    break;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(int i=0;i<=1000;i++)
            for(int j=0;j<=1000;j++)
            dis[i][j]=inf;

        for(int i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
        }
        scanf("%d%d%d%d",&x3,&y3,&x2,&y2);
        x3--;y3--;x2--;y2--;

        bfs();
        if(dis[x2][y2]==inf)
            printf("-1\n");
        else
            printf("%d\n",dis[x2][y2]);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值