B. Igor and his way to work
来源:互联网 发布:匡恩网络物联网安全 编辑:程序博客网 时间:2024/05/20 05:09
Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.
Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.
The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the grid.
Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:
- "." — an empty cell;
- "*" — a cell with road works;
- "S" — the cell where Igor's home is located;
- "T" — the cell where Igor's office is located.
It is guaranteed that "S" and "T" appear exactly once each.
In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.
5 5..S..****.T....****......
YES
5 5S....****.......****..T..
NO
真菜啊,最近连个bfs都不会写了。
这个就是bfs从起点看能不能到达终点。只不过限制条件有点多。
1.其中一个状态就是当前点还能转几次弯。最开始的自然是能转两次。
2.一个状态就是当前点的方向。
所以vis标记数组出了横纵坐标外,应该还有上面说的那两维。
确定好这个之后就是一个简单的bfs可到达问题了。
#include <bits/stdc++.h>using namespace std;const int MAXN=1e3+7;const int inf=1e9;int n,m,k;int sx,sy,ex,ey;char tu[MAXN][MAXN];int dx[4]= {0,0,1,-1};int dy[4]= {-1,1,0,0};bool vis[MAXN][MAXN][4][2];struct node{ int x,y,d,sum;//横坐标,纵坐标,当前的方向,剩余的可转弯次数 node(int x1,int y1,int d1,int sum1) { x=x1; y=y1; d=d1; sum=sum1; }};int bfs(){ queue<node>q; for(int i=0; i<4; ++i)//先把相邻的满足条件的4个点加进来 { int x=sx+dx[i]; int y=sy+dy[i]; if(x>=0&&x<n&&y>=0&&y<m&&tu[x][y]!='*') { vis[x][y][i][2]=1; q.push(node(x,y,i,2)); } } while(!q.empty()) { node u=q.front(); q.pop(); if(u.x==ex&&u.y==ey)return 1;//表示可以到达 for(int i=0; i<4; ++i) { int x=u.x+dx[i]; int y=u.y+dy[i]; if(x>=0&&x<n&&y>=0&&y<m&&tu[x][y]!='*') { if(i==u.d)//如果方向相同 { if(!vis[x][y][i][u.sum]) { vis[x][y][i][u.sum]=1; q.push(node(x,y,i,u.sum)); } } else if(u.sum>0) { if(!vis[x][y][i][u.sum-1]) { vis[x][y][i][u.sum-1]=1; q.push(node(x,y,i,u.sum-1)); } } } } } return 0;}int main(){ scanf("%d%d",&n,&m); for(int i=0; i<n; ++i) { scanf("%s",tu[i]); for(int j=0; j<m; ++j) { if(tu[i][j]=='S') { sx=i; sy=j; } else if(tu[i][j]=='T') { ex=i; ey=j; } } } if(bfs())puts("YES"); else puts("NO"); return 0;}
- B. Igor and his way to work
- B. Igor and his way to work
- B. Igor and his way to work
- Tinkoff B. Igor and his way to work(BFS)
- codeforce 793 B Igor and his way to work
- Codeforces 793B Igor and his way to work 题解
- Codeforces 793B Igor and his way to work dfs
- Igor and his way to work-Codeforce 793B BFS
- codeforces Igor and his way to work
- Codeforces 793B Igor and his way to work (dfs/bfs
- CF-Tinkoff Challenge-Elimination Round-B-Igor and his way to work
- CF793B:B Igor and his way to work(DFS & BFS)
- codeforces B. Igor and his way to work dfs or bfs
- Codeforces-739B:Igor and his way to work(BFS+优化)
- BFS--cf793b Igor and his way to work
- Codeforces 793b B. Igor and his way to work 觉得大神写的3维bfs太复杂,突然发现这题是连连看算法。
- B. Sherlock and his girlfriend
- B. Sherlock and his girlfriend
- Solr安装与简单应用
- Nginx常用Rewrite伪静态规则
- File的基本使用
- 友盟第三方登录流程跟大家分享一下,不足的地方欢迎指点。
- 非交互式分区 (用脚本自动分区)
- B. Igor and his way to work
- 查阅大量资料之后的“匿名内部类”总结
- 170424 汇编-OF标志位的进一步理解
- [树上莫队] BZOJ 3460 Jc的宿舍
- HttpClient使用详解
- SpringMVC请求过程描述 (比喻非常通俗易懂)
- 前端工具安装
- 中南大学第十一届大学生程序设计竞赛-COJ1896-Symmetry
- 版本控制工具