結果

問題 No.348 カゴメカゴメ
ユーザー HachimoriHachimori
提出日時 2016-03-05 17:25:38
言語 C++11
(gcc 11.4.0)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,643 bytes
コンパイル時間 1,123 ms
コンパイル使用メモリ 80,140 KB
実行使用メモリ 813,148 KB
最終ジャッジ日時 2023-10-24 19:55:11
合計ジャッジ時間 4,062 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
9,764 KB
testcase_01 AC 3 ms
8,976 KB
testcase_02 MLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
権限があれば一括ダウンロードができます

ソースコード

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, int ringId2cnt[BUF]) {
    
    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);
            }
    
    static int ringId2cnt[BUF];
    memset(ringId2cnt, 0, sizeof(ringId2cnt));
    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(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