結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
9,388 KB
testcase_01 AC 4 ms
8,256 KB
testcase_02 AC 200 ms
21,100 KB
testcase_03 AC 118 ms
9,632 KB
testcase_04 AC 4 ms
8,648 KB
testcase_05 AC 4 ms
9,248 KB
testcase_06 AC 3 ms
8,396 KB
testcase_07 AC 3 ms
8,776 KB
testcase_08 AC 3 ms
8,396 KB
testcase_09 AC 3 ms
8,404 KB
testcase_10 AC 4 ms
8,916 KB
testcase_11 AC 4 ms
8,908 KB
testcase_12 AC 6 ms
8,780 KB
testcase_13 AC 5 ms
8,456 KB
testcase_14 AC 4 ms
8,308 KB
testcase_15 AC 78 ms
9,248 KB
testcase_16 AC 4 ms
8,520 KB
testcase_17 AC 3 ms
8,404 KB
testcase_18 AC 4 ms
8,264 KB
testcase_19 AC 3 ms
8,396 KB
testcase_20 AC 3 ms
8,408 KB
testcase_21 AC 3 ms
8,284 KB
testcase_22 AC 3 ms
8,268 KB
testcase_23 AC 119 ms
10,244 KB
testcase_24 AC 121 ms
10,144 KB
testcase_25 AC 121 ms
10,268 KB
testcase_26 AC 121 ms
10,208 KB
testcase_27 AC 120 ms
10,144 KB
testcase_28 AC 120 ms
10,140 KB
testcase_29 AC 120 ms
10,124 KB
testcase_30 AC 124 ms
10,140 KB
testcase_31 AC 120 ms
10,132 KB
testcase_32 AC 120 ms
10,176 KB
testcase_33 AC 14 ms
8,724 KB
testcase_34 AC 12 ms
8,592 KB
testcase_35 AC 13 ms
8,724 KB
testcase_36 AC 12 ms
8,584 KB
testcase_37 AC 13 ms
8,716 KB
testcase_38 AC 12 ms
9,228 KB
testcase_39 AC 12 ms
8,596 KB
testcase_40 AC 13 ms
8,592 KB
testcase_41 AC 14 ms
9,356 KB
testcase_42 AC 13 ms
8,724 KB
testcase_43 AC 5 ms
8,492 KB
testcase_44 AC 5 ms
8,748 KB
testcase_45 AC 5 ms
8,740 KB
testcase_46 AC 5 ms
8,492 KB
testcase_47 AC 5 ms
8,496 KB
testcase_48 AC 5 ms
8,616 KB
testcase_49 AC 4 ms
8,492 KB
testcase_50 AC 4 ms
8,564 KB
testcase_51 AC 5 ms
8,616 KB
testcase_52 AC 4 ms
8,496 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 initR, int initC, int currId, bool visited[BUF][BUF],
               vector< set<int> > &adjRing, int ringId[BUF][BUF]) {

    queue<Point> Q;
    
    Q.push(Point(initR, initC));
    visited[initR][initC] = true;

    while (!Q.empty()) {
        Point curr = Q.front();
        Q.pop();
        
        static int dr[] = {-1, 0, 1, 0};
        static int dc[] = {0, 1, 0, -1};
        
        for (int i = 0; i < 4; ++i) {
            int nr = curr.r + dr[i];
            int nc = curr.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]) {
                Q.push(Point(nr, nc));
                visited[nr][nc] = true;
            }
        }
    }
}


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