Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef vector<int> vi;
- typedef pair<int, int> pi;
- #define f first
- #define s second
- #define pb push_back
- #define mp make_pair
- #define loop(n) for(int i=0; i<n; i++)
- #define rep(i, a, n) for(int i=a; i<n; i++)
- #define file_read freopen("input.txt", "r", stdin); \
- freopen("output.txt", "w", stdout);
- #define tc int t; cin>>t; while(t--)
- #define endl "\n"
- #define usainbolt cin.tie(0) -> sync_with_stdio(0)
- char mat[1000][1000];
- int n, m;
- /*
- 0 - up
- 1 - right
- 2 - down
- 3 - left
- */
- queue<pair<uint16_t, uint16_t>> nodes;
- bool inbounds(int x, int y){
- if(x<m && x>=0 && y<n && y>=0 && mat[y][x]!='*')
- return true;
- else
- return false;
- }
- void hasreached(int x, int y){
- if(mat[y][x]=='T'){
- cout<<"YES";
- exit(0);
- }
- }
- void mark_tiles(){
- for(int n = nodes.size(), i=0; i<n; i++){
- int x = nodes.front().first, y = nodes.front().second;
- nodes.pop();
- bool b1, b2;
- b1 = b2 = true;
- switch(mat[y][x]){
- //turn horizontal
- case 1:
- for(int l=x-1, r=x+1; b1 || b2 == true; r++, l--){
- if(inbounds(l, y) && b1 && mat[y][l]!=2){
- hasreached(l, y);
- nodes.push(make_pair(l, y));
- mat[y][l] = 2;
- }
- else b1 = false;
- if(inbounds(r, y) && b2 && mat[y][r]!=2){
- hasreached(r, y);
- nodes.push(make_pair(r, y));
- mat[y][r] = 2;
- }
- else b2 = false;
- }
- break;
- //turn vertical
- case 2:
- for(int u=y-1, d=y+1; b1 || b2 == true; d++, u--){
- if(inbounds(x, u) && b1 && mat[u][x]!=1){
- hasreached(x, u);
- nodes.push(make_pair(x, u));
- mat[u][x] = 1;
- }
- else b1 = false;
- if(inbounds(x, d) && b2 && mat[d][x]!=1){
- hasreached(x, d);
- nodes.push(make_pair(x, d));
- mat[d][x] = 1;
- }
- else b2 = false;
- }
- break;
- }
- }
- }
- // ..S..****.T....****......
- int main(void){
- usainbolt;
- //file_read
- cin>>n>>m;
- int h_x, h_y, b_x, b_y;
- for(int i=0; i<n; i++){
- for(int j=0; j<m; j++){
- char inp;
- cin>>inp;
- mat[i][j] = inp;
- if(mat[i][j]=='S')
- {
- h_x = j;
- h_y = i;
- mat[i][j] = 1;
- }
- }
- }
- bool b1, b2, b3, b4;
- b1 = b2 = b3 = b4 = true;
- for(int u=h_y-1, d=h_y+1, l=h_x-1, r=h_x+1; b1 || b2 || b3 || b4 == true; r++, l--, u--, d++){
- if(inbounds(h_x, u) && b1){
- hasreached(h_x, u);
- nodes.push(make_pair(h_x, u));
- mat[u][h_x] = 1;
- }
- else b1 = false;
- if(inbounds(h_x, d) && b2){
- hasreached(h_x, d);
- nodes.push(make_pair(h_x, d));
- mat[d][h_x] = 1;
- }
- else b2 = false;
- if(inbounds(l, h_y) && b3){
- hasreached(l, h_y);
- nodes.push(make_pair(l, h_y));
- mat[h_y][l] = 2;
- }
- else b3 = false;
- if(inbounds(r, h_y) && b4){
- hasreached(r, h_y);
- nodes.push(make_pair(r, h_y));
- mat[h_y][r] = 2;
- }
- else b4=false;
- }
- for(int i=0; i<2; i++){
- mark_tiles();
- }
- /* while (!nodes.empty())
- {
- cout << nodes.front().first << " " << nodes.front().second<<endl;
- nodes.pop();
- }
- */
- cout << "NO";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement