Here is the problem I am working on -
There is an \$N \times N\$ field on which missiles are being bombarded. Initially, all the cells in this field have the value \$0\$. There will be \$M\$ missiles bombarded on this field. The \$i\$th missile will have power \$P_i\$ and it will affect all the cells in region with \$(A_i,B_i)\$ as top-left corner and \$(C_i,D_i)\$ as bottom-right corner. Because of missile, value of all the cells in this rectangle will get
XOR
ed with \$P_i\$.After all the missiles have been bombarded, you have to find out values in each cell of this field.
Here is what I have tried -
N = 3
M = 3
missiles = [[3,3,1,3,2],
[2,2,1,2,2],
[3,1,1,2,3]]
from itertools import product
# Logic
coords = {}
for i in missiles:
Pi, Ai, Bi, Ci, Di = i
for j in product(*[range(Ai-1, Ci), range(Bi-1, Di)]):
if coords.get(j):
coords[j] ^= Pi
#[NxN[i[0]][i[1]] ^= Pi
else:
coords[j] = 0 ^ Pi
def print_grid(coords, N):
count = 0
for i in product(*[range(N), range(N)]):
if i in coords:
print(coords[i], "",end="")
else:
print(0, "",end="")
count+=1
if count%N==0:
print()
print_grid(coords, N)
Output -
3 3 3
1 1 3
3 3 0
Exactly does what I want, but I was wondering is there a way to optimize it for large inputs. Any help appreciated.
N x N
be found and presented strictly faster than inN x N
steps? \$\endgroup\$numpy
any good for you? \$\endgroup\$np.array()
out of the coordinate indices and find a way to multiplyPi
directly with the originalnp.zeros((N,N))
that would help. The bottleneck for me seems to be the way I am generating all the points in the rectangle givenAi, Bi, Ci, Di
\$\endgroup\$