196B - Infinite Maze - CodeForces Solution


dfs and similar graphs *2000

Please click on ads to support us..

C++ Code:

#include <iostream>

using namespace std;

const int N = 5000;
int a, b, n, m;
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
char s[N][N];
bool mark[N][N];

void dfs(int x, int y) {
    mark[x][y] = true;
    for (int i = 0; i < 4; i++) {
        int X = x + dx[i], Y = y + dy[i];
        X = (X + 3 * n) % (3 * n);
        Y = (Y + 3 * m) % (3 * m);
        if (s[X][Y] != '#' && !mark[X][Y]) {
            if (s[X][Y] == 'S') {
                cout << "Yes\n";
                exit(0);
            }
            dfs(X, Y);
        }
    }
}

int main() {
    cin >> n >> m;
    for (int i = n; i < 2 * n; i++) {
        for (int j = m; j < 2 * m; j++) {
            cin >> s[i][j];
            if (s[i][j] == 'S') {
                a = i, b = j;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            s[i][j] = s[i + n][j + m];
        }
    }

    for (int i = n; i < 2 * n; i++) {
        for (int j = 0; j < m; j++) {
            s[i][j] = s[i][j + m];
        }
    }

    for (int i = 2 * n; i < 3 * n; i++) {
        for (int j = 0; j < m; j++) {
            s[i][j] = s[i - n][j + m];
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 2 * m; j < 3 * m; j++) {
            s[i][j] = s[i + n][j - m];
        }
    }

    for (int i = n; i < 2 * n; i++) {
        for (int j = 2 * m; j < 3 * m; j++) {
            s[i][j] = s[i][j - m];
        }
    }

    for (int i = 2 * n; i < 3 * n; i++) {
        for (int j = 2 * m; j < 3 * m; j++) {
            s[i][j] = s[i - n][j - m];
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = m; j < 2 * m; j++) {
            s[i][j] = s[i + n][j];
        }
    }

    for (int i = 2 * n; i < 3 * n; i++) {
        for (int j = m; j < 2 * m; j++) {
            s[i][j] = s[i - n][j];
        }
    }
    dfs(a, b);
    cout << "No\n";
    return 0;
}


Comments

Submit
0 Comments
More Questions

734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality