You are given a 8*8 chess board. Along with the chess board there is a Bishop placed on board and its position is known. Position of Bishop is given in form of two digit integer where both digits are greater than 0 and less than 9 (like 67 denotes 6th row and 7 column). Now your task is to find the number of ways in which you can place a pawn safely on the board.
Input : Bishop's Position = 11 Output : Safe Positions = 56 Input : Bishop's Position = 44 Output : Safe Positions = 50
Brute Force Approach : One of the basic approach is to iterate through all the 64 possible positions on chess board and check whether that position is safe or not. This approach will require much more time.
Better Approach: As we know that movement of Bishop is in diagonal manner so from any position on the chess board a Bishop can move in both direction of both diagonals. So, all the positions which does not lie on the way of diagonal movement of the given Bishop are the safe positions.
Now our task is to find the maximum length in all possible four direction from the position of Bishop.
-> From any position a Bishop can move towards four corner that are (11, 18, 81, 88). So, we will try to find the find the maximum distance which the Bishop can move towards these corner.
Let position of bishop is ij then:
- Distance towards 11 = min (mod(1-i), mod(1-j) ).
- Distance towards 18 = min (mod(1-i), mod(8-j) ).
- Distance towards 81 = min (mod(8-i), mod(1-j) ).
- Distance towards 88 = min (mod(8-i), mod(8-j) ).
Beside all these four, one position at which Bishop is already placed is also not safe so total number of unsafe position is sum of above results + 1. and total number of safe position is 64 -(sum+1).
Safe Positions = 52
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Construct a string that has exactly K subsequences from given string
- Replace each element of Array with it's corresponding rank
- Largest subset with M as smallest missing number
- Find the node at the centre of an N-ary tree
- Count of submatrix with sum X in a given Matrix
- Check if two arrays can be made equal by reversing any subarray once
- Longest substring that starts with X and ends with Y
- Count of substrings having all distinct characters
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Find largest factor of N such that N/F is less than K
- Select K elements from an array whose maximum value is minimized
- Maximum sum subarray of size range [L, R]
- First subarray with negative sum from the given Array
- Check if the Binary Tree contains a balanced BST of size K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.