Advertisement
Egor_1425

Untitled

Feb 9th, 2024 (edited)
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. from collections import deque
  2.  
  3. def check(row, col, a, color, q):
  4.     empty = 0
  5.     if not color[row][col]:
  6.         if a[row + 1][col] == '.':
  7.             empty += 1
  8.             if not color[row + 1][col]:
  9.                 q.append(row + 1)
  10.                 q.append(col)
  11.         if a[row - 1][col] == '.':
  12.             empty += 1
  13.             if not color[row - 1][col]:
  14.                 q.append(row - 1)
  15.                 q.append(col)
  16.         if a[row][col + 1] == '.':
  17.             empty += 1
  18.             if not color[row][col + 1]:
  19.                 q.append(row)
  20.                 q.append(col + 1)
  21.         if a[row][col - 1] == '.':
  22.             empty += 1
  23.             if not color[row][col - 1]:
  24.                 q.append(row)
  25.                 q.append(col - 1)
  26.         color[row][col] = 1
  27.         return 4 - empty
  28.     return 0
  29.  
  30. n = int(input())
  31. a = [['*'] * (n+2) for i in range(n + 2)]
  32. color = [[0] * (n+2) for i in range(n + 2)]
  33.  
  34. for i in range(1, n + 1):
  35.     row = input()
  36.     for j in range(1, n + 1):
  37.         a[i][j] = row[j - 1]
  38.  
  39. q = deque()
  40. q.append(1)
  41. q.append(1)
  42. walls = 0
  43.  
  44. while q:
  45.     row = q.popleft()
  46.     col = q.popleft()
  47.     walls += check(row, col, a, color, q)
  48.  
  49. if not color[n][n]:
  50.     q.append(n)
  51.     q.append(n)
  52.     while q:
  53.         row = q.popleft()
  54.         col = q.popleft()
  55.         walls += check(row, col, a, color, q)
  56.  
  57. walls -= 4
  58. m = walls * 9
  59. print(m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement