結果

問題 No.348 カゴメカゴメ
ユーザー HachimoriHachimori
提出日時 2016-03-05 17:44:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 3,372 bytes
コンパイル時間 813 ms
コンパイル使用メモリ 77,736 KB
実行使用メモリ 150,432 KB
最終ジャッジ日時 2024-09-24 14:23:15
合計ジャッジ時間 4,606 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
44,960 KB
testcase_01 AC 4 ms
8,368 KB
testcase_02 AC 221 ms
84,308 KB
testcase_03 AC 93 ms
9,960 KB
testcase_04 AC 3 ms
8,648 KB
testcase_05 AC 4 ms
9,656 KB
testcase_06 AC 3 ms
8,528 KB
testcase_07 AC 3 ms
8,416 KB
testcase_08 AC 3 ms
8,396 KB
testcase_09 AC 4 ms
8,916 KB
testcase_10 AC 4 ms
8,656 KB
testcase_11 AC 3 ms
8,144 KB
testcase_12 AC 7 ms
11,096 KB
testcase_13 AC 5 ms
8,840 KB
testcase_14 AC 3 ms
8,304 KB
testcase_15 AC 193 ms
150,432 KB
testcase_16 AC 3 ms
8,392 KB
testcase_17 AC 3 ms
8,404 KB
testcase_18 AC 3 ms
8,392 KB
testcase_19 AC 3 ms
8,396 KB
testcase_20 AC 3 ms
8,284 KB
testcase_21 AC 3 ms
8,288 KB
testcase_22 AC 3 ms
8,276 KB
testcase_23 AC 138 ms
38,564 KB
testcase_24 AC 134 ms
35,620 KB
testcase_25 AC 137 ms
39,568 KB
testcase_26 AC 135 ms
39,012 KB
testcase_27 AC 132 ms
36,044 KB
testcase_28 AC 131 ms
35,636 KB
testcase_29 AC 138 ms
38,944 KB
testcase_30 AC 134 ms
37,540 KB
testcase_31 AC 131 ms
33,920 KB
testcase_32 AC 135 ms
38,048 KB
testcase_33 AC 15 ms
12,048 KB
testcase_34 AC 14 ms
11,408 KB
testcase_35 AC 14 ms
11,660 KB
testcase_36 AC 14 ms
11,148 KB
testcase_37 AC 14 ms
11,788 KB
testcase_38 AC 15 ms
11,660 KB
testcase_39 AC 14 ms
11,664 KB
testcase_40 AC 14 ms
11,664 KB
testcase_41 AC 14 ms
11,408 KB
testcase_42 AC 14 ms
11,536 KB
testcase_43 AC 5 ms
8,872 KB
testcase_44 AC 4 ms
8,748 KB
testcase_45 AC 5 ms
8,748 KB
testcase_46 AC 5 ms
8,748 KB
testcase_47 AC 5 ms
8,752 KB
testcase_48 AC 5 ms
9,000 KB
testcase_49 AC 5 ms
9,128 KB
testcase_50 AC 5 ms
8,876 KB
testcase_51 AC 5 ms
8,620 KB
testcase_52 AC 4 ms
8,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#define r first
#define c second
using namespace std;
typedef pair<int, int> Point;
const int BUF = 1005;



int row, col;
char b[BUF][BUF];

void read() {
    cin >> row >> col;

    for (int i = 0; i < row + 2; ++i) {
        for (int j = 0; j < col + 2; ++j) {
            b[i][j] = '.';
        }
    }
    
    for (int i = 1; i <= row; ++i) {
        for (int j = 1; j <= col; ++j) {
            cin >> b[i][j];
        }
    }
    
    row += 2;
    col += 2;
}


bool isInside(int r, int c) {
    return 0 <= r && r < row && 0 <= c && c < col;
}


void traceRing(int r, int c, int curId, int ringId[BUF][BUF]) {
    
    static int dr[] = {-1, -1, -1, 0, 1, 1,  1,  0};
    static int dc[] = {-1,  0,  1, 1, 1, 0, -1, -1};
    
    ringId[r][c] = curId;
    
    for (int i = 0; i < 8; ++i) {
        int nr = r + dr[i];
        int nc = c + dc[i];
        if (isInside(nr, nc) && b[nr][nc] == 'x' && ringId[nr][nc] == -1) {
            traceRing(nr, nc, curId, ringId);
        }
    }
}


void colorArea(int r, int c, int currId, bool visited[BUF][BUF],
               vector< set<int> > &adjRing, int ringId[BUF][BUF]) {
    visited[r][c] = true;

    static int dr[] = {-1, 0, 1, 0};
    static int dc[] = {0, 1, 0, -1};

    for (int i = 0; i < 4; ++i) {
        int nr = r + dr[i];
        int nc = c + dc[i];
        if (!isInside(nr, nc)) continue;
        if (b[nr][nc] == 'x') {
            if (currId != ringId[nr][nc])
                adjRing[currId].insert(ringId[nr][nc]);
        }
        else if (!visited[nr][nc]) {
            colorArea(nr, nc, currId, visited, adjRing, ringId);
        }
    }
}


int doDp(bool preUsed, int curr, vector< vector<int> > &dp,
         vector< set<int> > &adjRing, vector<int> &ringId2cnt) {
    
    int &ret = dp[preUsed][curr];
    if (ret != -1) return ret;
    
    int A = 0; // use current
    int B = 0; // do not use current
    for (set<int>::iterator it = adjRing[curr].begin(); it != adjRing[curr].end(); ++it) {
        A += doDp(true, *it, dp, adjRing, ringId2cnt);
        B += doDp(false, *it, dp, adjRing, ringId2cnt);
    }
    
    return ret = (preUsed ? B : max(A + ringId2cnt[curr], B));
}


void work() {
    int nRing = 1;
    static int ringId[BUF][BUF];
    memset(ringId, -1, sizeof(ringId));

    for (int i = 0; i < row; ++i)
        for (int j = 0; j < col; ++j)
            if (b[i][j] == 'x' && ringId[i][j] == -1) {
                traceRing(i, j, nRing++, ringId);
            }
    
    vector<int> ringId2cnt(nRing, 0);
    for (int i = 0; i < row; ++i)
        for (int j = 0; j < col; ++j)
            if (ringId[i][j] != -1) {
                ++ringId2cnt[ringId[i][j]];
            }

    vector< set<int> > adjRing;
    adjRing.assign(nRing, set<int>());
    
    static bool visited[BUF][BUF];
    memset(visited, 0, sizeof(visited));
    
    colorArea(0, 0, 0, visited, adjRing, ringId);
    for (int i = 0; i < row; ++i)
        for (int j = 0; j < col; ++j)
            if (!visited[i][j] && ringId[i][j] != -1) {
                colorArea(i, j, ringId[i][j], visited, adjRing, ringId);
            }

    
    vector< vector<int> > dp(2, vector<int>(nRing, -1));
    cout << doDp(false, 0, dp, adjRing, ringId2cnt) << endl;
}


int main() {
    read();
    work();
    return 0;
}
0