P17132 [GeeksforGeeks] Cd61add036272faa69c6814e34aa7007d5a25aa6
普及
题目描述

Given a matrix of size N x M. You have to find the K^{th} element which will obtain while traversing the matrix spirally starting from the top-left corner of the matrix.
Example 1:
Input:
N = 3, M = 3, K = 4
A[] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output:
6
Explanation: Spiral traversal of matrix:
{1, 2, 3, 6, 9, 8, 7, 4, 5}. Fourth element
is 6.
Example 2:
Input:
N = 2, M = 2, K = 2
A[] = {{1, 2},
{3, 4}}
Output:
2
Explanation: Spiral traversal of matrix:
{1, 2, 4, 3}. Second element is 2.
Your Task:
You don't need to read input or print anything. Complete the function findK() which takes the matrix A[ ][ ], number of rows N, number of columns M, and integer K as input parameters and returns the K^{th} element in the spiral traversal of the matrix.
Expected Time Complexity: O(NM)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ K ≤ N
M ≤ 10^{6}

输入格式

见题面

输出格式

见题面

样例输入
N = 3, M = 3, K = 4 A[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
样例输出
6