Posição total onde o rei pode alcançar em um tabuleiro de xadrez em exatamente M lances | Conjunto 2
Dada a posição do rei em um tabuleiro de xadrez 8 X 8 , a tarefa é contar o número total de casas que podem ser visitadas pelo rei em m lances. A posição do rei é indicada pelo número da linha e da coluna.
Nota: A casa atualmente adquirida pelo rei já foi visitada e será contabilizada no resultado.
Exemplos:
Entrada: r = 4, c = 4, m = 1
Saída: 9
Entrada: r = 4, c = 4, m = 2
Saída: 25
Abordagem: um rei pode se mover uma casa em qualquer direção (ou seja, horizontal, vertical e diagonal). Então, em um movimento, o rei pode visitar seus quadrados adjacentes.
Assim, um quadrado que está a m unidades de distância (considerando 1 quadrado como 1 unidade de distância) da posição atual do rei pode ser visitado em m movimentos.
- Para todas as casas do tabuleiro de xadrez, verifique se uma casa em particular está a m unidade de distância ou menos da posição atual do rei.
- Aumente a contagem, se a etapa 1 for verdadeira.
- Imprimir a contagem
Abaixo está a implementação da abordagem acima:
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count of squares
// that can be visited by king in m moves
int countSquares(int r, int c, int m)
{
// To store the count of squares
int squares = 0;
// Check all squares of
// the chessboard
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
// Check if square (i, j) is
// at a distance <= m units
// from king's current position
if (max(abs(i - r), abs(j - c)) <= m)
squares++;
}
}
// Return count of squares
return squares;
}
// Driver code
int main()
{
int r = 4, c = 4, m = 1;
cout << countSquares(r, c, m) << endl;
return 0;
}
// Java implementation of the approach
class GFG {
// Function to return the count of squares
// that can be visited by king in m moves
static int countSquares(int r, int c, int m)
{
// To store the count of squares
int squares = 0;
// Check all squares of
// the chessboard
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
// Check if square (i, j) is
// at a distance <= m units
// from king's current position
if (Math.max(Math.abs(i - r), Math.abs(j - c)) <= m)
squares++;
}
}
// Return count of squares
return squares;
}
// Driver code
public static void main(String[] args)
{
int r = 4, c = 4, m = 1;
System.out.print(countSquares(r, c, m));
}
}
// C# implementation of the approach
using System;
class GFG {
// Function to return the count of squares
// that can be visited by king in m moves
static int countSquares(int r, int c, int m)
{
// To store the count of squares
int squares = 0;
// Check all squares of
// the chessboard
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
// Check if square (i, j) is
// at a distance <= m units
// from king's current position
if (Math.Max(Math.Abs(i - r), Math.Abs(j - c)) <= m)
squares++;
}
}
// Return count of squares
return squares;
}
// Driver code
public static void Main()
{
int r = 4, c = 4, m = 1;
Console.Write(countSquares(r, c, m));
}
}
# Python implementation of the approach
# Function to return the count of squares
# that can be visited by king in m moves
def countSquares(r, c, m):
# To store the count of squares
squares = 0
# Check all squares of
# the chessboard
for i in range (1, 9):
for j in range (1, 9):
# Check if square (i, j) is
# at a distance <= m units
# from king's current position
if(max(abs(i - r), abs(j - c)) <= m):
squares = squares + 1
# Return count of squares
return squares
# Driver code
r = 4
c = 4
m = 1
print(countSquares(r, c, m));
<?php
// PHP implementation of the approach
// Function to return the count of squares
// that can be visited by king in m moves
function countSquares($r, $c, $m)
{
// To store the count of squares
$squares = 0;
// Check all squares of
// the chessboard
for ($i = 1; $i <= 8; $i++)
{
for ($j = 1; $j <= 8; $j++)
{
// Check if square (i, j) is
// at a distance <= m units
// from king's current position
if (max(abs($i - $r),
abs($j - $c)) <= $m)
$squares++;
}
}
// Return count of squares
return $squares;
}
// Driver code
$r = 4;
$c = 4;
$m = 1;
echo countSquares($r, $c, $m);
// This code is contributed by Ryuga
?>
<script>
// Javascript implementation of the approach
// Function to return the count of squares
// that can be visited by king in m moves
function countSquares(r, c, m)
{
// To store the count of squares
let squares = 0;
// Check all squares of
// the chessboard
for (let i = 1; i <= 8; i++) {
for (let j = 1; j <= 8; j++) {
// Check if square (i, j) is
// at a distance <= m units
// from king's current position
if (Math.max(Math.abs(i - r), Math.abs(j - c)) <= m)
squares++;
}
}
// Return count of squares
return squares;
}
// Driver Code
let r = 4, c = 4, m = 1;
document.write(countSquares(r, c, m));
</script>
9
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva