Codeforces 877D. Olya and Energy Drinks BFS+并查集
来源:互联网 发布:阿里云服务器类型 编辑:程序博客网 时间:2024/05/29 04:34
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");}
- Codeforces 877D. Olya and Energy Drinks BFS+并查集
- Codeforces 877D Olya and Energy Drinks【思维优化Bfs】
- Codeforces 877 D. Olya and Energy Drinks (bfs)
- codeforces 877 problem D Olya and Energy Drinks 【bfs剪枝】
- [codeforces] 877D. Olya and Energy Drinks (BFS)
- Codeforces 877 D Olya and Energy Drinks
- Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks
- Codeforces Round #442 (Div. 2) 877 D. Olya and Energy Drinks BFS
- cf 877D Olya and Energy Drinks
- Codeforces Round #442(Div.2)Problem D Olya and Energy Drinks(BFS)
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks (bfs)
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks
- codeforces #442 div2 Olya and Energy Drinks
- Codeforces Round #442 (Div. 2) Olya and Energy Drinks(搜索 bfs 套路题)
- Codeforces Round #442 (Div. 2)-广搜&剪枝&技巧&好题-D. Olya and Energy Drinks
- CodeForces 305D Olya and Graph
- Codeforces 366D Dima and Trap Graph 【并查集】
- codeforces 659F (并查集 bfs)
- win10 ubuntu16 双系统,无法grb引导的解决
- lower_bound和upper_bound算法
- Java中outer标签的用法
- Linux—给普通用户sudo权限
- 第三课 Netty客户端
- Codeforces 877D. Olya and Energy Drinks BFS+并查集
- 2017-10-29 DBA日记,dbms_stats打开并行度后引发递归登录次数过高
- Gradle详解(一)——Groovy语法快速入门
- 汉诺塔+
- VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION
- CSRF攻击与防御原理
- Java 内存和GC机制
- 二叉树前序、中序、后序遍历相互求法
- Java VisualVM监控远程JVM