codeforces 793B. Igor and his way to work
B. Igor and his way to work
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.
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.
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.
5 5
..S..
****.
T....
****.
.....
YES
5 5
S....
****.
.....
.****
..T..
#include<bits/stdc++.h> using namespace std; const int Max = 1009; char f[Max][Max]; int d[Max][Max]; int m,n; int fuck(int x,int y,int v) { int i=x+1;int j=y; while(i<m&&d[i][j]==-1&&f[i][j]!='*') d[i++][j]=v; i=x-1;j=y; while(i>=0&&d[i][j]==-1&&f[i][j]!='*') d[i--][j]=v; i=x;j=y+1; while(j<n&&d[i][j]==-1&&f[i][j]!='*') d[i][j++]=v; i=x;j=y-1; while(j>=0&&d[i][j]==-1&&f[i][j]!='*') d[i][j--]=v; } int main() { ios_base::sync_with_stdio(0); int i,j; cin>>m>>n; memset(d, -1, Max*Max*sizeof(int)); for(i=0;i<m;i++) for(j=0;j<n;j++) cin>>f[i][j]; for(i=0;i<m;i++) for(j=0;j<n;j++) if(f[i][j]=='S'){ d[i][j]=0; //cout<<i<<j; fuck(i,j,0); } for(i=0;i<m;i++) for(j=0;j<n;j++) if(d[i][j]==0){ fuck(i,j,1);} for(i=0;i<m;i++) for(j=0;j<n;j++) if(d[i][j]==1){ fuck(i,j,2);} for(i=0;i<m;i++) for(j=0;j<n;j++) if(f[i][j]=='T'){ cout<<(d[i][j]==-1?"NO":"YES");} return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步