Submission #977034

# Submission time Handle Problem Language Result Execution time Memory
977034 2024-05-07T10:38:10 Z farica Mecho (IOI09_mecho) C++14
37 / 100
160 ms 7260 KB
#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<int, pair<int,int>>>pq;
    pq.push({dist[sx][sy], {sx, sy}});
    while(!pq.empty()) {
        int val = pq.top().first, x = pq.top().second.first, y = pq.top().second.second, eD=abs(x-sx) + abs(y-sy);
        pq.pop();
        if(visited[x][y]) continue;
        visited[x][y] = 1;
        if(dist[x][y] < eD/s + (eD % s > 0 ? 1 : 0)) continue;
        if(grid[x][y] == 'D') {
            cout <<  val - (eD/s + (eD % s > 0 ? 1 : 0)) << endl;
            return;
        }
        if(x && grid[x-1][y] != 'T') pq.push({min(val, dist[x-1][y]), {x-1, y}});
        if(y && grid[x][y-1] != 'T') pq.push({min(val, dist[x][y-1]), {x, y-1}});
        if(x+1<n && grid[x+1][y] != 'T') pq.push({min(val, dist[x+1][y]), {x+1, y}});
        if(y+1<n && grid[x][y+1] != 'T') pq.push({min(val, dist[x][y+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

mecho.cpp: In function 'void solve()':
mecho.cpp:48:96: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   48 |         int val = pq.top().first, x = pq.top().second.first, y = pq.top().second.second, eD=abs(x-sx) + abs(y-sy);
      |                                                                                             ~~~^~~~~~
mecho.cpp:48:108: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
   48 |         int val = pq.top().first, x = pq.top().second.first, y = pq.top().second.second, eD=abs(x-sx) + abs(y-sy);
      |                                                                                                         ~~~^~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Incorrect 0 ms 600 KB Output isn't correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Incorrect 0 ms 344 KB Output isn't correct
6 Incorrect 1 ms 348 KB Output isn't correct
7 Incorrect 160 ms 6596 KB Output isn't correct
8 Incorrect 0 ms 348 KB Output isn't correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Incorrect 1 ms 348 KB Output isn't correct
11 Incorrect 1 ms 348 KB Output isn't correct
12 Incorrect 1 ms 348 KB Output isn't correct
13 Incorrect 0 ms 348 KB Output isn't correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 2 ms 1372 KB Output is correct
34 Correct 2 ms 1372 KB Output is correct
35 Incorrect 24 ms 1884 KB Output isn't correct
36 Correct 3 ms 1880 KB Output is correct
37 Correct 2 ms 1884 KB Output is correct
38 Incorrect 31 ms 2324 KB Output isn't correct
39 Correct 3 ms 2140 KB Output is correct
40 Correct 3 ms 2140 KB Output is correct
41 Incorrect 40 ms 2804 KB Output isn't correct
42 Correct 5 ms 2904 KB Output is correct
43 Correct 3 ms 2664 KB Output is correct
44 Incorrect 57 ms 3236 KB Output isn't correct
45 Correct 5 ms 3164 KB Output is correct
46 Correct 4 ms 3164 KB Output is correct
47 Incorrect 60 ms 3676 KB Output isn't correct
48 Correct 5 ms 3928 KB Output is correct
49 Correct 5 ms 3676 KB Output is correct
50 Incorrect 71 ms 4228 KB Output isn't correct
51 Correct 6 ms 4188 KB Output is correct
52 Correct 5 ms 4188 KB Output is correct
53 Incorrect 90 ms 4820 KB Output isn't correct
54 Correct 7 ms 4696 KB Output is correct
55 Correct 6 ms 4700 KB Output is correct
56 Incorrect 98 ms 5572 KB Output isn't correct
57 Correct 8 ms 5468 KB Output is correct
58 Correct 6 ms 5508 KB Output is correct
59 Incorrect 116 ms 6168 KB Output isn't correct
60 Correct 10 ms 6232 KB Output is correct
61 Correct 7 ms 6236 KB Output is correct
62 Incorrect 130 ms 7004 KB Output isn't correct
63 Incorrect 47 ms 6736 KB Output isn't correct
64 Incorrect 59 ms 6704 KB Output isn't correct
65 Incorrect 58 ms 6832 KB Output isn't correct
66 Incorrect 53 ms 6984 KB Output isn't correct
67 Incorrect 71 ms 6704 KB Output isn't correct
68 Incorrect 30 ms 7004 KB Output isn't correct
69 Correct 31 ms 6748 KB Output is correct
70 Incorrect 29 ms 7004 KB Output isn't correct
71 Incorrect 29 ms 6768 KB Output isn't correct
72 Incorrect 18 ms 6236 KB Output isn't correct
73 Incorrect 14 ms 7260 KB Output isn't correct
74 Correct 66 ms 7204 KB Output is correct
75 Correct 75 ms 7256 KB Output is correct
76 Correct 59 ms 7256 KB Output is correct
77 Correct 77 ms 7260 KB Output is correct
78 Correct 136 ms 6736 KB Output is correct
79 Correct 72 ms 6872 KB Output is correct
80 Correct 60 ms 6880 KB Output is correct
81 Correct 87 ms 6748 KB Output is correct
82 Correct 83 ms 6864 KB Output is correct
83 Correct 143 ms 6672 KB Output is correct
84 Correct 118 ms 7092 KB Output is correct
85 Correct 89 ms 6748 KB Output is correct
86 Correct 121 ms 6748 KB Output is correct
87 Correct 125 ms 6740 KB Output is correct
88 Correct 148 ms 7004 KB Output is correct
89 Correct 133 ms 6740 KB Output is correct
90 Correct 160 ms 6736 KB Output is correct
91 Correct 134 ms 6736 KB Output is correct
92 Correct 146 ms 6992 KB Output is correct