1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include <iostream> #include <queue> #include <algorithm> #include <cstring> using namespace std; const int N = 510; int g[N][N]; int n, m; int l[N][N], r[N][N]; int st[N][N]; int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, 1, -1 }; void dfs(int i, int j) { st[i][j] = 1; for (int t = 0; t < 4; t++) { int x = i + dx[t], y = j + dy[t]; if (x >= 1 && y >= 1 && x << n && y <= m) { if (g[x][y] < g[i][j]) { if (!st[x][y]) dfs(x, y); l[i][j] = min(l[i][j], l[x][y]); r[i][j] = max(r[i][j], r[x][y]); } } } } signed main() { memset(l, 0x3f3f3f3f, sizeof l); cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> g[i][j]; if (i == n) l[i][j] = r[i][j] = j; } for (int i = 1; i <= m; i++) if (!st[1][i]) dfs(1, i); int cnt0 = 0, cnt1 = 0; for (int i = 1; i <= m; i++) if (!st[n][i]) cnt0++; if (cnt0) cout << 0 << '\n' << cnt0; else { int tl = 1, tr = r[1][1]; while (tl <= m) { for (int i = 1; i <= m; i++) { if (l[1][i] <= tl) tr = max(tr, r[1][i]); } tl = tr + 1; cnt1++; } cout << 1 << '\n' << cnt1; } return 0; }
|