Codeforces 793B Igor and his way to work (dfs/bfs
来源:互联网 发布:网络舆情是什么 编辑:程序博客网 时间:2024/06/04 19:08
Description
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.
Input
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.
Output
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.
Sample Input
5 5..S..****.T....****......5 5S....****.......****..T..
Sample Output
YESNO
Hint
The first sample is shown on the following picture:
In the second sample it is impossible to reach Igor’s office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:
题意 :
从一个点到另一个点 其中转弯次数不能超过2次
题解:
还不很理解 明天补上吧
AC代码
#include <bits/stdc++.h>using namespace std;char str[1005][1005];int vis[1005][5005];int n, m;int dx[] = {1,-1,0,0};int dy[] = {0,0,1,-1};void dfs(int x, int y, int d, int pre){ if(x<0||x>=n||y<0||y>=m) return ; if(str[x][y] == '*') return ; vis[x][y] = 1; if(d==1) { for(int i = 0; i < 4; i++) dfs(x+dx[i],y+dy[i],0,i); } dfs(x+dx[pre],y+dy[pre],d,pre);}bool ans_dfs(int x,int y,int pre){ if(x<0||x>=n||y<0||y>=m) return false; if(str[x][y]=='*') return false; if(vis[x][y]==1) return true; return ans_dfs(x+dx[pre],y+dy[pre],pre);}int main(){ memset(vis,0,sizeof(vis)); int x1,x2,y1,y2; scanf("%d%d",&n,&m); for(int i = 0;i < n; i++) scanf("%s",str[i]); for(int i = 0;i < n; i++) { for(int j = 0;j < m; j++) { if(str[i][j]=='S') { x1 = i; y1 = j; } if(str[i][j]=='T') { x2 = i; y2 = j; } } } for(int i = 0;i < 4; i++) { dfs(x1,y1,1,i); } bool flag = false; for(int i = 0;i < 4; i++) { if(ans_dfs(x2,y2,i)) { flag = true; break; } } if(flag) puts("YES"); else puts("NO");return 0;}
- Codeforces 793B Igor and his way to work dfs
- Codeforces 793B Igor and his way to work (dfs/bfs
- codeforces B. Igor and his way to work dfs or bfs
- Igor and his way to work-Codeforce 793B BFS
- Codeforces 793B Igor and his way to work 题解
- Tinkoff B. Igor and his way to work(BFS)
- CF793B:B Igor and his way to work(DFS & BFS)
- Codeforces-739B:Igor and his way to work(BFS+优化)
- codeforces Igor and his way to work
- B. Igor and his way to work
- B. Igor and his way to work
- B. Igor and his way to work
- codeforce 793 B Igor and his way to work
- Codeforces 793b B. Igor and his way to work 觉得大神写的3维bfs太复杂,突然发现这题是连连看算法。
- BFS--cf793b Igor and his way to work
- CF-Tinkoff Challenge-Elimination Round-B-Igor and his way to work
- Codeforces 689B Mike and Shortcuts DFS or BFS
- 【CodeForces 510B】 Fox And Two Dots (dfs+bfs)
- 安装matplotlib basemap
- [AHK]用DOS命令Dir把一个文件夹里的文件名保存到txt中
- 多线程同步的方法
- opencv3 环境安装及实验surf特征的描述符匹配文章整理
- internet协议入门——你该懂得知识
- Codeforces 793B Igor and his way to work (dfs/bfs
- 集合类常见问题
- SpringMVC小结
- POJ 3620 Avoid The Lakes
- android5.1去掉开机锁屏
- java web 使用jdbc 过程错误记录
- Java学习【知识点及代码8】
- JSP(6)简单购物车实现
- 利用JS实现基本的图片轮播功能,包括鼠标的经过事件