1

My goal is to find places in the chess board where I can put a figure of a king. I can put max. two towers in a row and/or a column. I cannot put king in a row or a column where any tower appears. Input is a matrix nxn with '1's (places where I put towers). Output: coordinates where I can put a king. Matrix with towers is not generated automatically.

Really important: I cannot use any loop

My question is: how to combine two arrays together to have such pairs: in:[2, 3, 4] and [1, 3, 4], where 1st array is a row, 2nd is a column out: [2, 1], [2, 3], [2, 4], [3, 1], [3, 3], [3, 4], [4, 1], [4, 3], [4, 4]

import numpy as np
a = np.matrix([[1, 0, 0, 0, 0], [0, 0, 1, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]])
print(a)
print('*for 0*')
y = np.argwhere(a == 0)
z = np.argwhere(a == 1)
rowsy = np.unique(np.array(y[:,0]))
print('rows where 0 appears')
print(rowsy)
colsy = np.unique(np.array(y[:,1]))
print('columns where 0 appears')
print(colsy)
print('*for 1*')
rowsz = np.unique(np.array(z[:,0]))
print('rows where 1 appears')
print(rowsz)
colsz = np.unique(np.array(z[:,1]))
print('columns where 1 appears')
print(colsz)
print('**')
print('diff between rows:')
r0 = np.setdiff1d(rowsy, rowsz)
print(r0)
print('diff between columns:')
r1 = np.setdiff1d(colsy, colsz)
print(r1)
print("**")

shortened version:

import numpy as np
a = np.matrix([[1, 0, 0, 0, 0], [0, 0, 1, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]])
y = np.argwhere(a == 0)
z = np.argwhere(a == 1)
rowsy = np.unique(np.array(y[:,0]))
colsy = np.unique(np.array(y[:,1]))
rowsz = np.unique(np.array(z[:,0]))
colsz = np.unique(np.array(z[:,1]))
r0 = np.setdiff1d(rowsy, rowsz)
r1 = np.setdiff1d(colsy,colsz)

I know it's very general solution but to finish this I need just to combine those last arrays r0 and r1

Kolay.Ne
  • 1,345
  • 1
  • 8
  • 23
Marta
  • 37
  • 10

1 Answers1

1
from itertools import product
list(product([2,3,4], [1,3,4]))
paradox
  • 602
  • 6
  • 13