Igor and his way to work-Codeforce 793B BFS
来源:互联网 发布:js显示当前时间年月日 编辑:程序博客网 时间:2024/06/05 01:02
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 andm 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 andm (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
题意:是否能在少于两次转弯次数的条件下从S到达T
分析:搜索题,BFS和DFS都可以,条件不够很容易超时;
我们需要保存到达每个点的需要多少次转弯,可以开一个三维数组vis去存,要注意每次搜索出来不一定是最小的转弯次数;
AC code:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <stack>#include <string>#include <map>using namespace std;#define LL long long#define INF 0x3f3f3f3f#define eps 1const int maxn=100000+50;char map1[1005][1005];int vis[1005][1005][5]; //保存每个点各个方向的最优转弯次数int n,m;int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};struct point{ int x,y; int dir,turn; //方向和转弯次数};//是否越界int isOut(int x,int y){ if(x<0||x>=n||y<0||y>=m) return 1; return 0;}bool bfs(point s){ memset(vis,INF,sizeof vis); queue<point> q; q.push(s); while(!q.empty()){ s=q.front(); q.pop(); if(map1[s.x][s.y]=='T') return true; for(int i=0;i<4;i++){ int x=s.x+dx[i]; int y=s.y+dy[i]; int cnt; if(s.dir==-1||s.dir==i) //判断是否转弯 cnt=s.turn; else cnt=s.turn+1; //转弯次数不能大于2 if(isOut(x,y)||map1[x][y]=='*'||cnt>2) continue; if(vis[x][y][i]<=cnt) continue; //存在更优解 else{ point t; t.x=x; t.y=y; t.dir=i; t.turn=cnt; vis[x][y][i]=cnt; //更新最优解 q.push(t); } } } return false;}int main() { while(~scanf("%d%d",&n,&m)){ for(int i=0;i<n;i++){ scanf("%s",map1[i]); } point s; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(map1[i][j]=='S'){ s.x=i; s.y=j; s.turn=0; s.dir=-1; } } } if(bfs(s)) printf("YES\n"); else printf("NO\n"); }}
- Igor and his way to work-Codeforce 793B BFS
- codeforce 793 B Igor and his way to work
- Tinkoff B. Igor and his way to work(BFS)
- B. Igor and his way to work
- B. Igor and his way to work
- B. Igor and his way to work
- Codeforces 793B Igor and his way to work 题解
- Codeforces 793B Igor and his way to work dfs
- Codeforces 793B Igor and his way to work (dfs/bfs
- BFS--cf793b 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+优化)
- codeforces Igor and his way to work
- Codeforces 793b B. Igor and his way to work 觉得大神写的3维bfs太复杂,突然发现这题是连连看算法。
- CF-Tinkoff Challenge-Elimination Round-B-Igor and his way to work
- Codeforce 515 B . Drazil and His Happy Friends 暴力
- Codeforce 515 B . Drazil and His Happy Friends
- java hibernate 报错 参数值[1]与预期的类型[java.lang.String(n / a)]不匹配
- Druid连接池与DbUtils整合工具类
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
- Tensorflow实现Softmax Regression识别手写数字
- (4)qt5制作简易计算器详细步骤(ui界面)
- Igor and his way to work-Codeforce 793B BFS
- 移动Web开发基础-利用VW单位适配布局
- mysql 添加字段、删除字段、调整字段顺序
- Java web从入门到放弃(3)
- 编译进阶(二)精彩的C4
- 第14周—Java集合框架(第2题)
- Python学习日记 第六天
- 软键盘的显示和隐藏的正确姿势
- BZOJ5105: [CodePlus2017]晨跑