给定大小为n X n的mat [] [] ,任务是找到一个元素X ,如果从X开始逆时针遍历,则要打印的最后一个元素为mat [n – 1] [n – 1] 。
The anti-clockwise traversal of the matrix, mat[][] =
{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
starting at element 5 will be 5, 6, 3, 2, 1, 4, 7, 8, 9.
例子:
Input: mat[][] = {{1, 2}, {3, 4}}
Output: 2
If we start traversing from mat[0][1] i.e. 2 then
we will end up with the element at mat[1][1] which is 4.
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output: 5
方法:从mat [n – 1] [n – 1]处的元素开始,以相反的顺序(即顺时针)遍历矩阵。当遍历矩阵的所有元素时,最后访问的元素将是结果。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print last element
// Of given matrix
int printLastElement(int mat[][2], int n)
{
// Starting row index
int si = 0;
// Starting column index
int sj = 0;
// Ending row index
int ei = n - 1;
// Ending column index
int ej = n - 1;
// Track the move
int direction = 0;
// While starting index is less than ending
// row index or starting column index
// is less the ending column index
while (si < ei || sj < ej) {
// Switch cases for all direction
// Move under all cases for all
// directions
switch (direction % 4) {
case 0:
sj++;
break;
case 1:
ei--;
break;
case 2:
ej--;
break;
case 3:
si++;
break;
}
// Increment direction by one
// for each case
direction++;
}
// Finally return the last element
// If not found return 0
return mat[si][sj];
return 0;
}
// Driver code
int main()
{
int n = 2;
int mat[][2] = { { 1, 2 }, { 3, 4 } };
cout << printLastElement(mat, n);
return 0;
}
Java
// Java implementation of the approach
class GfG
{
// Function to print last element
// Of given matrix
static int printLastElement(int mat[][], int n)
{
// Starting row index
int si = 0;
// Starting column index
int sj = 0;
// Ending row index
int ei = n - 1;
// Ending column index
int ej = n - 1;
// Track the move
int direction = 0;
// While starting index is less than ending
// row index or starting column index
// is less the ending column index
while (si < ei || sj < ej)
{
// Switch cases for all direction
// Move under all cases for all
// directions
switch (direction % 4)
{
case 0:
sj++;
break;
case 1:
ei--;
break;
case 2:
ej--;
break;
case 3:
si++;
break;
}
// Increment direction by one
// for each case
direction++;
}
// Finally return the last element
// If not found return 0
return mat[si][sj];
}
// Driver code
public static void main(String[] args)
{
int n = 2;
int mat[][] = new int[][]{{ 1, 2 }, { 3, 4 }};
System.out.println(printLastElement(mat, n));
}
}
// This code is contributed by Prerna Saini
Python3
# Python 3 implementation of the approach
# Function to print last element
# Of given matrix
def printLastElement(mat, n):
# Starting row index
si = 0
# Starting column index
sj = 0
# Ending row index
ei = n - 1
# Ending column index
ej = n - 1
# Track the move
direction = 0
# While starting index is less than ending
# row index or starting column index
# is less the ending column index
while (si < ei or sj < ej):
# Switch cases for all direction
# Move under all cases for all
# directions
if (direction % 4 == 0):
sj += 1
if (direction % 4 == 1):
ei -= 1
if (direction % 4 == 2):
ej -= 1
if (direction % 4 == 3):
si += 1
# Increment direction by one
# for each case
direction += 1
# Finally return the last element
# If not found return 0
return mat[si][sj]
return 0
# Driver code
if __name__ == '__main__':
n = 2
mat = [[1, 2], [3, 4 ]]
print(printLastElement(mat, n))
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to print last element
// Of given matrix
static int printLastElement(int [,]mat, int n)
{
// Starting row index
int si = 0;
// Starting column index
int sj = 0;
// Ending row index
int ei = n - 1;
// Ending column index
int ej = n - 1;
// Track the move
int direction = 0;
// While starting index is less than ending
// row index or starting column index
// is less the ending column index
while (si < ei || sj < ej)
{
// Switch cases for all direction
// Move under all cases for all
// directions
switch (direction % 4)
{
case 0:
sj++;
break;
case 1:
ei--;
break;
case 2:
ej--;
break;
case 3:
si++;
break;
}
// Increment direction by one
// for each case
direction++;
}
// Finally return the last element
// If not found return 0
return mat[si, sj];
}
// Driver code
public static void Main()
{
int n = 2;
int [,]mat = {{ 1, 2 }, { 3, 4 }};
Console.WriteLine(printLastElement(mat, n));
}
}
// This code is contributed by Ryuga
PHP
Javascript
输出:
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。