B. Igor and his way to work

来源:互联网 发布:匡恩网络物联网安全 编辑:程序博客网 时间:2024/05/20 05:09

B. Igor and his way to work
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
5 5..S..****.T....****......
output
YES
input
5 5S....****.......****..T..
output
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;}






0 0