-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathSpiral_Matrix.cpp
76 lines (62 loc) · 1.47 KB
/
Spiral_Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
using namespace std;
/*
Given a matrix of size N x M.
You have to find the Kth 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
*/
class Solution {
public:
int findK(vector<vector<int>> &a, int n, int m, int k)
{
int cnt = 1;
int size = 0, up = 0, down = n - 1, left = 0, right = m - 1;
while (size < (n * m)) {
for (int j = left; j <= right; ++j) {
if (cnt == k) return a[up][j];
cnt++;
}
for (int i = up + 1; i <= down; ++i) {
if (cnt == k) return a[i][right];
cnt++;
}
for (int j = right - 1; j >= left; --j) {
if (cnt == k) return a[down][j];
cnt++;
}
for (int i = down - 1; i > up; --i) {
if (cnt == k) return a[i][left];
cnt++;
}
size = cnt;
up++, left++, down--; right--;
}
return -1;
}
};
int main()
{
int n, m;
int k = 0;
cin >> n >> m >> k;
vector<vector<int>> a(n, vector<int>(m, 0));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
}
}
Solution obj;
cout << obj.findK(a, n, m, k) << "\n";
}