Submission #1129335

#TimeUsernameProblemLanguageResultExecution timeMemory
1129335subham_krrTracks in the Snow (BOI13_tracks)C++20
2.19 / 100
1483 ms222728 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
    int n, m;
    cin >> n >> m;

    int r = 0, f = 0;
    vector<vector<char> > g(n, vector<char>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> g[i][j];
            if (g[i][j] == 'R') {
                r++;
            } else if (g[i][j] == 'F') {
                f++;
            }
        }
    }

    // Define direction vectors for 4 directions
   	vector<pair<int,int > >v;
	v.push_back({-1,0});
		v.push_back({0,-1});
		v.push_back({0,1});
		v.push_back({1,0});

    // Initialize the level array
    vector<vector<int> > level(n, vector<int>(m, LLONG_MAX));
    level[0][0] = 0;

    // 0/1 BFS using deque
    deque<pair<int, int> > dq;
    dq.push_front({0, 0});

    while (!dq.empty()) {
        pair<int, int> p = dq.front();
        dq.pop_front();

        for (int i = 0; i < 4; i++) {
            int d = p.first + v[i].first;
            int e = p.second + v[i].second;

            // Boundary check
            if (d >= 0 && d < n && e >= 0 && e < m && g[d][e] != '.') {
                int wt = (g[p.first][p.second] != g[d][e]) ? 1 : 0;
                if (wt + level[p.first][p.second] < level[d][e]) {
                    level[d][e] = wt + level[p.first][p.second];
                    if (wt == 1) {
                        dq.push_back({d, e}); // Push to the back if weight is 1
                    } else {
                        dq.push_front({d, e}); // Push to the front if weight is 0
                    }
                }
            }
        }
    }

    // Adjust result for corner cases
    int result = level[n-1][m-1];
    if (result == LLONG_MAX) {
        cout << -1 << endl; // If destination is unreachable
    } else {
        cout << result + (f != 0) + (r != 0) << endl; // Include additional penalties if required
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...