結果

問題 No.348 カゴメカゴメ
ユーザー kimiyukikimiyuki
提出日時 2016-02-26 23:45:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 2,618 bytes
コンパイル時間 1,395 ms
コンパイル使用メモリ 93,400 KB
実行使用メモリ 117,336 KB
最終ジャッジ日時 2023-10-24 10:44:19
合計ジャッジ時間 6,283 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 147 ms
23,392 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 259 ms
63,620 KB
testcase_03 AC 148 ms
13,260 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 6 ms
5,212 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 178 ms
117,336 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 156 ms
20,060 KB
testcase_24 AC 156 ms
20,484 KB
testcase_25 AC 157 ms
21,892 KB
testcase_26 AC 156 ms
20,280 KB
testcase_27 AC 156 ms
19,868 KB
testcase_28 AC 156 ms
21,100 KB
testcase_29 AC 158 ms
19,968 KB
testcase_30 AC 157 ms
20,020 KB
testcase_31 AC 156 ms
20,820 KB
testcase_32 AC 158 ms
22,160 KB
testcase_33 AC 14 ms
5,596 KB
testcase_34 AC 14 ms
5,236 KB
testcase_35 AC 14 ms
5,388 KB
testcase_36 AC 14 ms
5,416 KB
testcase_37 AC 14 ms
5,584 KB
testcase_38 AC 14 ms
5,524 KB
testcase_39 AC 14 ms
5,120 KB
testcase_40 AC 15 ms
5,664 KB
testcase_41 AC 15 ms
5,456 KB
testcase_42 AC 14 ms
5,616 KB
testcase_43 AC 4 ms
4,348 KB
testcase_44 AC 3 ms
4,348 KB
testcase_45 AC 3 ms
4,348 KB
testcase_46 AC 3 ms
4,348 KB
testcase_47 AC 3 ms
4,348 KB
testcase_48 AC 3 ms
4,348 KB
testcase_49 AC 4 ms
4,348 KB
testcase_50 AC 3 ms
4,348 KB
testcase_51 AC 3 ms
4,348 KB
testcase_52 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
template <typename T> bool setmax(T & l, T const & r) { if (not (l < r)) return false; l = r; return true; }
using namespace std;
struct point_t { int y, x; };
const int dy[] = { -1, 1, 0, 0,  -1, 1, -1, 1 };
const int dx[] = { 0, 0, -1, 1,  -1, -1, 1, 1 };
int main() {
    int h, w; cin >> h >> w;
    vector<vector<bool> > is_x(h, vector<bool>(w));
    repeat (y,h) repeat (x,w) {
        char c; cin >> c;
        is_x[y][x] = c == 'x';
    }
    auto is_on_field = [&](int y, int x) {
        return 0 <= y and y < h and 0 <= x and x < w;
    };
    vector<vector<point_t> > rings;
    vector<vector<int> > rev(h, vector<int>(w, -1));
    function<void (int, int)> make_ring = [&](int y, int x) {
        if (not is_on_field(y, x)) return;
        if (not is_x[y][x]) return;
        if (rev[y][x] != -1) return;
        rings.back().push_back((point_t) { y, x });
        rev[y][x] = rings.size() - 1;
        repeat (i,8) make_ring(y + dy[i], x + dx[i]);
    };
    repeat (y,h) repeat (x,w) if (is_x[y][x] and rev[y][x] == -1) {
        rings.emplace_back();
        make_ring(y, x);
    }
    int n = rings.size();
    rings.emplace_back();
    vector<set<int> > is_in(n + 1);
    vector<vector<bool> > used(h, vector<bool>(w));
    function<void (int, int, int)> analyze_inclusion = [&](int y, int x, int r) {
        if (not is_on_field(y, x)) return;
        if (used[y][x]) return;
        if (rev[y][x] != -1 and rev[y][x] != r) {
            is_in[r].insert(rev[y][x]);
            return;
        }
        used[y][x] = true;
        repeat (i,4) analyze_inclusion(y + dy[i], x + dx[i], r);
    };
    repeat (y,h) {
        analyze_inclusion(y,   0, n);
        analyze_inclusion(y, w-1, n);
    }
    repeat (x,w) {
        analyze_inclusion(  0, x, n);
        analyze_inclusion(h-1, x, n);
    }
    repeat (y,h) repeat (x,w) if (rev[y][x] != -1 and not used[y][x]) {
        int r = rev[y][x];
        for (point_t p : rings[r]) {
            analyze_inclusion(p.y, p.x, r);
        }
    }
    vector<vector<int> > memo(2, vector<int>(n + 1));
    function<void (int)> calculate_score = [&](int r) {
        for (int s : is_in[r]) calculate_score(s);
        memo[1][r] += rings[r].size(); // use r
        for (int s : is_in[r]) memo[1][r] += memo[0][s];
        for (int s : is_in[r]) memo[0][r] += memo[1][s]; // don't use r
        setmax(memo[1][r], memo[0][r]);
    };
    calculate_score(n);
    cout << memo[1][n]<< endl;
    return 0;
}
0