結果
問題 | No.348 カゴメカゴメ |
ユーザー | Hachimori |
提出日時 | 2016-03-06 17:58:05 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 223 ms / 2,000 ms |
コード長 | 3,357 bytes |
コンパイル時間 | 919 ms |
コンパイル使用メモリ | 78,500 KB |
実行使用メモリ | 150,492 KB |
最終ジャッジ日時 | 2024-09-24 14:47:41 |
合計ジャッジ時間 | 4,774 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 119 ms
45,092 KB |
testcase_01 | AC | 4 ms
8,436 KB |
testcase_02 | AC | 223 ms
84,404 KB |
testcase_03 | AC | 90 ms
9,804 KB |
testcase_04 | AC | 4 ms
8,904 KB |
testcase_05 | AC | 4 ms
9,788 KB |
testcase_06 | AC | 4 ms
8,404 KB |
testcase_07 | AC | 4 ms
9,160 KB |
testcase_08 | AC | 3 ms
8,396 KB |
testcase_09 | AC | 3 ms
8,408 KB |
testcase_10 | AC | 3 ms
8,400 KB |
testcase_11 | AC | 3 ms
8,404 KB |
testcase_12 | AC | 7 ms
11,088 KB |
testcase_13 | AC | 4 ms
9,216 KB |
testcase_14 | AC | 4 ms
8,308 KB |
testcase_15 | AC | 194 ms
150,492 KB |
testcase_16 | AC | 4 ms
8,396 KB |
testcase_17 | AC | 3 ms
8,396 KB |
testcase_18 | AC | 3 ms
8,648 KB |
testcase_19 | AC | 3 ms
8,648 KB |
testcase_20 | AC | 4 ms
8,284 KB |
testcase_21 | AC | 3 ms
8,288 KB |
testcase_22 | AC | 3 ms
8,400 KB |
testcase_23 | AC | 139 ms
38,560 KB |
testcase_24 | AC | 130 ms
35,744 KB |
testcase_25 | AC | 137 ms
39,460 KB |
testcase_26 | AC | 137 ms
38,812 KB |
testcase_27 | AC | 132 ms
36,012 KB |
testcase_28 | AC | 132 ms
35,748 KB |
testcase_29 | AC | 134 ms
39,108 KB |
testcase_30 | AC | 134 ms
37,660 KB |
testcase_31 | AC | 130 ms
33,952 KB |
testcase_32 | AC | 134 ms
38,176 KB |
testcase_33 | AC | 14 ms
12,044 KB |
testcase_34 | AC | 14 ms
11,404 KB |
testcase_35 | AC | 14 ms
11,536 KB |
testcase_36 | AC | 14 ms
11,404 KB |
testcase_37 | AC | 15 ms
11,792 KB |
testcase_38 | AC | 14 ms
12,168 KB |
testcase_39 | AC | 15 ms
11,792 KB |
testcase_40 | AC | 15 ms
11,532 KB |
testcase_41 | AC | 14 ms
11,664 KB |
testcase_42 | AC | 14 ms
11,532 KB |
testcase_43 | AC | 5 ms
8,620 KB |
testcase_44 | AC | 5 ms
8,616 KB |
testcase_45 | AC | 4 ms
8,744 KB |
testcase_46 | AC | 4 ms
8,620 KB |
testcase_47 | AC | 5 ms
8,876 KB |
testcase_48 | AC | 4 ms
8,752 KB |
testcase_49 | AC | 4 ms
8,496 KB |
testcase_50 | AC | 5 ms
8,620 KB |
testcase_51 | AC | 5 ms
8,560 KB |
testcase_52 | AC | 5 ms
9,384 KB |
ソースコード
#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; } int traceRing(int r, int c, int curId, int ringId[BUF][BUF]) { int ringSize = 0; ringId[r][c] = curId; static int dr[] = {-1, -1, -1, 0, 1, 1, 1, 0}; static int dc[] = {-1, 0, 1, 1, 1, 0, -1, -1}; 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) { ringSize += traceRing(nr, nc, curId, ringId); } } return ringSize + 1; } 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> &ringId2size) { 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, ringId2size); B += doDp(false, *it, dp, adjRing, ringId2size); } return ret = (preUsed ? B : max(A + ringId2size[curr], B)); } void work() { int nRing = 1; static int ringId[BUF][BUF]; memset(ringId, -1, sizeof(ringId)); vector<int> ringId2size; ringId2size.push_back(0); for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) if (b[i][j] == 'x' && ringId[i][j] == -1) { int ringSize = traceRing(i, j, nRing++, ringId); ringId2size.push_back(ringSize); } 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, ringId2size) << endl; } int main() { read(); work(); return 0; }