def coronaVirus(arr):
# Constants for the size of the grid
ROW = 9
COL = 20
# To store Inverted Array
inverted = [[0 for _ in range(COL)] for _ in range(ROW)]
# Temporary array to store original array
temp = [[0 for _ in range(COL)] for _ in range(ROW)]
# To store number of infected citizens
count = 0
# To store total number of citizens ('a', 'c')
total = 0
# To invert the array
for i in range(ROW):
for j in range(COL):
# Invert the array row wise
inverted[i][j] = arr[ROW - 1 - i][j]
# Count the number of citizens
for i in range(ROW):
for j in range(COL):
# Count total number of citizens.
if inverted[i][j] == 'a' or inverted[i][j] == 'c':
total += 1
# Copy inverted array in temp
temp[i][j] = inverted[i][j]
# To store number of times, virus encountered a boundary
bound = 0
# Variable for row-wise traversal
i = 1
# Variable for column-wise traversal
j = 1
# Variable to store direction
direct = 1
# The virus starts from (0, 0) always
print("0 0")
# To break infinite looping
flag = 0
# Finding trajectory and safe and infected citizens
while bound <= 1 and flag < 1000:
flag += 1
print(i, j)
# Virus has striked the boundary twice. Therefore, end loop
if inverted[j][i] == '*' and bound == 1:
break
# Virus striked the corners, end loop
if inverted[j][i] == '*':
if (i == 0 and j == ROW - 1) or (i == COL - 1 and j == ROW - 1) or (i == COL - 1 and j == 0):
break
# First time for the virus to hit the boundary
if inverted[j][i] == '*' and bound == 0:
# Increment bound variable
bound += 1
# Strikes the left wall
if i == 0 and j != ROW - 1 and j != 0:
# Turns 90 degree clockwise
if direct == 2:
direct = 1
i += 1
j += 1
elif direct == 4:
direct = 3
j -= 1
i += 1
# Strikes the right wall
elif i == COL - 1 and j != 0 and j != ROW - 1:
# Turns 90 degree anticlockwise
if direct == 1:
direct = 2
i -= 1
j += 1
elif direct == 3:
direct = 4
i -= 1
j -= 1
# Strikes the upper wall
elif j == ROW - 1 and i != 0 and i != COL - 1:
# Turns 90 degree anticlockwise
if direct == 2:
direct = 4
i -= 1
j -= 1
elif direct == 1:
direct = 3
j -= 1
i += 1
# Strikes the lower wall
elif j == 0 and i != 0 and i != COL - 1:
# Turns 90 degree clockwise
if direct == 4:
direct = 2
i -= 1
j += 1
elif direct == 3:
direct = 1
i += 1
j += 1
continue
# Make 'c' visited by replacing it by'-'
if inverted[j][i] == 'c':
temp[j][i] = '-'
# Turns all directions 90 degree clockwise
if direct == 1:
direct = 3
j -= 1
i += 1
elif direct == 2:
direct = 1
i += 1
j += 1
elif direct == 3:
direct = 4
i -= 1
j -= 1
elif direct == 4:
direct = 2
i -= 1
j += 1
# Increment count of infected citizens
count += 1
continue
# Make 'a' visited by replacing it by'-'
if inverted[j][i] == 'a':
temp[j][i] = '-'
# Turns all directions by 90 degree anticlockwise
if direct == 1:
direct = 2
i -= 1
j += 1
elif direct == 2:
direct = 4
i -= 1
j -= 1
elif direct == 3:
direct = 1
i += 1
j += 1
elif direct == 4:
direct = 3
j -= 1
i += 1
# Increment count of infected citizens
count += 1
continue
# Increment the counter diagonally in the given direction.
if inverted[j][i] == '.':
if direct == 1:
i += 1
j += 1
elif direct == 2:
i -= 1
j += 1
elif direct == 3:
j -= 1
i += 1
elif direct == 4:
i -= 1
j -= 1
continue
# Print the mirror of the array i.e. last row must be printed first.
for k in range(ROW):
row = ''
for l in range(COL):
row += temp[ROW - 1 - k][l]
print(row)
# Print safe and infected citizens
print("safe=" + str(total - count))
print("infected=" + str(count))
# Given 2D array
arr = [
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'],
['*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'],
['*', '.', '.', 'c', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'],
['*', '.', '.', '.', '.', 'c', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'],
['*', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'a', '.', '.', '.', '.', '.', '.', '.', '.', '*'],
['*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'],
['*', '.', '.', '.', '.', '.', '.', '.', 'a', '.', '.', '.', '.', '.', '.', 'c', '.', '.', '.', '*'],
['*', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '*'],
['*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*']
]
# Function Call
coronaVirus(arr)