結果

問題 No.348 カゴメカゴメ
ユーザー kimiyukikimiyuki
提出日時 2016-02-26 23:45:11
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 2,618 bytes
コンパイル時間 983 ms
コンパイル使用メモリ 93,392 KB
実行使用メモリ 117,120 KB
最終ジャッジ日時 2024-09-23 03:07:36
合計ジャッジ時間 5,263 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
template <typename T> bool setmax(T & l, T const & r) { if (not (l < r)) return false; l = r; return true; }
using namespace std;
struct point_t { int y, x; };
const int dy[] = { -1, 1, 0, 0, -1, 1, -1, 1 };
const int dx[] = { 0, 0, -1, 1, -1, -1, 1, 1 };
int main() {
int h, w; cin >> h >> w;
vector<vector<bool> > is_x(h, vector<bool>(w));
repeat (y,h) repeat (x,w) {
char c; cin >> c;
is_x[y][x] = c == 'x';
}
auto is_on_field = [&](int y, int x) {
return 0 <= y and y < h and 0 <= x and x < w;
};
vector<vector<point_t> > rings;
vector<vector<int> > rev(h, vector<int>(w, -1));
function<void (int, int)> make_ring = [&](int y, int x) {
if (not is_on_field(y, x)) return;
if (not is_x[y][x]) return;
if (rev[y][x] != -1) return;
rings.back().push_back((point_t) { y, x });
rev[y][x] = rings.size() - 1;
repeat (i,8) make_ring(y + dy[i], x + dx[i]);
};
repeat (y,h) repeat (x,w) if (is_x[y][x] and rev[y][x] == -1) {
rings.emplace_back();
make_ring(y, x);
}
int n = rings.size();
rings.emplace_back();
vector<set<int> > is_in(n + 1);
vector<vector<bool> > used(h, vector<bool>(w));
function<void (int, int, int)> analyze_inclusion = [&](int y, int x, int r) {
if (not is_on_field(y, x)) return;
if (used[y][x]) return;
if (rev[y][x] != -1 and rev[y][x] != r) {
is_in[r].insert(rev[y][x]);
return;
}
used[y][x] = true;
repeat (i,4) analyze_inclusion(y + dy[i], x + dx[i], r);
};
repeat (y,h) {
analyze_inclusion(y, 0, n);
analyze_inclusion(y, w-1, n);
}
repeat (x,w) {
analyze_inclusion( 0, x, n);
analyze_inclusion(h-1, x, n);
}
repeat (y,h) repeat (x,w) if (rev[y][x] != -1 and not used[y][x]) {
int r = rev[y][x];
for (point_t p : rings[r]) {
analyze_inclusion(p.y, p.x, r);
}
}
vector<vector<int> > memo(2, vector<int>(n + 1));
function<void (int)> calculate_score = [&](int r) {
for (int s : is_in[r]) calculate_score(s);
memo[1][r] += rings[r].size(); // use r
for (int s : is_in[r]) memo[1][r] += memo[0][s];
for (int s : is_in[r]) memo[0][r] += memo[1][s]; // don't use r
setmax(memo[1][r], memo[0][r]);
};
calculate_score(n);
cout << memo[1][n]<< endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0