bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 bfs
来源:互联网 发布:snmp trap 接收 java 编辑:程序博客网 时间:2024/06/05 15:56
→题目链接←
bfs时,对于每个点,显然最多只能访问一次,并且只要访问到了就一定是最短路径
记录一下到达每个点的步数,最后从终点每次找周围步数比它小1的点,就可以找到我们需要的那个路径
已经知道路径了,反着搞回去就可以了,每次判断一下行走方向是否改变就好
懒得反着记录答案,所以用了个双向队列,真是方便啊
代码:
#include<iostream>#include<cstdio>#include<queue>#include<deque>#include<vector>#include<algorithm>#include<cstring>#define inf 999999999#define ll long longusing namespace std;struct point{int x,y;friend bool operator == (point a,point b){return (a.x==b.x && a.y==b.y);}};struct node{char way;int num;};point s,e;int n,m;int a[110][110];int dis[110][110];queue<point>q;int tox[4]={-1,0,1,0};int toy[4]={0,1,0,-1};deque<node>ans;int main(){scanf("%d%d\n",&n,&m);n=n*2-1;m=m*2-1;for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){dis[i][j]=-1;char c;c=getchar();if(c=='.')continue;else if(c=='+')a[i][j]=2;else if(c=='S')s.x=i,s.y=j,a[i][j]=2;else if(c=='E')e.x=i,e.y=j,a[i][j]=2;else a[i][j]=1;}getchar();}dis[s.x][s.y]=0;point t;t=s;q.push(t);while(!q.empty()){t=q.front();q.pop();if(t==e)break;for(int i=0; i<4; i++){point t1=t;t1.x+=tox[i];t1.y+=toy[i];if(t1.x<1 || t1.x>n || t1.y<1 || t1.y>m || a[t1.x][t1.y]==0 || dis[t1.x][t1.y]!=-1)continue;dis[t1.x][t1.y]=dis[t.x][t.y]+1;q.push(t1);}}int last=-1;int sum=0;t=e;while(1){if(t==s){node tt;if(last==0)tt.way='S';if(last==1)tt.way='W';if(last==2)tt.way='N';if(last==3)tt.way='E';tt.num=sum;ans.push_front(tt);break;}int to;if(a[t.x][t.y]==2)sum++;for(int i=0; i<4; i++){point t1=t;t1.x+=tox[i];t1.y+=toy[i];if(t1.x<1 || t1.x>n || t1.y<1 || t1.y>m || a[t1.x][t1.y]==0 || dis[t1.x][t1.y]!=dis[t.x][t.y]-1)continue;to=i;break;}if(last!=to){if(last!=-1){node tt;if(last==0)tt.way='S';if(last==1)tt.way='W';if(last==2)tt.way='N';if(last==3)tt.way='E';tt.num=sum-1;ans.push_front(tt);sum=1;}last=to;}t.x+=tox[to];t.y+=toy[to];}for(int i=0; i<ans.size(); i++){printf("%c %d\n",ans[i].way,ans[i].num);}return 0;}
阅读全文
0 0
- bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 bfs
- bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通(BFS)
- bzoj1687: [Usaco2005 Open]Navigating the City 城市交通
- BZOJ1687: [Usaco2005 Open]Navigating the City 城市交通
- BZOJ1687: [Usaco2005 Open]Navigating the City 城市交通 Spfa
- Cpp环境【POJ2435】【CQYZOJ3034】【USACO2005 U S Open Silver】Navigating the City城市交通
- poj Navigating the City 水bfs
- bzoj 1747: [Usaco2005 open]Expedition 探险 贪心
- BZOJ 1747 [Usaco2005 open]Expedition 探险 贪心
- bzoj 1749: [usaco2005 open]Landscaping 地形改造
- BZOJ 1746: [Usaco2005 open]Lazy Cows
- bzoj 1683: [Usaco2005 Nov]City skyline 城市地平线
- bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士(BFS)
- BZOJ 1689: [Usaco2005 Open] Muddy roads 泥泞的路
- BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理
- bzoj 1688 [Usaco2005 Open]Disease Manangement 疾病管理
- bzoj 1688: [Usaco2005 Open]Disease Manangement 疾病管理 dfs
- BZOJ 1689 [Usaco2005 Open] Muddy roads 泥泞的路 模拟
- 支付宝手机支付接口集成总结及支付宝missing signnature config错误处理
- LCD6210字符型液晶显示器 (2)
- 3480943346903
- B-树(B树)、B+树、B*树
- Linux下logrotate命令使用.配置和理解
- bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 bfs
- web前端之过滤器的作用
- Python机器学习实践与Kaggle实战
- MySQL性能优化的最佳21条经验
- centos下配置主机名称AND 配置网卡信息
- 修改输入框placeholder文字的样式
- 【STL】栈的实现原理以及应用
- 如何重新编辑排版错乱的PDF文件
- C# Regex类详解