Submission #977510

#TimeUsernameProblemLanguageResultExecution timeMemory
977510faricaMecho (IOI09_mecho)C++14
16 / 100
179 ms24084 KiB
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int MAX_N = 4e5 + 5;

bool visited[800][800];

void solve() {
    int n, s, sx, sy;
    cin >> n >> s;
    vector<vector<int>>dist(n, vector<int>(n, -1));
    string grid[n];
    queue<pair<int,int>>q;
    for(int i=0; i<n; ++i) {
        cin >> grid[i];
        for(int j=0; j<n; ++j) {
            if(grid[i][j] == 'H') {
                q.push({i, j});
                dist[i][j] = 0;
            } else if(grid[i][j] == 'M') sx=i, sy=j;
        }
    }
    while(!q.empty()) {
        int i = q.front().first, j = q.front().second;
        q.pop();
        if(i && dist[i-1][j] == -1 && grid[i-1][j] != 'T') {
            dist[i-1][j] = dist[i][j] + 1;
            q.push({i-1, j});
        }
        if(j && dist[i][j-1] == -1 && grid[i][j-1] != 'T') {
            dist[i][j-1] = dist[i][j] + 1;
            q.push({i, j-1});
        }
        if(i<n-1 && dist[i+1][j] == -1 && grid[i+1][j] != 'T') {
            dist[i+1][j] = dist[i][j] + 1;
            q.push({i+1, j});
        }
        if(j<n-1 && dist[i][j+1] == -1 && grid[i][j+1] != 'T') {
            dist[i][j+1] = dist[i][j] + 1;
            q.push({i, j+1});
        }
    }
    priority_queue<pair<pair<int,int>, pair<int,int>>>pq;
    pq.push({{dist[sx][sy], 0}, {sx, sy}});
    while(!pq.empty()) {
        int val = pq.top().first.first, x = pq.top().second.first, y = pq.top().second.second, eD = pq.top().first.second;
        pq.pop();
        if(visited[x][y] or val<0) continue;
        int cost = eD/s + (eD % s > 0 ? 1 : 0);
        if(grid[x][y] == 'D') {
            cout << val << endl;
            return;
        } else visited[x][y] = 1;
        if(x && grid[x-1][y] != 'T') pq.push({{min(val, dist[x-1][y] - cost), eD + 1}, {x-1, y}});
        if(y && grid[x][y-1] != 'T') pq.push({{min(val, dist[x][y-1] - cost), eD + 1}, {x, y-1}});
        if(x+1<n && grid[x+1][y] != 'T') pq.push({{min(val, dist[x+1][y] - cost), eD + 1}, {x+1, y}});
        if(y+1<n && grid[x][y+1] != 'T') pq.push({{min(val, dist[x][y+1] - cost), eD + 1}, {x, y+1}});
    }
    cout << -1 << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    while(t--) solve();

    return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'void solve()':
mecho.cpp:46:26: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
   46 |     pq.push({{dist[sx][sy], 0}, {sx, sy}});
      |                          ^
mecho.cpp:46:22: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   46 |     pq.push({{dist[sx][sy], 0}, {sx, sy}});
      |                      ^
#Verdict Execution timeMemoryGrader output
Fetching results...