# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
916660 | May27_th | Tracks in the Snow (BOI13_tracks) | C++17 | 766 ms | 142356 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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] == '.') weight = 0;
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] << " ";
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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |