你会得到一个8*8的棋盘。除了棋盘,棋盘上还有一位主教,他的位置是众所周知的。Bishop的位置以两位整数的形式给出,其中两位都大于0且小于9(如67表示第6行和第7列)。现在,你的任务是找到多种方法,让你可以把一个棋子安全地放在棋盘上。 例如:
null
Input : Bishop's Position = 11Output : Safe Positions = 56Input : Bishop's Position = 44 Output : Safe Positions = 50
暴力手段: 基本方法之一是迭代棋盘上所有64个可能的位置,并检查该位置是否安全。这种方法需要更多的时间。 更好的方法: 正如我们所知,主教的移动是以对角线的方式进行的,所以从棋盘上的任何位置,主教都可以沿着两条对角线的两个方向移动。所以,所有不在给定主教对角线运动方向上的位置都是安全位置。 现在我们的任务是从毕晓普的位置,找出所有可能的四个方向上的最大长度。
->主教可以从任何位置向四个角移动,即(11、18、81、88)。所以,我们将试图找到主教可以向这些角移动的最大距离。 假设主教的位置是ij,那么:
- 朝向11的距离=分钟(mod(1-i),mod(1-j))。
- 到18的距离=分钟(模(1-i),模(8-j))。
- 朝向81的距离=分钟(mod(8-i),mod(1-j))。
- 朝向88的距离=分钟(mod(8-i),mod(8-j))。
除了这四个位置之外,主教所在的位置也不安全,所以不安全位置的总数是上述结果的总和+1。安全位置总数为64-(总和+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; } |
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. |
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 |
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. |
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. ?> |
Javascript
<script> // Javascript program to find total safe position // to place your Bishop // function to calc total safe position function calcSafe( pos) { // i,j denotes row and column of position of bishop let j = pos % 10; let i = Math.floor(pos /10); // calc distance in four direction let dis_11 = Math.min( Math.abs(1-i), Math.abs(1-j)); let dis_18 = Math.min( Math.abs(1-i), Math.abs(8-j)); let dis_81 = Math.min( Math.abs(8-i), Math.abs(1-j)); let dis_88 = Math.min( Math.abs(8-i), Math.abs(8-j)); // calc total sum of distance + 1 for unsafe positions let sum = dis_11 + dis_18 + dis_81 + dis_88 + 1; // return total safe positions return (64- sum); } // driver code let pos = 34; document.write( "Safe Positions = " + calcSafe(pos)); </script> |
输出:
Safe Positions = 52
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END