Submission #336469

#TimeUsernameProblemLanguageResultExecution timeMemory
336469parsabahramiZoo (COCI19_zoo)C++17
110 / 110
47 ms6252 KiB
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 1e3 + 10, MOD = 1e9 + 7;
char C[N][N];
int ans, dp[N][N], dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0}, n, m;

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%s", C[i] + 1);
    }
    for (int i = 1; i <= n; i++) fill(dp[i], dp[i] + N, MOD);
    dp[1][1] = 1;
    deque<pii> dq; 
    dq.push_back({1, 1});
    while (SZ(dq)) {
        int x = dq.front().F, y = dq.front().S;
        dq.pop_front();
        ans = max(ans, dp[x][y]);
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx > n || nx <= 0 || ny <= 0 || ny > m || C[nx][ny] == '*') continue;
            if (dp[nx][ny] > dp[x][y] + (C[x][y] != C[nx][ny])) {
                dp[nx][ny] = dp[x][y] + (C[x][y] != C[nx][ny]);
                if (C[nx][ny] != C[x][y]) dq.push_back({nx, ny});
                else dq.push_front({nx, ny});
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

Compilation message (stderr)

zoo.cpp: In function 'int main()':
zoo.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   17 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
zoo.cpp:19:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   19 |         scanf("%s", C[i] + 1);
      |         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...