diff --git a/NFLSOJ/13367/13367.cpp b/NFLSOJ/13367/13367.cpp new file mode 100644 index 00000000..d68068b7 --- /dev/null +++ b/NFLSOJ/13367/13367.cpp @@ -0,0 +1,125 @@ +#include +#include +#include + +// using std::cin; +// using std::cout; +std::ifstream cin("field.in"); +std::ofstream cout("field.out"); +const char endl = '\n'; + +const int N = 505, + M = (N << 1) * (N << 1); + +int h, w, fa[M], x1[M], y1[M], x2[M], y2[M], ans; +std::string g[N << 1]; +bool flag; + +int id(int x, int y) { + return (x - 1) * w + y; +} + +int find(int x) { + return fa[x] == x ? x : fa[x] = find(fa[x]); +} + +void merge(int x, int y) { + x = find(x), y = find(y); + + if (x == y) return; + + flag = true; + fa[y] = x; + + x1[x] = std::min(x1[x], x1[y]); + y1[x] = std::min(y1[x], y1[y]); + + x2[x] = std::max(x2[x], x2[y]); + y2[x] = std::max(y2[x], y2[y]); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> h >> w; + + for (int i = 1; i <= 2 * h + 1; i++) { + cin >> g[i]; + + g[i] = ' ' + g[i]; + } + + for (int i = 1; i <= h; i++) { + for (int j = 1; j <= w; j++) { + int p = id(i, j); + fa[p] = p; + x1[p] = x2[p] = i; + y1[p] = y2[p] = j; + } + } + + for (int i = 1; i <= h; i++) { + for (int j = 1; j < w; j++) { + if (g[i * 2][j * 2 + 1] == '.') { + merge(id(i, j), id(i, j + 1)); + } + } + } + + for (int j = 1; j <= w; j++) { + for (int i = 1; i < h; i++) { + if (g[i * 2 + 1][j * 2] == '.') { + merge(id(i, j), id(i + 1, j)); + } + } + } + + while (true) { + flag = false; + + for (int i = 1; i <= h; i++) { + for (int j = 1; j <= w; j++) { + if (x2[find(id(i, j))] > i) { + merge(id(i, j), id(i + 1, j)); + } + } + } + + for (int i = h; i >= 1; i--) { + for (int j = 1; j <= w; j++) { + if (x1[find(id(i, j))] < i) { + merge(id(i, j), id(i - 1, j)); + } + } + } + + for (int j = 1; j <= w; j++) { + for (int i = 1; i <= h; i++) { + if (y2[find(id(i, j))] > j) { + merge(id(i, j), id(i, j + 1)); + } + } + } + + for (int j = w; j >= 1; j--) { + for (int i = 1; i <= h; i++) { + if (y1[find(id(i, j))] < j) { + merge(id(i, j), id(i, j - 1)); + } + } + } + + if (!flag) break; + } + + for (int i = 1; i <= h; i++) { + for (int j = 1; j <= w; j++) { + if (find(id(i, j)) == id(i, j)) ans++; + } + } + + cout << ans << endl; + + return 0; +} diff --git a/NFLSOJ/13367/field.pdf b/NFLSOJ/13367/field.pdf new file mode 100644 index 00000000..446d0533 --- /dev/null +++ b/NFLSOJ/13367/field.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:650f6aff7c8f054f1fd3735adb12c387fb56944a120777a10df7a3f45ef1a7d4 +size 168623 diff --git a/NFLSOJ/13367/samples/ex_field1.ans b/NFLSOJ/13367/samples/ex_field1.ans new file mode 100644 index 00000000..d76a596d --- /dev/null +++ b/NFLSOJ/13367/samples/ex_field1.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f0b5c2c2211c8d67ed15e75e656c7862d086e9245420892a7de62cd9ec582a06 +size 2 diff --git a/NFLSOJ/13367/samples/ex_field1.in b/NFLSOJ/13367/samples/ex_field1.in new file mode 100644 index 00000000..8f23c07f --- /dev/null +++ b/NFLSOJ/13367/samples/ex_field1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8659c1a3bb6c6e18fb532a7dfab900edce054b4adce4069906af5b2976f6d62a +size 94 diff --git a/NFLSOJ/13367/samples/ex_field2.ans b/NFLSOJ/13367/samples/ex_field2.ans new file mode 100644 index 00000000..2c7fd4e4 --- /dev/null +++ b/NFLSOJ/13367/samples/ex_field2.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8cd2510271575d8430c05368315a87b9c4784c7389a47496080c1e615a2a00b6 +size 3 diff --git a/NFLSOJ/13367/samples/ex_field2.in b/NFLSOJ/13367/samples/ex_field2.in new file mode 100644 index 00000000..36053ebd --- /dev/null +++ b/NFLSOJ/13367/samples/ex_field2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:160bf0ee8670e62242fe913cb49ecd45b77f1a18119a26325cce0ac07c33bcc4 +size 2144 diff --git a/NFLSOJ/13367/samples/ex_field3.ans b/NFLSOJ/13367/samples/ex_field3.ans new file mode 100644 index 00000000..fca644df --- /dev/null +++ b/NFLSOJ/13367/samples/ex_field3.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9537f32ec7599e1ae953af6c9f929fe747ff9dadf79a9beff1f304c550173011 +size 3 diff --git a/NFLSOJ/13367/samples/ex_field3.in b/NFLSOJ/13367/samples/ex_field3.in new file mode 100644 index 00000000..2423bd9d --- /dev/null +++ b/NFLSOJ/13367/samples/ex_field3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d0de09edb7fbc84f2d5684074869c275b946f1154947caa671c9c9af48e4ed1a +size 2336