結果

問題 No.348 カゴメカゴメ
ユーザー togari_takamototogari_takamoto
提出日時 2016-02-26 23:59:40
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,386 bytes
コンパイル時間 1,666 ms
コンパイル使用メモリ 176,088 KB
実行使用メモリ 128,768 KB
最終ジャッジ日時 2024-09-24 10:35:20
合計ジャッジ時間 5,296 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 88 ms
12,800 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 WA -
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 6 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 181 ms
128,768 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 3 ms
6,944 KB
testcase_48 AC 3 ms
6,944 KB
testcase_49 WA -
testcase_50 AC 3 ms
6,944 KB
testcase_51 WA -
testcase_52 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void scan(T&) [with T = int]’:
main.cpp:8:47: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘int’ [-Wformat=]
    8 | template<> void scan<int>(int & n)  { scanf("%d", n); }
      |                                              ~^   ~
      |                                               |   |
      |                                               |   int
      |                                               int*
main.cpp: In function ‘void scan(T&) [with T = long long int]’:
main.cpp:9:47: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘ll’ {aka ‘long long int’} [-Wformat=]
    9 | template<> void scan<ll>(ll & n)  { scanf("%lld", n); }
      |                                            ~~~^   ~
      |                                               |   |
      |                                               |   ll {aka long long int}
      |                                               long long int*
main.cpp: In function ‘void scan(T&) [with T = double]’:
main.cpp:10:54: warning: format ‘%lf’ expects argument of type ‘double*’, but argument 2 has type ‘double’ [-Wformat=]
   10 | template<> void scan<double>(double & n)  { scanf("%lf", n); }
      |                                                    ~~^   ~
      |                                                      |   |
      |                                                      |   double
      |                                                      double*
main.cpp: In function ‘void scan(T&) [with T = int]’:
main.cpp:8:44: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    8 | template<> void scan<int>(int & n)  { scanf("%d", n); }
      |                                       ~~~~~^~~~~~~~~
main.cpp: In function ‘void scan(T&) [with T = long long int]’:
main.cpp:9:42

ソースコード

diff #

#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>;

template<class T> void scan(T &);
template<> void scan<int>(int & n)  { scanf("%d", n); }
template<> void scan<ll>(ll & n)  { scanf("%lld", n); }
template<> void scan<double>(double & n)  { scanf("%lf", n); }

#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)
#define EPS 1e-10
#define MOD (TEN(9) + 9)

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 = make_pair(0, 0);
	for (auto&& child : nodes) {
		auto _ = dfs(child);
		ans.first += _.second;
		ans.second += _.first;
	}

	cout << max(ans.first, ans.second) << endl;

	return 0;
}
0