If I need to get the number of diagonal squares in all directions:

Queen on E4 with its diagonals traced outwards[1]

I do the following formula 2 N − 2 − |x − y| − |x + y − N − 1|

The above example has 13 and that's what the formula gives.

Now, what if you wanted to get the diagonal squares, but only in one direction?

The top right has 4 squares, the top left has 3, the bottom left has 3 and the bottom right has 3.

Assume I have that program to compute all legal diagonals for the Queen, and as I demonstrated above I can get all diagonals by one step using above formula, written like :

int positiveDiagonal = Math.abs(r_q - c_q);
int negativeDiagonal = Math.abs(r_q + c_q - n - 1);
int totalDiagonal = 2 * n - 2 - positiveDiagonal - negativeDiagonal;

So, assume there is a soldier on position (5,5), that's make the Queen can't move to others diagonals in the same direction with total 4 count.

Sample Input - 1:

  • Board length = 8
  • Queen on position (4, 4)
  • There is no soldiers.

  • Explanation :

    • Because there is no pieces (soldiers), we just get all diagonals we calculated using our formula.

Output - 1: 13

Sample Input - 2:

  • Board length = 8
  • Queen on position (4, 4)
  • There is a soldiers on point (5, 3)

  • Explanation :

    • Because there is a piece or whatever, on point (5, 3), The Queen can't go to (5, 3), (6, 2) and (7, 1).

Output - 2: The total diagonals we got from formula is 13 and by calculating the soldier diagonals [The steps that queen can't go through], the result would be 10.

Sample Input - 3:

  • Board length = 5
  • Queen on position (5, 5)
  • There is a soldiers on point (1, 1)

  • Explanation :

    • Because there is a piece (soldier), on point (1, 1), The Queen can't go to that square.

Output - 3: By subtracting this square from total diagonals which equal 4 it's results 3

Sample Input - 4:

  • Board length = 5
  • Queen on position (3, 3)
  • Soldiers on positions (5, 5) , (2, 1), and (4, 4)

  • Explanation :

    • Because there is a piece (soldier), on point (5, 5) and (4, 4), The Queen can't go to these squares.

Output - 4 = 6


closed as unclear what you're asking by Sriotchilism O'Zaic, NoOneIsHere, Xcali, Jo King, Embodiment of Ignorance Mar 30 at 17:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Provide a standard way for input and output - input could be position of queen and which diagonal, output could be length of diagonal, just a suggestion. But you will need to provide sample inputs and outputs. Do that, and I can almost promise you someone will figure it out. \$\endgroup\$ – Stephen Mar 18 at 12:55
  • 1
    \$\begingroup\$ Can there ever be more than one "soldier" on the board? \$\endgroup\$ – Shaggy Mar 18 at 13:14
  • 1
    \$\begingroup\$ What is a 'soldier'? Does it matter? It seems to just be a piece blocking the way. \$\endgroup\$ – Rɪᴋᴇʀ Mar 18 at 14:24
  • 3
    \$\begingroup\$ You say "There is may a way to calculate that via loops, but I really prefer formulas as it's saving much time and efforts", but with code-golf you don't get to determine what method people use. Maybe that part should be removed or rephrased, as I'm not really sure what it adds to the question. \$\endgroup\$ – mbomb007 Mar 18 at 19:43
  • 2
    \$\begingroup\$ Err, shouldn't you be using the bishop as the example piece, rather than the queen? \$\endgroup\$ – Jo King Mar 19 at 4:55

Jelly, 41 37 bytes


Try it online!

A dyadic link that takes as its left argument the zero-indexed positions of the queen and any soldiers as a list (queen first, any subsequent entries a solider) and as right argument the board size. Returns an integer that represents the number of squares available for moves on the diagonal.


æ.                                    | dot product of piece positions with board size, right-padded with 1
  µ                                   | start new chain with this as input
   Ṭ                                  | make a boolean array with 1 at the position of each piece
    +ḢṬ$                              | and add the same again for the Queen so the Queen has a 2
        +⁴²x@0¤                       | expand to the board dimension square with zeroes
               s⁴                     | split into lists the length of the board dimension
                 µ                    | start a new chain
                  ŒD;UŒD$             | concatenate the diagonals and the diagonals of the flipped board
                         ṣ€1          | split at soldiers
                            Ẏ         | tighten (make a single list of lists)
                             =2Ẹ$Ƈ    | filter keeping only those diagonals with the Queen in
                                 F¬S  | count the number of zeroes

Not the answer you're looking for? Browse other questions tagged or ask your own question.