บนกระดานหมากรุกแสดงเป็นตาราง 8 X 8 เราได้รับตำแหน่งของอธิการในรูปแบบของตำแหน่งแถวและคอลัมน์ เป้าหมายคือการหาจำนวนช่องสี่เหลี่ยมทั้งหมดที่อธิการสามารถเยี่ยมชมได้ในคราวเดียว เรารู้ว่าอธิการสามารถเคลื่อนไปได้ทุกทิศทาง (ซ้ายขึ้น/ลงในแนวทแยงและขวาขึ้น/ลง)

ตัวอย่าง
อินพุต
row = 5, column = 4
ผลลัพธ์
Count of total number of squares that can be visited by Bishop in one move are: 13
คำอธิบาย
As shown in above figure the squares that Bishop can cover are 9.
อินพุต
row = 1, column = 1
ผลลัพธ์
Count of total number of squares that can be visited by Bishop in one move are: 7
คำอธิบาย
As this is the corner most position, then Bishop can only cover one diagonal which can have a maximum of 7 squares.
แนวทางที่ใช้ในโปรแกรมด้านล่างมีดังนี้ −
ในแนวทางนี้ เราจะคำนวณสี่เหลี่ยมจัตุรัสในแนวทแยงโดยใช้ตำแหน่งสี่เหลี่ยมจัตุรัสสูงสุดและต่ำสุดในแนวนอนและแนวตั้ง
-
หาตำแหน่งบิชอปจำนวนเต็มแถวและคอลัมน์
-
ฟังก์ชัน squares_visited(int first, int second) รับตำแหน่ง Bishop และคืนค่าจำนวนช่องสี่เหลี่ยมที่สามารถเข้าชมได้ในครั้งเดียว
-
นับเริ่มต้นเป็น 0
-
ตำแหน่งต่ำสุดของตำแหน่งด้านซ้ายคือค่าต่ำสุดของตำแหน่งแถวหรือคอลัมน์ -1
-
ตำแหน่งสูงสุดทางซ้ายคือ 8 − สูงสุดของแถวหรือตำแหน่ง 9−คอลัมน์
-
ตำแหน่งต่ำสุดของตำแหน่งที่ถูกต้องคือค่าต่ำสุดของแถวหรือตำแหน่ง 9−คอลัมน์ -1
-
ตำแหน่งสูงสุดที่ถูกต้องคือ 8 - ตำแหน่งสูงสุดของแถวหรือคอลัมน์
-
ช่องสี่เหลี่ยมทั้งหมดจะเป็นผลรวมของตำแหน่งที่คำนวณข้างต้น
-
ผลตอบแทนนับเป็นผลลัพธ์
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int squares_visited(int first, int second){
int count = 0;
int min_left = min(first, second) − 1;
int max_left = 8 − max(first, 9 − second);
int max_right = 8 − max(first, second);
int min_right = min(first, 9 − second) − 1;
count = min_left + min_right + max_right + max_left;
return count;
}
int main(){
int row = 3, column = 3;
cout<<"Count of total number of squares that can be visited by Bishop in one move are: "<<squares_visited(row, column);
return 0;
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างผลลัพธ์ต่อไปนี้ -
Count of total number of squares that can be visited by Bishop in one move are: 11