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.

Examples:

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).

## C++

`// CPP program to find total safe position ` `// to place your Bishop ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calc total safe position ` `int` `calcSafe(` `int` `pos) ` `{ ` ` ` `// i,j denotes row and column of position of bishop ` ` ` `int` `j = pos % 10; ` ` ` `int` `i = pos /10; ` ` ` ` ` `// calc distance in four direction ` ` ` `int` `dis_11 = min ( ` `abs` `(1-i), ` `abs` `(1-j)); ` ` ` `int` `dis_18 = min ( ` `abs` `(1-i), ` `abs` `(8-j)); ` ` ` `int` `dis_81 = min ( ` `abs` `(8-i), ` `abs` `(1-j)); ` ` ` `int` `dis_88 = min ( ` `abs` `(8-i), ` `abs` `(8-j)); ` ` ` ` ` `// calc total sum of distance + 1 for unsafe positions ` ` ` `int` `sum = dis_11 + dis_18 + dis_81 + dis_88 + 1; ` ` ` ` ` `// return total safe positions ` ` ` `return` `(64- sum); ` `} ` ` ` `// driver function ` `int` `main() ` `{ ` ` ` `int` `pos = 34; ` ` ` `cout << ` `"Safe Positions = "` `<< calcSafe(pos); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find total safe position ` `// to place your Bishop ` `class` `GFG ` `{ ` ` ` ` ` `// function to calc total safe position ` ` ` `static` `int` `calcSafe(` `int` `pos) ` ` ` `{ ` ` ` ` ` `// i,j denotes row and column of position of bishop ` ` ` `int` `j = pos % ` `10` `; ` ` ` `int` `i = pos /` `10` `; ` ` ` ` ` `// calc distance in four direction ` ` ` `int` `dis_11 = Math.min ( Math.abs(` `1` `-i), Math.abs (` `1` `-j)); ` ` ` `int` `dis_18 = Math.min ( Math.abs(` `1` `-i), Math.abs (` `8` `-j)); ` ` ` `int` `dis_81 = Math.min ( Math.abs(` `8` `-i), Math.abs (` `1` `-j)); ` ` ` `int` `dis_88 = Math.min ( Math.abs(` `8` `-i), Math.abs (` `8` `-j)); ` ` ` ` ` `// calc total sum of distance + 1 for unsafe positions ` ` ` `int` `sum = dis_11 + dis_18 + dis_81 + dis_88 + ` `1` `; ` ` ` ` ` `// return total safe positions ` ` ` `return` `(` `64` `- sum); ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `pos = ` `34` `; ` ` ` ` ` `System.out.print(` `"Safe Positions = "` `+calcSafe(pos)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# python program to find total safe ` `# position to place your Bishop ` `import` `math ` ` ` `# function to calc total safe position ` `def` `calcSafe(pos): ` ` ` ` ` `# i,j denotes row and column of ` ` ` `# position of bishop ` ` ` `j ` `=` `pos ` `%` `10` ` ` `i ` `=` `pos ` `/` `10` ` ` ` ` `# calc distance in four direction ` ` ` `dis_11 ` `=` `min` `( ` `abs` `(` `1` `-` `i), ` `abs` `(` `1` `-` `j)) ` ` ` `dis_18 ` `=` `min` `( ` `abs` `(` `1` `-` `i), ` `abs` `(` `8` `-` `j)) ` ` ` `dis_81 ` `=` `min` `( ` `abs` `(` `8` `-` `i), ` `abs` `(` `1` `-` `j)) ` ` ` `dis_88 ` `=` `min` `( ` `abs` `(` `8` `-` `i), ` `abs` `(` `8` `-` `j)) ` ` ` ` ` `# calc total sum of distance + 1 ` ` ` `# for unsafe positions ` ` ` `sum` `=` `(dis_11 ` `+` `dis_18 ` `+` `dis_81 ` ` ` `+` `dis_88 ` `+` `1` `) ` ` ` ` ` `# return total safe positions ` ` ` `return` `(` `64` `-` `sum` `) ` ` ` ` ` `# driver function ` `pos ` `=` `34` `print` `(` `"Safe Positions = "` `, ` ` ` `math.ceil(calcSafe(pos))) ` ` ` `# This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## C#

`// Program to find the total safe ` `// positions to place your Bishop ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to calc total safe position ` ` ` `static` `int` `calcSafe(` `int` `pos) ` ` ` `{ ` ` ` ` ` `// i, j denotes row and column of ` ` ` `// position of bishop ` ` ` `int` `j = pos % 10; ` ` ` `int` `i = pos / 10; ` ` ` ` ` `// calc distance in four direction ` ` ` `int` `dis_11 = Math.Min(Math.Abs(1 - i), Math.Abs(1 - j)); ` ` ` `int` `dis_18 = Math.Min(Math.Abs(1 - i), Math.Abs(8 - j)); ` ` ` `int` `dis_81 = Math.Min(Math.Abs(8 - i), Math.Abs(1 - j)); ` ` ` `int` `dis_88 = Math.Min(Math.Abs(8 - i), Math.Abs(8 - j)); ` ` ` ` ` `// calc total sum of distance + 1 ` ` ` `// for unsafe positions ` ` ` `int` `sum = dis_11 + dis_18 + dis_81 + dis_88 + 1; ` ` ` ` ` `// return total safe positions ` ` ` `return` `(64 - sum); ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `pos = 34; ` ` ` ` ` `Console.WriteLine(` `"Safe Positions = "` `+ calcSafe(pos)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// total safe position ` `// to place your Bishop ` ` ` `// function to calculate ` `// total safe position ` `function` `calcSafe( ` `$pos` `) ` `{ ` ` ` ` ` `// i,j denotes row and ` ` ` `// column of position ` ` ` `// of bishop ` ` ` `$j` `= ` `$pos` `% 10; ` ` ` `$i` `= ` `$pos` `/10; ` ` ` ` ` `// calc distance in four direction ` ` ` `$dis_11` `= min(` `abs` `(1 - ` `$i` `), ` ` ` `abs` `(1 - ` `$j` `)); ` ` ` `$dis_18` `= min(` `abs` `(1 - ` `$i` `), ` ` ` `abs` `(8 - ` `$j` `)); ` ` ` `$dis_81` `= min(` `abs` `(8 - ` `$i` `), ` ` ` `abs` `(1 - ` `$j` `)); ` ` ` `$dis_88` `= min(` `abs` `(8 - ` `$i` `), ` ` ` `abs` `(8 - ` `$j` `)); ` ` ` ` ` `// calc total sum of ` ` ` `// distance + 1 for ` ` ` `// unsafe positions ` ` ` `$sum` `= ` `$dis_11` `+ ` `$dis_18` `+ ` ` ` `$dis_81` `+ ` `$dis_88` `+ 1; ` ` ` ` ` `// return total safe positions ` ` ` `return` `ceil` `(64- ` `$sum` `); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$pos` `= 34; ` ` ` `echo` `"Safe Positions = "` `,calcSafe(` `$pos` `); ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Safe Positions = 52

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum non-attacking Rooks that can be placed on an N*N Chessboard
- Find all the queens attacking the king in a chessboard
- Queries to find longest subsequence having no smilar adjacent elements with updates
- Search an element in a Doubly Linked List
- Program to find the Nth natural number with exactly two bits set | Set 2
- Check if a non-contiguous subsequence same as the given subarray exists or not
- Find the next greater element in a Circular Array | Set 2
- Replace even-indexed characters of minimum number of substrings to convert a string to another
- Count subarrays having an equal count of 0s and 1s segregated
- Longest subarray in which all elements are smaller than K
- C program to count frequency of each element in an array
- Longest subsequence having maximum sum
- Farthest index that can be reached from the Kth index of given array by given operations
- Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.