admin管理员组文章数量:1252687
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 long
const 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");
}
本文标签: OlyaCodeForcesBFSDrinksEnergy
版权声明:本文标题:Codeforces 877D. Olya and Energy Drinks BFS+并查集 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1740361722a2266647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论