Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Radiation Search by Kurush
import copy
from collections import deque
def checkio(Matrix):
Visited = [[False for j in range(len(Matrix))] for i in range(len(Matrix))]
MaxValue = 0
MaxNumber = 0
for i in range(len(Matrix)):
for j in range(len(Matrix)):
if Visited[i][j] == False:
Number = Matrix[i][j]
Value = FindSolution(Visited, Matrix, Number, i, j)
if Value > MaxValue:
MaxValue = Value
MaxNumber = Number
return [MaxValue, MaxNumber]
def IsCorrectCell(i, j, Dimension):
if i < 0 or i > Dimension: return False
if j < 0 or j > Dimension: return False
return True
def FindSolution(Visited, Matrix, Number, CurI, CurJ):
Value = 0
PosQueue = deque()
PosQueue.append([CurI, CurJ])
while True:
if not PosQueue: break
CurI, CurJ = PosQueue.popleft()
if IsCorrectCell(CurI, CurJ, len(Matrix) - 1) == False:
continue
if Visited[CurI][CurJ] == True:
continue
if Matrix[CurI][CurJ] != Number:
continue
Value += 1
Visited[CurI][CurJ] = True
PossibleMoves = [
[CurI + 1, CurJ],
[CurI - 1, CurJ],
[CurI, CurJ + 1],
[CurI, CurJ - 1]]
for NewCurI, NewCurJ in PossibleMoves:
PosQueue.append([NewCurI, NewCurJ]);
return Value
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([
[1, 2, 3, 4, 5],
[1, 1, 1, 2, 3],
[1, 1, 1, 2, 2],
[1, 2, 2, 2, 1],
[1, 1, 1, 1, 1]
]) == [14, 1], "14 of 1"
assert checkio([
[2, 1, 2, 2, 2, 4],
[2, 5, 2, 2, 2, 2],
[2, 5, 4, 2, 2, 2],
[2, 5, 2, 2, 4, 2],
[2, 4, 2, 2, 2, 2],
[2, 2, 4, 4, 2, 2]
]) == [19, 2], '19 of 2'
Oct. 9, 2013