fork download
  1. # your code goes here
  2. input = ["5, 5","F,2,1","F,2,3", "F,2,4", "F,0,4"]
  3. i = 0
  4. rows, cols = 0, 0
  5. grid = [[]]
  6. for line in input:
  7. if i == 0:
  8. temp = line.split(",")
  9. rows = int(temp[0])
  10. cols = int(temp[1])
  11. grid = [['B']*cols for _ in range(rows)]
  12. else:
  13. temp = line.split(",")
  14. c = temp[0]
  15. x = int(temp[1])
  16. y = int(temp[2])
  17. grid[y][x] = c
  18. i += 1
  19.  
  20. leftPrefix = [[0]*cols for _ in range(rows)]
  21. rightPrefix = [[0]*cols for _ in range(rows)]
  22. topPrefix = [[0]*cols for _ in range(rows)]
  23. bottomPrefix = [[0]*cols for _ in range(rows)]
  24.  
  25. #left
  26. for i in range(0, rows):
  27. if grid[i][0] == 'F':
  28. leftPrefix[i][0] = 1
  29. for j in range(1, cols):
  30. if grid[i][j] == 'B':
  31. leftPrefix[i][j] = leftPrefix[i][j-1]
  32. elif grid[i][j] == 'F':
  33. leftPrefix[i][j] = leftPrefix[i][j-1] + 1
  34.  
  35. #right
  36. for i in range(0, rows):
  37. if grid[i][cols-1] == 'F':
  38. rightPrefix[i][cols-1] = 1
  39. for j in range(cols-2, -1, -1):
  40. if grid[i][j] == 'B':
  41. rightPrefix[i][j] = rightPrefix[i][j+1]
  42. elif grid[i][j] == 'F':
  43. rightPrefix[i][j] = rightPrefix[i][j+1] + 1
  44.  
  45. # topPrefix
  46. for i in range(0, rows):
  47. if i == 0:
  48. for j in range(0, cols):
  49. topPrefix[i][j] = 1 if grid[i][j] == 'F' else 0
  50. else:
  51. for j in range(0, cols):
  52. if grid[i][j] == 'B':
  53. topPrefix[i][j] = topPrefix[i-1][j]
  54. elif grid[i][j] == 'F':
  55. topPrefix[i][j] = topPrefix[i-1][j] + 1
  56.  
  57. # bottom
  58. for i in range(rows-1, -1, -1):
  59. if i == rows-1:
  60. for j in range(0, cols):
  61. bottomPrefix[i][j] = 1 if grid[i][j] == 'F' else 0
  62. else:
  63. for j in range(0, cols):
  64. if grid[i][j] == 'B':
  65. bottomPrefix[i][j] = bottomPrefix[i+1][j]
  66. elif grid[i][j] == 'F':
  67. bottomPrefix[i][j] = bottomPrefix[i+1][j] + 1
  68.  
  69. output = [['B']*cols for _ in range(rows)]
  70. overAll = [[0]*cols for _ in range(rows)]
  71. for i in range(0, rows):
  72. for j in range(0, cols):
  73. output[i][j] = grid[i][j]
  74.  
  75. maxm = 0
  76. for i in range(0, rows):
  77. for j in range(0, cols):
  78. if grid[i][j] != 'B':
  79. continue
  80. left = leftPrefix[i][j-1] if j-1>=0 else 0
  81. right = rightPrefix[i][j+1] if j+1<cols else 0
  82. top = topPrefix[i-1][j] if i-1>=0 else 0
  83. bottom = bottomPrefix[i+1][j] if i+1<rows else 0
  84. overAll[i][j] = left + right + top + bottom
  85. maxm = max(maxm, overAll[i][j])
  86.  
  87. for i in range(0, rows):
  88. for j in range(0, cols):
  89. if overAll[i][j] == maxm and grid[i][j] == 'B':
  90. output[i][j] = '*'
  91.  
  92. print "\n".join("".join(str(el) for el in r) for r in output)
  93.  
Success #stdin #stdout 0.01s 7216KB
stdin
Standard input is empty
stdout
BB*BB
BBFBB
BB*BB
BBFBB
FBFBB