codeforces 877 problem D Olya and Energy Drinks 【bfs剪枝】
来源:互联网 发布:集成吊顶软件免费版 编辑:程序博客网 时间:2024/05/14 07:53
传送门
//题意: 就是给定一个起点和一个终点, 每次可以往四个方向最多走k步, 每次走花费一秒, 问走到终点最少需要多少秒.
//思路: 直接bfs肯定会T, 我们需要加上几个剪枝, 一个是当我们走到某点时, 该点曾经被到达过,并且此时的时间比从当前点走到这个点的时间还要短, 那么就不要走了, 因为后续的点由这个点到达更优一点. 具体细节请看代码.
AC Code
const int maxn = 1e3+5;int cas=1;char mapp[maxn][maxn];int vis[maxn][maxn],dis[maxn][maxn];int dx[] = {1,-1,0,0};int dy[] = {0,0,1,-1};void solve(){ int n,m,k; int sx,sy,ex,ey; while(~scanf("%d%d%d",&n,&m,&k)){ for(int i=1;i<=n;i++){ scanf("%s",mapp[i]+1); } scanf("%d%d%d%d",&sx,&sy,&ex,&ey); Fill(vis,0); Fill(dis,inf); queue<pair<int,int>>q; q.push(make_pair(sx,sy)); vis[sx][sy] = 1; dis[sx][sy] = 0; while(!q.empty()){ pair<int,int>tmp = q.front(); q.pop(); for(int i=0;i<4;i++){ for(int j=1;j<=k;j++){ int xx = tmp.fi+dx[i]*j; int yy = tmp.se+dy[i]*j; if(xx<1 || xx>n || yy<1 || yy>m) break; if(mapp[xx][yy] == '#') break; if(dis[xx][yy]!=inf && dis[xx][yy] < dis[tmp.fi][tmp.se] + 1) break; if(vis[xx][yy]) continue; vis[xx][yy] = 1; dis[xx][yy] = dis[tmp.fi][tmp.se] + 1; q.push(make_pair(xx,yy)); } } } if(dis[ex][ey]!=inf) cout << dis[ex][ey] << endl; else cout << "-1" << endl; }}
阅读全文
0 0
- codeforces 877 problem D Olya and Energy Drinks 【bfs剪枝】
- Codeforces 877D Olya and Energy Drinks【思维优化Bfs】
- Codeforces 877 D. Olya and Energy Drinks (bfs)
- [codeforces] 877D. Olya and Energy Drinks (BFS)
- Codeforces 877 D Olya and Energy Drinks
- Codeforces Round #442(Div.2)Problem D Olya and Energy Drinks(BFS)
- Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks
- Codeforces Round #442 (Div. 2) 877 D. Olya and Energy Drinks BFS
- Codeforces 877D. Olya and Energy Drinks BFS+并查集
- cf 877D Olya and Energy Drinks
- Codeforces Round #442 (Div. 2)-广搜&剪枝&技巧&好题-D. Olya and Energy Drinks
- 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 305D Olya and Graph
- Codeforces Drinks
- Codeforces 463D Gargari and Permutations(BFS)
- struts2的标签 s:property 的 escape
- Java
- VS2015+Qt 如何设置窗口左上角图标
- javascript const Array Object 的注意点
- 无名管道
- codeforces 877 problem D Olya and Energy Drinks 【bfs剪枝】
- jQuery操作节点元素方法总结
- [MAC]OpenGL中SOIL库与stbi_load库加载图片,透明黑色问题,遇到的坑及解决办法
- MBProgressHUD中0.7版本在ios11上面的小坑
- 共享打印机复印机方案开发
- Adb interface驱动无法安装解决方法!
- cuda 9.0 + vs2015 环境搭建
- 1003. 我要通过!(20)
- linux scp用法