結果
| 問題 |
No.348 カゴメカゴメ
|
| コンテスト | |
| ユーザー |
togari_takamoto
|
| 提出日時 | 2016-02-27 00:01:59 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,073 bytes |
| コンパイル時間 | 1,733 ms |
| コンパイル使用メモリ | 175,324 KB |
| 実行使用メモリ | 128,640 KB |
| 最終ジャッジ日時 | 2024-09-24 10:35:55 |
| 合計ジャッジ時間 | 4,722 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 34 WA * 19 |
コンパイルメッセージ
main.cpp: In function ‘ll ring_dfs(const vvb&, ll, ll, ll, ll, vvb&)’:
main.cpp:58:1: warning: control reaches end of non-void function [-Wreturn-type]
58 | }
| ^
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>; using vb = vector<bool>; using vd = vector<double>; using vl = vector<ll>;
using vvi = vector<vi>; using vvb = vector<vb>; using vvd = vector<vd>; using vvl = vector<vl>;
#define REP(i,n) for(ll i=0; i<(n); ++i)
#define FOR(i,b,n) for(ll i=(b); i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define TEN(x) ((ll)1e##x)
struct Node{
ll size = 0;
vector<Node> children;
};
int dx[] = { -1, 0, 0, 1,-1, 1, -1, 1 };
int dy[] = { 0, -1, 1, 0, 1, 1, -1,-1 };
bool contain(ll n, ll x) { return 0 <= x && x < n; }
void blank_dfs(const vvb & table, ll x, ll y, vvb & visited, vector<pair<ll, ll>> & discover) {
ll n = table.size(); ll m = table[0].size();
if (visited[y][x]) return;
visited[y][x] = true;
REP(i, 4) {
ll nx = x + dx[i]; ll ny = y + dy[i];
if (!contain(m, nx) || !contain(n, ny)) continue;
if (visited[ny][nx]) continue;
if (table[ny][nx]) discover.push_back({ nx, ny });
else blank_dfs(table, nx, ny, visited, discover);
}
}
Node ring_search(const vvb & table, ll x, ll y, vvb & visited);
void blank_search(const vvb & table, ll x, ll y, vvb & visited, vector<Node> & nodes) {
vector<pair<ll, ll>> discover;
blank_dfs(table, x, y, visited, discover);
for (auto pos : discover) {
if (visited[pos.second][pos.first]) continue;
nodes.emplace_back(ring_search(table, pos.first, pos.second, visited));
}
}
ll ring_dfs(const vvb & table,ll x, ll y, ll px, ll py, vvb & visited) {
ll n = table.size(); ll m = table[0].size();
if (visited[y][x]) return 0;
visited[y][x] = true;
REP(i, 8) {
ll nx = x + dx[i]; ll ny = y + dy[i];
if (!contain(m, nx) || !contain(n, ny)) continue;
if (nx == px && ny == py) continue;
if (table[ny][nx]) return 1 + ring_dfs(table, nx, ny, x, y, visited);
}
}
Node ring_search(const vvb & table, ll x, ll y, vvb & visited) {
ll n = table.size(); ll m = table[0].size();
Node node;
node.size = ring_dfs(table, x, y, -1, -1, visited);
REP(i, 8) {
ll nx = x + dx[i]; ll ny = y + dy[i];
if (!contain(m, nx) || !contain(n, ny)) continue;
if (!visited[ny][nx] && !table[ny][nx]) {
blank_search(table, nx, ny, visited, node.children);
return node;
}
}
return node;
}
pair<ll,ll> dfs(Node & node) {
auto ans = make_pair(node.size, 0);
for (auto&& child : node.children) {
auto _ = dfs(child);
ans.first += _.second;
ans.second += _.first;
}
return ans;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(30);
ll n, m;
cin >> n >> m;
vvb table(n, vb(m));
REP(i, n) REP(j, m) {
char c; cin >> c;
table[i][j] = c!='.';
}
vvb visited(n, vb(m, false));
vector<Node> nodes;
REP(y, n) REP(x, m){
if (visited[y][x]) continue;
if (table[y][x]) {
nodes.emplace_back(ring_search(table, x, y, visited));
} else {
blank_search(table, x, y, visited, nodes);
}
}
auto ans = 0;
for (auto&& child : nodes) {
auto _ = dfs(child);
ans += max(_.first, _.second);
}
cout << ans << endl;
return 0;
}
togari_takamoto