0w1

Yuki 421 しろくろチョコレート ( Greedy, Maximum Bipartite Matching )

No.421 しろくろチョコレート - yukicoder

題意:
有一個 N * M 的黑白交接的棋盤巧克力。有些已經被吃掉了,用 '.' 表示。你每吃一片 1 * 2 的巧克力會得到幸福值 100,每吃不相鄰的一塊黑色和一塊白色會得到幸福值 10,隨便吃一塊會得到幸福值 1,問最大幸福值總和。

制約:
N, M ≤ 50

解法:
顯然只要最大化 1 * 2 的數量,答案就出現了。
對非障礙物的相鄰元素連邊,進行二分圖最大匹配即可。

時間 / 空間複雜度:
???
If someone could tell me...

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 ] != 'w': 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 ):
  if G[ i // M ][ i % M ] != 'w': continue
  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 ) )