結果

問題 No.421 しろくろチョコレート
ユーザー 0w1
提出日時 2017-04-27 17:09:26
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,117 bytes
コンパイル時間 648 ms
コンパイル使用メモリ 82,824 KB
実行使用メモリ 77,416 KB
最終ジャッジ日時 2024-09-13 14:49:40
合計ジャッジ時間 6,477 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30 WA * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import product

dx = [ 0, 1, 0, -1 ]
dy = [ 1, 0, -1, 0 ]

N, M = map( int, input().split() )
G = [ input() for i in range( N ) ]

adj = [ [] for i in range( N * M ) ]

for i in range( N ):
  for j in range( M ):
    if G[ i ][ j ] in "b.": continue
    for di in range( 4 ):
      ni, nj = i + dx[ di ], j + dy[ di ]
      if not ( 0 <= ni and ni < N and 0 <= nj and nj < M ): continue
      if G[ ni ][ nj ] == '.': continue
      adj[ i * M + j ].append( ni * M + nj )

def find_mate( u, bf, vis = None ):
  if vis is None:
    vis = [ False for i in range( N * M ) ]
  for v in adj[ u ]:
    if vis[ v ]: continue
    vis[ v ] = True
    if bf[ v ] == -1 or find_mate( bf[ v ], bf, vis ):
      bf[ v ] = u
      return True
  return False

bf = [ -1 for i in range( N * M ) ]
match_cnt = 0
for i in range( 0, N * M, 2 ):
  match_cnt += find_mate( i, bf )

rw = sum( G[ i ][ j ] == 'w' for i, j in product( range( N ), range( M ) ) ) - match_cnt
rb = sum( G[ i ][ j ] == 'b' for i, j in product( range( N ), range( M ) ) ) - match_cnt
print( match_cnt * 100 + min( rw, rb ) * 10 + abs( rw - rb ) )
0