View Code of Problem 3684

#include <bits/stdc++.h>
using namespace std;

int main() {

    int n, m, t;
    while (cin >> n >> m >> t) {
        vector<vector<char>> mat(n, vector<char>(m));
        vector<vector<int>> visited(n, vector<int>(m, 0));
        vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        int sr = -1, sc = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> mat[i][j];
                if (mat[i][j] == 'S') {
                    sr = i, sc = j;
                }
            }
        }

        auto bfs = [&](int i, int j) -> bool {
            queue<pair<int, int>> q1, q2;
            q1.push({i, j});
            visited[i][j] = 1;
            while (t != 0 && !q1.empty()) {
                auto [sx, sy] = q1.front();
                q1.pop();


                for (auto [dx, dy] : dirs) {
                    if (sx+dx < 0 || sx+dx >= n || sy+dy < 0 || sy+dy >= m 
                        || visited[sx+dx][sy+dy] || mat[sx+dx][sy+dy] == '#') {
                        continue;
                    }
                    if (mat[sx+dx][sy+dy] == 'F') return true;

                    q2.push({sx+dx, sy+dy});
                    visited[sx+dx][sy+dy] = 1;
                }

                if (q1.empty()) {
                    t--;
                    q1.swap(q2);
                }
            }
            return false;
        }

        if (bfs(sr, sc)) {
            cout << "Happy\n";
        } else {
            cout << "Cry\n";
        }
    }



}

/*
Main.cc: In lambda function:
Main.cc:26:22: warning: structured bindings only available with -std=c++17 or -std=gnu++17
                 auto [sx, sy] = q1.front();
                      ^
Main.cc:30:27: warning: structured bindings only available with -std=c++17 or -std=gnu++17
                 for (auto [dx, dy] : dirs) {
                           ^
Main.cc: In function 'int main()':
Main.cc:49:9: error: expected ',' or ';' before 'if'
         if (bfs(sr, sc)) {
         ^~
Main.cc:51:11: error: 'else' without a previous 'if'
         } else {
           ^~~~
*/

Double click to view unformatted code.


Back to problem 3684