Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks (bfs)
来源:互联网 发布:知乎 日本研究生申请 编辑:程序博客网 时间:2024/05/30 04:07
题意:
给你一张地图,其中只有’.’和’#’,’.’能走,’#’不能走,你一步最多走k个格子(不能翻越’#’)现在要从某个点走到另一个点,问至少几步就能走过去
思路:
理论上的bfs复杂度是理论上最差是
错误及反思:
代码:
#include<bits/stdc++.h>using namespace std;const int maxn = 1010;char mm[maxn][maxn];int num[maxn][maxn];bool did[maxn][maxn];int n,m,k;map<pair<int,int>,int> km;int nex[4][2]={0,1,1,0,0,-1,-1,0};int x1,x2,y2,y1;void bfs(int x,int y){ num[x][y]=0; did[x][y]=false; km[{x,y}]=1; queue<pair<int,int> > q; q.push({x,y}); while(!q.empty()) { pair<int,int> temp=q.front(); q.pop(); int tx=temp.first; int ty=temp.second; if(tx==x2&&ty==y2||num[x2][y2]!=-1) return ; for(int i=0;i<4;i++) { for(int j=1;j<=k;j++) { int xx=tx+j*nex[i][0],yy=ty+j*nex[i][1]; if(xx<=0||yy<=0||xx>n||yy>m) break; if(did[xx][yy]&&mm[xx-1][yy-1]=='#') { break; } if(did[xx][yy]&&mm[xx-1][yy-1]=='.') { did[xx][yy]=false; num[xx][yy]=num[tx][ty]+1; } } } for(int i=0;i<4;i++) { for(int j=1;j<=k;j++) { int xx=tx+j*nex[i][0],yy=ty+j*nex[i][1]; if(xx<=0||yy<=0||xx>n||yy>m) break; if(did[xx][yy]&&mm[xx-1][yy-1]=='#') { break; } for(int l=0;l<4;l++) { int ax=xx+nex[l][0],ay=yy+nex[l][1]; if(ax>0&&ax<=n&&ay>0&&ay<=m&&did[ax][ay]&&mm[ax-1][ay-1]=='.') { if(km[{xx,yy}]==0){ q.push({xx,yy}); km[{xx,yy}]=1; break; } } } } } }}int main(){ scanf("%d%d%d",&n,&m,&k); for(int i=0;i<n;i++) scanf("%s",mm[i]); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); memset(num,-1,sizeof(num)); memset(did,true,sizeof(did)); bfs(x1,y1); printf("%d\n",num[x2][y2]);}
阅读全文
0 0
- 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 Round #442 (Div. 2) 877 D. Olya and Energy Drinks BFS
- Codeforces Round #877 (Div. 2) D. 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 877 D. Olya and Energy Drinks (bfs)
- Codeforces 877D 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 #442 div2 Olya and Energy Drinks
- Codeforces 877D. Olya and Energy Drinks BFS+并查集
- cf 877D Olya and Energy Drinks
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth (BFS)
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth(BFS)
- D. Arthur and Walls(Codeforces Round #297 (Div. 2) 搜索(BFS))
- linux 下安装并运行kettle 程序
- siamese(孪生) 网络
- Visual Leak Detector工作原理(旧版本)
- 自定义控件基础知识--Canvas
- 10.25日常总结
- Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks (bfs)
- nginx的启动_停止_重启
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
- Servlet学习心得总结
- idea for Mac 错误: 找不到或无法加载主类 com.company.Main
- 前端怎么写出一个好的页面
- 立体匹配的研究背景以及意义
- Android BLE基础框架全新改版
- 切绳子