結果

問題 No.421 しろくろチョコレート
ユーザー tnakao0123tnakao0123
提出日時 2016-09-19 02:27:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 2,346 bytes
コンパイル時間 780 ms
コンパイル使用メモリ 92,028 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:48:57
合計ジャッジ時間 2,195 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 5 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 3 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 1 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
testcase_45 AC 1 ms
4,348 KB
testcase_46 AC 1 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 4 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 3 ms
4,348 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 1 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 1 ms
4,348 KB
testcase_56 AC 1 ms
4,348 KB
testcase_57 AC 1 ms
4,348 KB
testcase_58 AC 1 ms
4,348 KB
testcase_59 AC 1 ms
4,348 KB
testcase_60 AC 4 ms
4,348 KB
testcase_61 AC 8 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 421.cc: No.421 しろくろチョコレート - 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_H = 50;
const int MAX_W = 50;
const int MAX_N = MAX_H * MAX_W;

/* typedef */

typedef vector<int> vi;
typedef vector<bool> vb;

/* global variables */

string flds[MAX_H];
int ids[MAX_H][MAX_W];
vi nbrs[MAX_N];

/* subroutines */

bool max_match_rec(const vi *nbrs, int u, vi &matches, vb &visited) {
  if (u < 0) return true;
  const vi &nbru = nbrs[u];
  for (vi::const_iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
    const int &v = *vit;
    if (! visited[v]) {
      visited[v] = true;
      if (max_match_rec(nbrs, matches[v], matches, visited)) {
        matches[u] = v;
        matches[v] = u;
        return true;
      }
    }
  }
  return false;
}

int max_match(const int n, int l, const vi *nbrs) {
  vi matches(n, -1);
  int count = 0;
  
  for (int u = 0; u < l; u++) {
    vb visited(n, false);
    if (max_match_rec(nbrs, u, matches, visited)) count++;
  }

  return count;
}

/* main */

int main() {
  int h, w;
  cin >> h >> w;

  memset(ids, -1, sizeof(ids));
  int bn = 0, wn = 0;

  for (int y = 0; y < h; y++) {
    cin >> flds[y];
    for (int x = 0; x < w; x++)
      switch (flds[y][x]) {
      case 'b': ids[y][x] = bn++; break;
      case 'w': ids[y][x] = wn++; break;
    }
  }

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      if (ids[y][x] >= 0) {
	bool bf = (flds[y][x] == 'b');
	int u = bf ? ids[y][x] : ids[y][x] + bn;
	if (x < w - 1 && ids[y][x + 1] >= 0) {
	  int v = bf ? ids[y][x + 1] + bn : ids[y][x + 1];
	  nbrs[u].push_back(v);
	  nbrs[v].push_back(u);
	}
	if (y < h - 1 && ids[y + 1][x] >= 0) {
	  int v = bf ? ids[y + 1][x] + bn : ids[y + 1][x];
	  nbrs[u].push_back(v);
	  nbrs[v].push_back(u);
	}
      }

  int mm = max_match(bn + wn, bn, nbrs);
  //printf("bn=%d, wn=%d, mm=%d\n", bn, wn, mm);

  bn -= mm, wn -= mm;
  int sc = mm * 100 + min(bn, wn) * 10 + abs(bn - wn);

  printf("%d\n", sc);
  return 0;
}
0