zoukankan      html  css  js  c++  java
  • Codeforces 877 D. Olya and Energy Drinks

     
    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 nm 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 ≤ n1 ≤ 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.

    题意:

    n*m 网格,每秒走1——k步

    不能走‘#’

    从指定位置走到目标位置所需的最短时间

    bfs 

    用并查集标记每个点上下左右第一个没有到过的点

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    #define turn(i,j) ((i)-1)*m+(j)
    
    const int INF=0x3f3f3f3f;
    
    int n,m,k,sx,sy,ex,ey,dis[1005][1005];
    int quex[1000005],quey[1000005];
    
    int faU[1000001],faD[1000001],faL[1000001],faR[1000001];
    
    char map[1005][1005];
    
    int find(int fa[1000001],int i)
    {
        return fa[i]==i ? i : fa[i]=find(fa,fa[i]);
    }
    
    void unionn(int i,int j)
    {
        int k=turn(i,j);
        if(i!=1) faU[k]=find(faU,turn(i-1,j));
        else faU[k]=0;
        if(i!=n) faD[k]=find(faD,turn(i+1,j));
        else faD[k]=0;
        if(j!=1) faL[k]=find(faL,turn(i,j-1));
        else faL[k]=0;
        if(j!=m) faR[k]=find(faR,turn(i,j+1));
        else faR[k]=0;
    }
    
    int bfs()
    {
        for(int i=1;i<=n;i++)
            for(int v=1;v<=m;v++)
                dis[i][v]=INF;
        if(map[sx][sy]=='#'||map[ex][ey]=='#')
            return -1;
        dis[sx][sy]=0;
        int h=0,tail=1;
        quex[h]=sx;
        quey[h]=sy;
        unionn(sx,sy);
        int nowx,nowy;
        while(h<tail)
        {
            nowx=quex[h];
            nowy=quey[h++];
            for(int i=1,x=nowx,y=nowy;i<=k;i++)
            {
                y=find(faR,turn(x,y));
                if(!y) break;
                y%=m; if(!y) y=m;
                if(y-nowy>k) break;
                if(map[x][y]=='#') break;
                dis[x][y]=dis[nowx][nowy]+1;
                unionn(x,y);
                quex[tail]=x;
                quey[tail++]=y;
            }
            for(int i=1,x=nowx,y=nowy;i<=k;i++)
            {
                y=find(faL,turn(x,y));
                if(!y) break;
                y%=m; if(!y) y=m;
                if(nowy-y>k) break;
                if(map[x][y]=='#') break;
                dis[x][y]=dis[nowx][nowy]+1;
                unionn(x,y);
                quex[tail]=x;
                quey[tail++]=y;
            }
            for(int i=1,x=nowx,y=nowy;i<=k;i++)
            {
                x=find(faD,turn(x,y));
                if(!x) break;
                x=(x-1)/m+1;
                if(x-nowx>k) break;
                if(map[x][y]=='#') break;
                if(dis[x][y]!=INF) continue;
                dis[x][y]=dis[nowx][nowy]+1;
                unionn(x,y);
                quex[tail]=x;
                quey[tail++]=y;
            }
            for(int i=1,x=nowx,y=nowy;i<=k;i++)
            {
                x=find(faU,turn(x,y));
                if(!x) break;
                x=(x-1)/m+1;
                if(nowx-x>k) break;
                if(map[x][y]=='#') break;
                if(dis[x][y]!=INF) continue;
                dis[x][y]=dis[nowx][nowy]+1;
                unionn(x,y);
                quex[tail]=x;
                quey[tail++]=y;
            }
        }
        if(dis[ex][ey]==INF)
            return -1;
        return dis[ex][ey];
    }
    
    int main()
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
            scanf("%s",map[i]+1);
        int k;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                k=turn(i,j);
                faU[k]=faD[k]=faL[k]=faR[k]=k;
            }
        scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
        std::cout<<bfs();
        return 0;
    }
  • 相关阅读:
    poj 2418 Hardwood Species
    hdu 3791 二叉搜索树
    九度oj 1544 数字序列区间最小值
    九度oj 1525 子串逆序打印
    九度oj 1530 最长不重复子串
    九度oj 1523 从上往下打印二叉树
    P1190 接水问题
    P1179 数字统计
    P1083 借教室
    P1079 Vigenère 密码
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7732318.html
Copyright © 2011-2022 走看看