A - Igor and his way to work CodeForces - 793B (D

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.

Example
Input
5 5
..S..
****.
T....
****.
.....
Output
YES
Input
5 5
S....
****.
.....
.****
..T..
Output
NO
Note

The first sample is shown on the following picture:


In the second sample it is impossible to reach Igor's office using less that 4turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture.


题意:给出S和T表示起点和终点。‘.'表示路,‘*'表示墙,问你是否能找出一条由S通往T的路,这条路的拐弯次数不能超过两次。

思路:很明显的Dfs,横向扫一下,竖向扫一下,搜到墙或者转弯次数超过2回朔就好,关键是看你转弯统计是否做得巧妙,话不多说,看代码。

#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
int n,m,ch,t,visit[maxn][maxn][5][5];
char str[maxn][maxn];
void dfs(int x,int y,int c,int t){
     if(x<1||y<1||x>n||y>m) return;
     if(t>2||str[x][y]=='*'||visit[x][y][c][t]||ch) return;
      visit[x][y][c][t]=1;
     if(str[x][y]=='T') {ch=1; return;}
    if(!c) dfs(x-1,y,c,t),dfs(x+1,y,c,t);
    else dfs(x,y-1,c,t),dfs(x,y+1,c,t);
    dfs(x,y,!c,t+1);
}
int main()
{
    int x,y;
    ch=0;
    memset(visit,0,sizeof(0));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) {cin>>str[i][j];if(str[i][j]=='S')x=i,y=j;}
    dfs(x,y,0,0);dfs(x,y,1,0);
    if(ch)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}











  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值