python.graphs

By Vitaly Pavlenko, ago, written in Python.
URL http://pastecode.org/index.php/view/29602793
Download Paste or View RawExpand paste to full width of browser | Change Viewing Options
  1. from sys import setrecursionlimit
  2.  
  3. setrecursionlimit(100000)
  4.  
  5. def dfs_repaint(field, i, j):
  6.     field[i][j] = '.'
  7.     a = [-1, 0, 0, 1]
  8.     b = [0, -1, 1, 0]
  9.     for k in range(4):
  10.         if field[i + a[k]][j + b[k]] == '#':
  11.             dfs_repaint(field, i + a[k], j + b[k])
  12.  
  13.  
  14. n, m = map(int, input().split())
  15. m += 2
  16. field = ([['.' for i in range(m)]] + [list('.' + input() + '.') for i in range(n)] +
  17.          [['.' for i in range(m)]])
  18. n += 2
  19. res = 0
  20.  
  21. for i in range(n):
  22.     for j in range(m):py
  23.         if field[i][j] == '#':
  24.             res += 1
  25.             dfs_repaint(field, i, j)
  26.  
  27. print(res)

Here you can reply to the paste above

Make Private

   

Feeling clever? Set some advanced options.