코드 >>
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
77
78
79
80
81
|
#include <iostream>
#include <vector>
int xsize = 4;
int ysize = 4;
typedef struct
{
int x;
int y;
} coordXY;
coordXY getcoordXY(coordXY BeforeXY)
{
coordXY newcoordXY;
newcoordXY.x = ysize - BeforeXY.y -1;
newcoordXY.y = BeforeXY.x;
return newcoordXY;
};
std::vector<std::vector<int>> inplaceRotateImage(std::vector<std::vector<int>>& image)
{
coordXY rot90;
coordXY rot180;
coordXY rot270;
for (int y=0; y<(ysize+1)/2; y++)
{
for (int x=0; x<(xsize/2); x++)
{
coordXY org;
org.x = x;
org.y = y;
int temp0 = image[y][x];
rot90 = getcoordXY(org);
rot180 = getcoordXY(rot90);
rot270 = getcoordXY(rot180);
image[y][x] = image[rot270.y][rot270.x];
image[rot270.y][rot270.x] = image[rot180.y][rot180.x];
image[rot180.y][rot180.x] = image[rot90.y][rot90.x];
image[rot90.y][rot90.x] = temp0;
}
}
return image;
}
int main()
{
//[1, 2, 3, 4] => [13, 9, 5, 1]
//[5, 6, 7, 8] => [14, 10, 6, 2]
//[9, 10, 11, 12] => [15, 11, 7, 3]
//[13, 14, 15, 16] => [16, 12, 8, 4]
coordXY testcoor;
testcoor.x = 10;
testcoor.y = 12;
std::cout << "testcoor.x : " << testcoor.x << " testcoor.y : " << testcoor.y << std::endl;
std::vector<std::vector<int>> image;
image.emplace_back(std::vector<int>{1,2,3,4});
image.emplace_back(std::vector<int>{5,6,7,8});
image.emplace_back(std::vector<int>{9,10,11,12});
image.emplace_back(std::vector<int>{13,14,15,16});
image = inplaceRotateImage(image);
for (const auto& image_array : image)
{
for (const auto& image_element : image_array)
{
std::cout << image_element << " ";
}
std::cout << std::endl;
}
std::cout<<"Hello World";
return 0;
}
|
'C' 카테고리의 다른 글
c++ 문자열 검색 O(N)으로 하기 with Rabin-Karp 알고리즘 (0) | 2021.07.05 |
---|---|
c++ O(N)으로 2D matrix 원소 찾기 (0) | 2021.07.04 |
c++ leetcode array find duplicate by O(N) (0) | 2021.07.04 |
c++ leetcode 배열 Shorted Unsorted Continuous Subarray (0) | 2021.07.03 |
c++ leet code medium merge_interval 구현 (0) | 2021.07.02 |