Submission #336469

# Submission time Handle Problem Language Result Execution time Memory
336469 2020-12-15T11:58:30 Z parsabahrami Zoo (COCI19_zoo) C++17
110 / 110
47 ms 6252 KB
#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

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 time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 876 KB Output is correct
6 Correct 2 ms 876 KB Output is correct
7 Correct 1 ms 876 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 1 ms 748 KB Output is correct
10 Correct 1 ms 876 KB Output is correct
11 Correct 1 ms 748 KB Output is correct
12 Correct 1 ms 876 KB Output is correct
13 Correct 1 ms 748 KB Output is correct
14 Correct 2 ms 876 KB Output is correct
15 Correct 1 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 876 KB Output is correct
6 Correct 2 ms 876 KB Output is correct
7 Correct 1 ms 876 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 1 ms 748 KB Output is correct
10 Correct 1 ms 876 KB Output is correct
11 Correct 1 ms 748 KB Output is correct
12 Correct 1 ms 876 KB Output is correct
13 Correct 1 ms 748 KB Output is correct
14 Correct 2 ms 876 KB Output is correct
15 Correct 1 ms 748 KB Output is correct
16 Correct 11 ms 6252 KB Output is correct
17 Correct 11 ms 6144 KB Output is correct
18 Correct 11 ms 6252 KB Output is correct
19 Correct 12 ms 6252 KB Output is correct
20 Correct 15 ms 6124 KB Output is correct
21 Correct 43 ms 5924 KB Output is correct
22 Correct 44 ms 5996 KB Output is correct
23 Correct 46 ms 5996 KB Output is correct
24 Correct 47 ms 6252 KB Output is correct
25 Correct 43 ms 6252 KB Output is correct
26 Correct 45 ms 5996 KB Output is correct
27 Correct 42 ms 5996 KB Output is correct
28 Correct 43 ms 6044 KB Output is correct
29 Correct 44 ms 6252 KB Output is correct
30 Correct 44 ms 6124 KB Output is correct