結果
問題 | No.348 カゴメカゴメ |
ユーザー | tnakao0123 |
提出日時 | 2016-04-07 21:02:56 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 61 ms / 2,000 ms |
コード長 | 3,457 bytes |
コンパイル時間 | 953 ms |
コンパイル使用メモリ | 95,312 KB |
実行使用メモリ | 21,948 KB |
最終ジャッジ日時 | 2024-10-04 02:31:42 |
合計ジャッジ時間 | 4,067 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
20,824 KB |
testcase_01 | AC | 6 ms
18,348 KB |
testcase_02 | AC | 61 ms
21,948 KB |
testcase_03 | AC | 49 ms
21,460 KB |
testcase_04 | AC | 6 ms
18,096 KB |
testcase_05 | AC | 5 ms
18,416 KB |
testcase_06 | AC | 5 ms
17,900 KB |
testcase_07 | AC | 6 ms
18,924 KB |
testcase_08 | AC | 6 ms
18,412 KB |
testcase_09 | AC | 6 ms
18,804 KB |
testcase_10 | AC | 6 ms
19,052 KB |
testcase_11 | AC | 6 ms
18,928 KB |
testcase_12 | AC | 8 ms
18,576 KB |
testcase_13 | AC | 5 ms
18,064 KB |
testcase_14 | AC | 6 ms
18,548 KB |
testcase_15 | AC | 46 ms
20,268 KB |
testcase_16 | AC | 6 ms
19,056 KB |
testcase_17 | AC | 6 ms
18,284 KB |
testcase_18 | AC | 4 ms
18,060 KB |
testcase_19 | AC | 6 ms
19,564 KB |
testcase_20 | AC | 4 ms
18,540 KB |
testcase_21 | AC | 5 ms
18,284 KB |
testcase_22 | AC | 5 ms
18,664 KB |
testcase_23 | AC | 54 ms
20,308 KB |
testcase_24 | AC | 55 ms
21,712 KB |
testcase_25 | AC | 53 ms
20,212 KB |
testcase_26 | AC | 57 ms
20,384 KB |
testcase_27 | AC | 55 ms
20,568 KB |
testcase_28 | AC | 57 ms
20,952 KB |
testcase_29 | AC | 55 ms
21,204 KB |
testcase_30 | AC | 56 ms
21,076 KB |
testcase_31 | AC | 54 ms
20,212 KB |
testcase_32 | AC | 56 ms
20,436 KB |
testcase_33 | AC | 10 ms
19,540 KB |
testcase_34 | AC | 10 ms
18,304 KB |
testcase_35 | AC | 10 ms
20,040 KB |
testcase_36 | AC | 9 ms
17,996 KB |
testcase_37 | AC | 10 ms
18,896 KB |
testcase_38 | AC | 9 ms
18,896 KB |
testcase_39 | AC | 9 ms
18,188 KB |
testcase_40 | AC | 10 ms
19,284 KB |
testcase_41 | AC | 9 ms
18,216 KB |
testcase_42 | AC | 9 ms
18,360 KB |
testcase_43 | AC | 6 ms
18,072 KB |
testcase_44 | AC | 6 ms
18,296 KB |
testcase_45 | AC | 6 ms
18,324 KB |
testcase_46 | AC | 6 ms
18,816 KB |
testcase_47 | AC | 6 ms
18,936 KB |
testcase_48 | AC | 6 ms
19,064 KB |
testcase_49 | AC | 6 ms
18,188 KB |
testcase_50 | AC | 6 ms
19,584 KB |
testcase_51 | AC | 5 ms
18,244 KB |
testcase_52 | AC | 6 ms
19,580 KB |
ソースコード
/* -*- coding: utf-8 -*- * * 348.cc: No.348 カゴメカゴメ - yukicoder */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_N = 1000; const int MAX_M = 1000; const int MAX_T = MAX_N * MAX_M / 3 + 1; const int dxs[] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dys[] = {0, -1, -1, -1, 0, 1, 1, 1}; const int INF = 1 << 30; /* typedef */ typedef vector<int> vi; typedef queue<int> qi; typedef pair<int,int> pii; /* global variables */ int n, m, nm; string lines[MAX_N]; int used[MAX_N][MAX_M]; int prts[MAX_T], wis[MAX_T]; vi chlds[MAX_T]; int dp[MAX_T][2]; /* subroutines */ void print_flds() { for (int y = 0; y < n; y++) { for (int x = 0; x < m; x++) printf("%2d ", used[y][x]); putchar('\n'); } } int adj_ids(int x, int y, int ddi = 1) { int maxid = -1; for (int di = 0; di < 8; di += ddi) { int vx = x + dxs[di], vy = y + dys[di]; int id = (vx >= 0 && vx < m && vy >= 0 && vy < n) ? used[vy][vx] : 0; if (maxid < id) maxid = id; } return maxid; } inline int adj4_ids(int x, int y) { return adj_ids(x, y, 2); } int paint(int x0, int y0, int id, char ch, int ddi = 1) { if (lines[y0][x0] != ch || used[y0][x0] >= 0) return 0; int pn = 1; used[y0][x0] = id; queue<pii> q; q.push(pii(x0, y0)); while (! q.empty()) { pii u = q.front(); q.pop(); int &ux = u.first, &uy = u.second; for (int di = 0; di < 8; di += ddi) { int vx = ux + dxs[di], vy = uy + dys[di]; if (vx >= 0 && vx < m && vy >= 0 && vy < n && lines[vy][vx] == ch && used[vy][vx] < 0) { pn++; used[vy][vx] = id; q.push(pii(vx, vy)); } } } return pn; } inline int paint4(int x0, int y0, int id, char ch) { return paint(x0, y0, id, ch, 2); } void rec(int u) { dp[u][0] = 0; dp[u][1] = wis[u]; vi &chldu = chlds[u]; for (vi::iterator vit = chldu.begin(); vit != chldu.end(); vit++) { int &v = *vit; rec(v); dp[u][0] += max(dp[v][0], dp[v][1]); dp[u][1] += dp[v][0]; } } /* main */ int main() { cin >> n >> m; for (int y = 0; y < n; y++) cin >> lines[y]; nm = n * m; memset(used, -1, sizeof(used)); prts[0] = -1; int pn = 0; for (int y = 0; y < n; y++) pn += paint4(0, y, 0, '.'), pn += paint4(m - 1, y, 0, '.'); for (int x = 0; x < m; x++) pn += paint4(x, 0, 0, '.'), pn += paint4(x, n - 1, 0, '.'); //printf("pn=%d\n", pn); print_flds(); int tn = 1; while (pn < nm) { for (int y = 0; y < n; y++) for (int x = 0; x < m; x++) if (used[y][x] < 0) { if (lines[y][x] == 'x') { int aid = adj_ids(x, y); if (aid >= 0) { wis[tn] = paint(x, y, tn, 'x'); prts[tn] = aid; chlds[aid].push_back(tn); pn += wis[tn]; tn++; } } else { int aid = adj4_ids(x, y); if (aid >= 0) pn += paint4(x, y, aid, '.'); } } //printf("pn=%d\n", pn); print_flds(); } //for (int i = 0; i < tn; i++) printf("%d ", prts[i]); putchar('\n'); //for (int i = 0; i < tn; i++) printf("%d ", wis[i]); putchar('\n'); //for (int i = 0; i < tn; i++) printf("%lu ", chlds[i].size()); putchar('\n'); rec(0); printf("%d\n", dp[0][0]); return 0; }