Submission #443707

#TimeUsernameProblemLanguageResultExecution timeMemory
443707BeanZTracks in the Snow (BOI13_tracks)C++14
100 / 100
965 ms143136 KiB
// I_Love_LPL 1y0m3d
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define endl '\n'
const int N = 4005;
long long mod = 1000007;
const int lim = 4e5 + 5;
const int lg = 20;
const int base = 311;
const long double eps = 1e-6;
ll nx[4] = {0, 0, 1, -1};
ll ny[4] = {1, -1, 0, 0};
char a[N][N];
ll dp[N][N];
struct viet{
    ll x, y, v;
};
ll n, m;
bool check(ll x, ll y){
    if (x < 1 || x > n || y < 1 || y > m || a[x][y] == '.') return false;
    else return true;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    if (fopen("tests.inp", "r")){
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    cin >> n >> m;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= m; j++){
            cin >> a[i][j];
            dp[i][j] = 1e9;
        }
    }
    deque<viet> q;
    q.push_back({1, 1, 1});
    dp[1][1] = 1;
    ll ans = 1;
    while (q.size()){
        viet x = q.front();
        ans = max(ans, x.v);
        q.pop_front();
        for (int i = 0; i < 4; i++){
            ll nwx = x.x + nx[i];
            ll nwy = x.y + ny[i];
            if (!check(nwx, nwy)) continue;
            ll cost = x.v + (a[nwx][nwy] != a[x.x][x.y]);
            if (dp[nwx][nwy] > cost){
                dp[nwx][nwy] = cost;
                if (cost == x.v) q.push_front({nwx, nwy, cost});
                else q.push_back({nwx, nwy, cost});
            }
        }
    }
    cout << ans;
}
/*
Ans:

Out:
*/

Compilation message (stderr)

tracks.cpp: In function 'int main()':
tracks.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...