Submission #916661

# Submission time Handle Problem Language Result Execution time Memory
916661 2024-01-26T08:53:50 Z May27_th Tracks in the Snow (BOI13_tracks) C++17
100 / 100
660 ms 122464 KB
#include <bits/stdc++.h>
#define int64_t long long
#define double long double
using namespace std;
using type = int64_t;
const long long mod = 1000000007, inf = 1e18;
const int base = 33;
const int N = 2e5 + 10;
const int LG = 20;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void Minimize(int64_t &a, int64_t b) {if(b < a) a = b;}
void Maximize(int &a, int b) {if(b > a) a = b;}
void Add(int64_t& a, int64_t b) {a = a + b; a %= mod;}
void Dec(int64_t& a, int64_t b) {a = a - b + mod; a %= mod;}

int h, w; char g[4010][4010]; int d[4005][4005];
void Solve(void)
{
    cin >> h >> w;
    for(int i = 1; i <= h; i ++){
        for(int j = 1; j <= w; j ++){
            cin >> g[i][j];
            d[i][j] = 1e9;
        }
    }
    deque<pair<int, int>>q;
    d[1][1] = 1;
    q.push_front(make_pair(1, 1));
    while(!q.empty()){
        int u = q.front().first;
        int v = q.front().second;
        q.pop_front();
        for(int i = 0; i < 4; i ++){
            int nu = u + dx[i];
            int nv = v + dy[i];
            if(nu >= 1 && nv >= 1 && nu <= h && nv <= w){
                int weight = (g[u][v] != g[nu][nv]);
                if(g[nu][nv] == '.') continue;
                if(d[nu][nv] > d[u][v] + weight){
                    d[nu][nv] = d[u][v] + weight;
                    //cout << d[nu][nv] << "\n";
                    if(weight == 1) q.push_back(make_pair(nu, nv));
                    else q.push_front(make_pair(nu, nv));
                }
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= h; i ++){
        for(int j = 1; j <= w; j ++){
            //cout << d[i][j] << " ";
            if(g[i][j] != '.')
                ans = max(ans, d[i][j]);
        }
        //cout << "\n";
    }
    cout << ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    if(fopen("poetry.in", "r")){
        freopen("poetry.in", "r", stdin);
        freopen("poetry.out", "w", stdout);
    }
    if(fopen("A.inp", "r")){
        freopen("A.inp", "r", stdin);
        freopen("A.out", "w", stdout);
    }
    int tc = 1;
    //cin >> tc;
    while(tc --){
        Solve();
    }
}

Compilation message

tracks.cpp: In function 'int main()':
tracks.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         freopen("poetry.in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen("poetry.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |         freopen("A.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
tracks.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen("A.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 13144 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 7 ms 12892 KB Output is correct
5 Correct 3 ms 10588 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 3 ms 6660 KB Output is correct
11 Correct 3 ms 6488 KB Output is correct
12 Correct 5 ms 10584 KB Output is correct
13 Correct 4 ms 10584 KB Output is correct
14 Correct 4 ms 10584 KB Output is correct
15 Correct 10 ms 12636 KB Output is correct
16 Correct 11 ms 12912 KB Output is correct
17 Correct 9 ms 12636 KB Output is correct
18 Correct 8 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 78428 KB Output is correct
2 Correct 40 ms 27228 KB Output is correct
3 Correct 277 ms 78912 KB Output is correct
4 Correct 80 ms 41556 KB Output is correct
5 Correct 147 ms 62124 KB Output is correct
6 Correct 660 ms 92636 KB Output is correct
7 Correct 14 ms 78684 KB Output is correct
8 Correct 13 ms 78428 KB Output is correct
9 Correct 2 ms 4700 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 14 ms 78428 KB Output is correct
12 Correct 2 ms 6492 KB Output is correct
13 Correct 40 ms 27228 KB Output is correct
14 Correct 25 ms 20828 KB Output is correct
15 Correct 21 ms 22872 KB Output is correct
16 Correct 18 ms 10848 KB Output is correct
17 Correct 101 ms 43604 KB Output is correct
18 Correct 88 ms 43596 KB Output is correct
19 Correct 88 ms 41828 KB Output is correct
20 Correct 62 ms 37460 KB Output is correct
21 Correct 168 ms 64080 KB Output is correct
22 Correct 156 ms 62088 KB Output is correct
23 Correct 182 ms 53844 KB Output is correct
24 Correct 149 ms 64084 KB Output is correct
25 Correct 485 ms 78988 KB Output is correct
26 Correct 398 ms 122464 KB Output is correct
27 Correct 545 ms 105616 KB Output is correct
28 Correct 629 ms 92392 KB Output is correct
29 Correct 648 ms 90880 KB Output is correct
30 Correct 605 ms 96436 KB Output is correct
31 Correct 405 ms 66692 KB Output is correct
32 Correct 473 ms 94220 KB Output is correct