| ps | |
|---|---|
| 링크 | acmicpc.net/… | 
| 출처 | BOJ | 
| 문제 번호 | 16724 | 
| 문제명 | 피리 부는 사나이 | 
| 레벨 | 골드 2 | 
| 분류 | 
 Disjoint set  | 
	
| 시간복잡도 | O(nm) | 
| 인풋사이즈 | n<=1000, m<=1000 | 
| 사용한 언어 | Python | 
| 제출기록 | 53140KB / 1308ms | 
| 최고기록 | 680ms | 
| 해결날짜 | 2021/11/18 | 
"""Solution code for "BOJ 16724. 피리 부는 사나이".
- Problem link: https://www.acmicpc.net/problem/16724
- Solution link: http://www.teferi.net/ps/problems/boj/16724
Tags: [Disjoint Set]
"""
import itertools
import sys
from teflib import disjointset
def main():
    N, M = [int(x) for x in sys.stdin.readline().split()]
    grid = [sys.stdin.readline().rstrip() for _ in range(N)]
    delta = {'U': -M, 'D': M, 'L': -1, 'R': 1}
    group_count = N * M
    dsu = disjointset.DisjointSet(group_count)
    counter = itertools.count()
    for row in grid:
        for direction in row:
            cur_cell = next(counter)
            adj_cell = cur_cell + delta[direction]
            try:
                dsu.union(cur_cell, adj_cell, should_raise=True)
            except ValueError:
                pass
            else:
                group_count -= 1
    print(group_count)
if __name__ == '__main__':
    main()