Codeforces 877D. Olya and Energy Drinks BFS+並查集

來源:互聯網
上載者:User

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. 題解:

感覺NOIP藥丸……這題都不會做……由於每個點只會被訪問一次,那麼我們只需要快速找到一個點上下左右四個方向沒有被訪問過的點就行了,這可以用並查集實現,剩下的就是常規的BFS了。 代碼:

#include<bits/stdc++.h>using namespace std;#define pa pair<int,int>#define LL long longconst int Maxn=1010;int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}    return x*f;}int k,n,m,stx,sty,edx,edy;int mp[Maxn][Maxn],num[Maxn][Maxn],nnum[Maxn*Maxn][2],tot=0;queue<int>qx,qy;bool vis[Maxn*Maxn];int f[Maxn*Maxn];int fa[4][Maxn*Maxn];//0123上下左右 int findfa(int o,int x){return((fa[o][x]==x)?x:fa[o][x]=findfa(o,fa[o][x]));}void del(int o,int x,int y){    fa[0][o]=findfa(0,fa[0][num[x-1][y]]);    fa[1][o]=findfa(1,fa[1][num[x+1][y]]);    fa[2][o]=findfa(2,fa[2][num[x][y-1]]);    fa[3][o]=findfa(3,fa[3][num[x][y+1]]);}int main(){    memset(f,63,sizeof(f));    n=read();m=read();k=read();    for(int i=1;i<=n;i++)    for(int j=1;j<=m;j++)    num[i][j]=++tot,nnum[tot][0]=i,nnum[tot][1]=j;    for(int i=1;i<=n;i++)    {        char str[Maxn];        scanf("%s",str+1);        for(int j=1;j<=m;j++)        {            mp[i][j]=((str[j]=='#')?0:1);            for(int l=0;l<4;l++)fa[l][num[i][j]]=num[i][j];        }    }    stx=read(),sty=read(),edx=read(),edy=read();    qx.push(stx);qy.push(sty);    int t=num[stx][sty];f[t]=0;vis[t]=true;    fa[0][t]=num[stx-1][sty];fa[1][t]=num[stx+1][sty];    fa[2][t]=num[stx][sty-1];fa[3][t]=num[stx][sty+1];    while(!qx.empty())    {        int x=qx.front(),y=qy.front();        if(x==edx&&y==edy)break;        qx.pop();qy.pop();        int t,o=num[x][y];        for(int i=0;i<4;i++)        {            while((t=findfa(i,fa[i][o]))!=0)            {                int tx=nnum[t][0],ty=nnum[t][1];                if(!mp[tx][ty])break;                if(i==0&&x-tx>k)break;                else if(i==1&&tx-x>k)break;                else if(i==2&&y-ty>k)break;                else if(i==3&&ty-y>k)break;                vis[t]=true;                f[t]=f[o]+1;                del(t,tx,ty);                qx.push(tx);qy.push(ty);            }        }    }    int ans=f[num[edx][edy]];    if(ans!=1061109567)printf("%d",ans);    else puts("-1");}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.