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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <cstdio> #include <algorithm> using namespace std; const int N = 4e5 + 5; const int zl[5][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {0, 0}}; int n, m, A[N], f[N], tot, cc; inline int id(int x, int y) { return x * m + y; } inline int isBad(int x, int y) { if(A[id(x, y)] == 1) return 0; int v = A[id(x, y)]; if(x > 0 && A[id(x - 1, y)] < v) return 0; if(y > 0 && A[id(x, y - 1)] < v) return 0; if(x < n - 1 && A[id(x + 1, y)] < v) return 0; if(y < m - 1 && A[id(x, y + 1)] < v) return 0; return 1; } inline bool inSqu(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } bool vis[N]; inline bool judge(int x, int y, int p, int q) { int i1 = id(x, y), i2 = id(p, q), del = 0; int tx, ty, d, i3; swap(A[i1], A[i2]); for(int i = 0; i < 5; i++) { tx = x + zl[i][0], ty = y + zl[i][1]; if(inSqu(tx, ty)) { i3 = id(tx, ty); if(!vis[i3]) { vis[i3] = 1, d = isBad(tx, ty); if(d ^ f[i3]) { if(d > 0) del--; else del++; } } } tx = p + zl[i][0], ty = q + zl[i][1]; if(!inSqu(tx, ty)) continue; i3 = id(tx, ty); if(!vis[i3]) { vis[i3] = 1, d = isBad(tx, ty); if(d ^ f[i3]) { if(d > 0) del--; else del++; } } } for(int i = 0; i < 5; i++) { int tx = x + zl[i][0], ty = y + zl[i][1]; if(inSqu(tx, ty)) vis[id(tx, ty)] = 0; tx = p + zl[i][0], ty = q + zl[i][1]; if(!inSqu(tx, ty)) continue; vis[id(tx, ty)] = 0; } swap(A[i1], A[i2]); return del == tot; } struct Op { int a, b; Op(int s = 0, int t = 0): a(s), b(t) { if(a > b) swap(a, b); } bool operator < (const Op &B) const { if(a != B.a) return a < B.a; return b < B.b; } bool operator == (const Op &B) const { return a == B.a && b == B.b; } }L[N * 5]; int main() { scanf("%d%d", &n, &m); for(int i = 0; i < n * m; i++) scanf("%d", A + i); int bx = -1, by = -1; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(isBad(i, j)) { tot += (f[i * m + j] = 1); bx = i, by = j; } } } if(bx == -1) return puts("0"), 0; for(int i = 0; i < 5; i++) { int x = bx + zl[i][0], y = by + zl[i][1]; if(x < 0 || x >= n || y < 0 || y >= m) continue; for(int p = 0; p < n; p++) { for(int q = 0; q < m; q++) { if(judge(x, y, p, q)) { L[++cc] = Op(id(x, y), id(p, q)); } } } } if(cc == 0) return puts("2"), 0; sort(L + 1, L + cc + 1); cc = unique(L + 1, L + cc + 1) - L - 1; printf("1 %d\n", cc); return 0; }
|