B. Igor and his way to work

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 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 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:


真的不难啊,不知道什么情况考虑不周到,唉,在第七个测试数据WA哭。

比赛时打的代码如下:太困了,明天写了正确的再贴。

//
//  main.cpp
//  B. Igor and his way to work
//
//  Created by 徐智豪 on 2017/4/24.
//  Copyright © 2017年 徐智豪. All rights reserved.
//

#include <iostream>
#include <string>
using namespace std;
struct dir
{
    int x;
    int y;
}direction[5];
char map[1010][1010];
bool vis[1010][1010]={false};
int m,n;

string ans;
int findway(int di,int turn,int x,int y,int thecount)
{
    if(x>n||x<1||y>m||y<1)
        return 0;
    if(vis[x][y]||map[x][y]=='*')
    {
        vis[x][y]=1;thecount++;
        return 0;
    }
    if(map[x][y]=='T'&&!vis[x][y])
    {
        vis[x][y]=1;thecount++;
        if(turn<=2)
        {
            ans+="YES";return 0;
        }
        else
        {
            ans+="NO";return 0;
        }
    }
    vis[x][y]=1;thecount++;
        for(int i=1;i<=4;i++)
        {
            int tx=x+direction[i].x;
            int ty=y+direction[i].y;
            if(di!=i)
                findway(i,turn+1,tx,ty,thecount);
            else
                findway(di,turn,tx,ty,thecount);
        }
    if(thecount==1&&ans.size()==0)
        ans+="NO";
    return 0;
}
int main(int argc, const char * argv[]) {
    direction[1].x=1;direction[1].y=0;
    direction[2].x=-1;direction[2].y=0;
    direction[3].x=0;direction[3].y=1;
    direction[4].x=0;direction[4].y=-1;
    cin>>n>>m;
    int x,y;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cin>>map[i][j];
            if(map[i][j]=='S')
            {
                x=i;y=j;
            }
            
        }
    int turn=-1;
    int di=0;
    findway(di,turn,x,y,0);
    cout<<ans<<endl;
    return 0;
}


差点忘记了233333,这是AC的代码:还是看了别人的才学习到的,为什么WA呢?因为我智障的把VIS数组状态设太少了,若是转弯数量不同方向不同判断VIS岂不是因为前面已经走过了所以给跳过了吗?事实上应该加上方向和转弯次数 的判断状态。

代码如下:

//
//  main.cpp
//  B. Igor and his way to work
//
//  Created by 徐智豪 on 2017/4/24.
//  Copyright © 2017年 徐智豪. All rights reserved.
//

#include <iostream>
#include <string>
using namespace std;
struct dir
{
    int x;
    int y;
}direction[5];
char map[1010][1010];
int vis[1010][1010][5][5]={0};
int m,n;
int minturn=100000;
bool findtheway=0;
void findway(int di,int turn,int x,int y)
{
    if(x<1||x>n||y<1||y>m)
        return ;
    if(map[x][y]=='*')
        return ;
    if(turn>2)
        return ;
    if(vis[x][y][di][turn])
        return ;
    if(map[x][y]=='T')
        findtheway=true;
    vis[x][y][di][turn]=1;
    for(int i=1;i<=4;i++)
    {
        int nx=x+direction[i].x,ny= y+direction[i].y;
        findway(i, turn+(i!=di&&di!=0), nx,ny);
    }
}
int main(int argc, const char * argv[]) {
    direction[1].x=1;direction[1].y=0;
    direction[2].x=-1;direction[2].y=0;
    direction[3].x=0;direction[3].y=1;
    direction[4].x=0;direction[4].y=-1;
    cin>>n>>m;
    int x,y;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cin>>map[i][j];
            if(map[i][j]=='S')
            {
                x=i;
                y=j;
            }
        }
    findway(0,0,x,y);
    if(findtheway==true)
        cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
        return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值