📜  在螺旋填充矩阵的指定索引处查找元素

📅  最后修改于: 2021-09-07 02:02:04             🧑  作者: Mango

给定两个整数xy 分别代表矩阵中的行号和列号,任务是在螺旋填充矩阵的单元格(x, y)处找到整数。

例子:

方法:
为了解决这个问题,需要理解给定的螺旋填充矩阵背后的逻辑。以下是需要考虑的可能情况:

  • 情况 1:如果 y > x & y 是奇数
    (x, y)处的元素等于y 2 – x +1
  • 情况 2:如果 y > x & y 是偶数
    (x, y)处的元素等于(y – 1) 2 + x
  • 情况 3:如果 x ≥ y & x 是偶数
    (x, y)处的元素等于x 2 – y +1
  • 情况 4:如果如果 x ≥ y & x 是奇数
    (x, y)处的元素等于(x – 1) 2 + y

因此,为了解决这个问题,我们需要根据给定的xy满足的条件计算并打印方程的结果。

下面是上述方法的实现:

C++
// C++ Program to find the element
// at given position in spirally
// filled matrix
#include 
using namespace std;
 
// Function to return the
// element at (x, y)
int SpiralElement(int x, int y)
{
    int r;
 
    // If y is greater
    if (x < y) {
 
        // If y is odd
        if (y % 2 == 1) {
            r = y * y;
            return (r - x + 1);
        }
 
        // If y is even
        else {
            r = (y - 1) * (y - 1);
            return (r + x);
        }
    }
 
    else {
 
        // If x is even
        if (x % 2 == 0) {
            r = x * x;
            return (r - y + 1);
        }
 
        // If x is odd
        else {
            r = (x - 1) * (x - 1);
            return (r + y);
        }
    }
}
 
// Driver Code
int main()
{
 
    int x = 2, y = 3;
    cout << SpiralElement(x, y);
    return 0;
}


Java
// Java program to find the element
// at given position in spirally
// filled matrix
import java.util.*;
 
class GFG{
 
// Function to return the
// element at (x, y)
static int SpiralElement(int x, int y)
{
    int r;
 
    // If y is greater
    if (x < y)
    {
 
        // If y is odd
        if (y % 2 == 1)
        {
            r = y * y;
            return (r - x + 1);
        }
 
        // If y is even
        else
        {
            r = (y - 1) * (y - 1);
            return (r + x);
        }
    }
    else
    {
 
        // If x is even
        if (x % 2 == 0)
        {
            r = x * x;
            return (r - y + 1);
        }
 
        // If x is odd
        else
        {
            r = (x - 1) * (x - 1);
            return (r + y);
        }
    }
}
 
// Driver code
public static void main(String[] args)
{
    int x = 2, y = 3;
     
    System.out.println(SpiralElement(x, y));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to find the element
# at given position in spirally
# filled matrix
 
# Function to return the
# element at (x, y)
def SpiralElement(x, y):
     
    r = 0
 
    # If y is greater
    if (x < y):
 
        # If y is odd
        if (y % 2 == 1):
            r = y * y
            return (r - x + 1)
         
        # If y is even
        else:
            r = (y - 1) * (y - 1)
            return (r + x)
             
    else:
 
        # If x is even
        if (x % 2 == 0):
            r = x * x
            return (r - y + 1)
         
        # If x is odd
        else:
            r = (x - 1) * (x - 1)
            return (r + y)
     
# Driver code
if __name__ == '__main__':
     
    x = 2
    y = 3
 
    print(SpiralElement(x, y))
 
# This code is contributed by Amit Katiyar


C#
// C# program to find the element
// at given position in spirally
// filled matrix
using System;
  
class GFG{
     
// Function to return the
// element at (x, y)
static int SpiralElement(int x, int y)
{
    int r;
  
    // If y is greater
    if (x < y)
    {
         
        // If y is odd
        if (y % 2 == 1)
        {
            r = y * y;
            return (r - x + 1);
        }
  
        // If y is even
        else
        {
            r = (y - 1) * (y - 1);
            return (r + x);
        }
    }
    else
    {
  
        // If x is even
        if (x % 2 == 0)
        {
            r = x * x;
            return (r - y + 1);
        }
  
        // If x is odd
        else
        {
            r = (x - 1) * (x - 1);
            return (r + y);
        }
    }
}
 
// Driver code
static public void Main()
{
    int x = 2, y = 3;
     
    Console.WriteLine(SpiralElement(x, y));
}
}
 
// This code is contributed by offbeat


Javascript


输出:
8

时间复杂度: O(1)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live