2
If I need to get the number of diagonal squares in all directions:
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
Welcome to PPCG! This is actually pretty close to be a valid submission for this site, but you should rephrase it as a programming challenge with a description of the input and the expected output rather than a generic programming question. If it is however intended as the latter, Stack Overflow would be a better option (or maybe math.stackexchange.com). – Arnauld – 2019-03-18T12:26:58.153
@Arnauld Thank you, I will rephrase it. – Ibrahim Ali – 2019-03-18T12:29:26.600
@Arnauld Not sure if this is on topic on Stackoverflow either.. Looks more like a math program, since OP is looking for a formula to calculate the diagonal lengths (which could be solved with a program of course, but could also be solved with a formula like OP did for both diagonals). I agree that it could be a nice code-golf challenge if reworded a bit however. – Kevin Cruijssen – 2019-03-18T12:31:58.807
1Provide 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. – Stephen – 2019-03-18T12:55:08.007
@Stephen I added examples, I hope that's clear now. – Ibrahim Ali – 2019-03-18T13:01:36.950
You should remove the horizontals and verticals from your graphic, to avoid confusion. – Shaggy – 2019-03-18T13:12:21.583
1Can there ever be more than one "soldier" on the board? – Shaggy – 2019-03-18T13:14:30.823
@Shaggy Yes, there is can be many soldiers on the board, I will edit the question , sorry. – Ibrahim Ali – 2019-03-18T13:16:13.437
1What is a 'soldier'? Does it matter? It seems to just be a piece blocking the way. – Rɪᴋᴇʀ – 2019-03-18T14:24:43.740
1@Riker Yes, we called it soldier, I don't why it's off-topic – Ibrahim Ali – 2019-03-18T14:41:32.830
3You 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 [tag: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. – mbomb007 – 2019-03-18T19:43:40.170
1@IbrahimAli I voted as off-topic, but I should've voted as unclear. I don't understand exactly how this is supposed to work. Could you possibly provide a short example program (or pseudocode)? I'm uncertain what square we're counting. – Rɪᴋᴇʀ – 2019-03-18T23:54:12.807
2Err, shouldn't you be using the bishop as the example piece, rather than the queen? – Jo King – 2019-03-19T04:55:08.067
1Does "Assume I have that program to compute all legal diagonals" mean that we have a free function to calculate $2N-2-|x-y|-|x+y-N-1|$? Is "Now, what if you wanted to get the diagonal squares, but only in one direction?" relevant at all? Why does the last example give 5 rather than 6? – Peter Taylor – 2019-03-19T08:33:59.397
@JoKing In old image, There was up and down steps, but someone suggest to remove these steps. – Ibrahim Ali – 2019-03-19T09:05:11.347
@PeterTaylor I just showing what I achieved, and you are correct, last example should be 6, my fault. – Ibrahim Ali – 2019-03-19T09:08:32.380
I think the problem with the phrasing is that the title and the first few sentences imply that you're figuring out the amount of accessible diagonal squares in one direction, and then the very last sentence throws in other pieces out of nowhere. You should reword the title to something like "How many places can my bishop go?" and remove references to moving in one direction – Jo King – 2019-03-19T09:21:15.540
@JoKing You're right, The examples doesn't demonstrate what Exactly I need, It can be "How many places can my bishop go in one direction?", because I can get diagonals already in all direction, I will edit it for further explanation. – Ibrahim Ali – 2019-03-19T09:26:06.333