codeforce 793 B Igor and his way to work

Igor是一名财务分析师,他需要从家出发前往银行上班。由于城市部分道路施工且他的车只能转弯两次,需要找到一条合适的路径。本篇介绍了一个通过深度优先搜索(DFS)解决这一路径寻找问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 2turns. The path using exactly 4 turns is shown on this picture:



就是dfs()

不过vis我们需要特殊处理

每个点vis有4种方向记录一下最小的转弯

如果不满足就跳出


#include<stdio.h>
#include<string.h>
using namespace std;
int f=0;
char maps[1013][1013];
int vis[1003][1003][5];
int dx[4]= {0,0,1,-1};
int dy[4]= {1,-1,0,0};
int n,m;
void dfs(int x,int y,int l,int c)
{
    if(x<0||x>=n||y<0||y>=m)return;
    //if(vis[x][y])return;
    if(maps[x][y]=='*')return;
    if(vis[x][y][l]<=c)return;
    if(c>2)
    {
        //printf("x:%d y:%d\n",x,y);
        return;
    }
     vis[x][y][l]=c;
    if(maps[x][y]=='T')
    {
        f=1;
        return;
    }
   // vis[x][y]=1;
    for(int i=0; i<4; i++)
    {
        int xx=x+dx[i];
        int yy=y+dy[i];
        if(l==i)
            dfs(xx,yy,l,c);
        else dfs(xx,yy,i,c+1);
    }
    return;
}
int main()
{
    //int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int sx,sy;
        f=0;
        for(int i=0; i<n; i++)
        {
            scanf("%s",maps[i]);
            for(int j=0; j<m; j++)
            {
                if(maps[i][j]=='S')
                    sx=i,sy=j;
            }
        }
        memset(vis,0x1f1f1f,sizeof(vis));
        for(int i=0; i<4; i++)
        {
            int xx,yy;
            xx=sx+dx[i];
            yy=sy+dy[i];
            dfs(xx,yy,i,0);
        }
        if(f)
            printf("YES\n");
        else printf("NO\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值