Codeforces Round #442 (Div. 2) Olya and Energy Drinks(搜索 bfs 套路题)
来源:互联网 发布:数控铣床编程方法 编辑:程序博客网 时间:2024/05/29 02:03
根据题目可知,这个只允许每个节点搜一次,4个方向,所以只要访问过,就可以break;因为深搜的话,同一个方向,如果已经搜过,不可能再有一个比以前的节点小。这个深搜新套路,学习了,虽然我开始也想搜索,但是我掌握的搜索没有一个在这个时间复杂度过题的直接看了别人的代码,并找了队友错误代码,给他改成正确的,学到了这个套路。
#include<bits/stdc++.h>using namespace std;typedef __int64 LL;const int maxm = 1e3+10;const int maxn = 1e7+10;const int mv[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};struct node{ int x; int y; int t; node() {} node(int xx,int yy,int tt) { x = xx; y = yy; t = tt; }} g[maxn];char c[maxm][maxm];int vis[maxm][maxm][5];int f[maxm][maxm];int n,m,k;int x1,x2,y1,y2;queue<node>que;void init(){ for(int j=0; j<4; j++) for(int i=1; i<=k; i++) { int xx = x1+mv[j][0]*i; int yy = y1+mv[j][1]*i; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&c[xx][yy]=='.'&&!vis[xx][yy][j]) { f[xx][yy]=1; que.push(node(xx,yy,1)); vis[xx][yy][j]=1; } else break; }}void bfs(){ init(); while(!que.empty()) {if(f[x2][y2]){ printf("%d\n",f[x2][y2]); return;} node s=que.front(); que.pop(); for(int j=0; j<4; j++) for(int i=1; i<=k; i++) { int xx = s.x+mv[j][0]*i; int yy = s.y+mv[j][1]*i; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&c[xx][yy]=='.'&&!vis[xx][yy][j]) { if(f[xx][yy]==0)f[xx][yy]=s.t+1; que.push(node(xx,yy,f[xx][yy])); vis[xx][yy][j]=1; } else break; } } if(x1==x2&&y1==y2) puts("0");// cout<<0<<endl; else if(f[x2][y2]==0) puts("-1");// cout<<-1<<endl; else printf("%d\n",f[x2][y2]);// cout<<f[x2][y2]<<endl;}int main(){ scanf("%d%d%d%*c",&n,&m,&k); for(int i=1; i<=n; i++) gets(c[i]+1); memset(vis,0,sizeof(vis)); memset(f,0,sizeof(f)); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); bfs(); return 0;}
阅读全文
0 0
- Codeforces Round #442 (Div. 2) Olya and Energy Drinks(搜索 bfs 套路题)
- 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 #442 (Div. 2)-广搜&剪枝&技巧&好题-D. Olya and Energy Drinks
- Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks
- Codeforces 877 D. Olya and Energy Drinks (bfs)
- codeforces #442 div2 Olya and Energy Drinks
- 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 877D. Olya and Energy Drinks BFS+并查集
- cf 877D Olya and Energy Drinks
- D. Arthur and Walls(Codeforces Round #297 (Div. 2) 搜索(BFS))
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 搜索bfs
- Codeforces Round #442 (Div. 2) B. Nikita and string (记忆化搜索)
- android系统休眠唤醒服务修改
- Jmeter解决唯一标识的问题-UUID
- 《android源码分析系列》仿OkHttp-- 自己动手实现 okhttp
- 文档总结11-linux中管理网络
- Gulp教程(2)-分支管理
- Codeforces Round #442 (Div. 2) Olya and Energy Drinks(搜索 bfs 套路题)
- JS高程知识点总结-6(面向对象)
- 为Ubuntu14.04 安装MySQL的可视化界面的 WorkBench
- nginx多域名配置及默认页面
- bzoj1497: [NOI2006]最大获利(网络流)
- 系统开发哪里好
- JZOJ5411. 【NOIP2017提高A组集训10.22】友谊 DP
- explain详解
- 基于xxl-job的订单、库存同步分布式任务调度框架TPP_ORDER详解