-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2018 J4-S2.cpp
115 lines (69 loc) · 1.98 KB
/
2018 J4-S2.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
2018 J4/S2 - Sunflowers
Difficulty: Easy/Medium
Topics: Ad Hoc, 2-D Arrays
The only difficult part of this question is rotating the grid 90 degrees.
General algorithm:
1. Check if grid is valid
2. If not, rotate the grid
3. Repeat steps 1 to 2 until valid
*/
#include <iostream>
#include <vector>
int N; //Global declaration so I can access it in my functions
//Check if grid is correct, least to greatest from left to right and top to bottom
bool correct (std::vector<std::vector<int>> data){
//Left to right check
for (int i = 0; i < N; i++){
for (int j = 1; j < N; j++){
if (data[i][j] < data[i][j - 1]){
return false;
}
}
}
//Top to bottom check
for (int j = 0; j < N; j++){
for (int i = 1; i < N; i++){
if (data[i][j] < data[i - 1][j]){
return false;
}
}
}
return true;
}
//Rotate grid
std::vector<std::vector<int>> rotate (std::vector<std::vector<int>> data){
std::vector<std::vector<int>> copy = data; //Make a copy, so that changes are seperate
int jj = 0; //Represent column index that I'm taking from the original grid
for (int i = 0; i < N; i++){
int ii = data.size() - 1; //Reoresents the row index that I'm taking from the original grid
for (int j = 0; j < N; j++){
copy[i][j] = data[ii][jj];
ii--;
}
jj++;
}
return copy;
}
int main(){
std::cin >> N;
std::vector<std::vector<int>> data (N, std::vector<int> (N));
//Collect data
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
std::cin >> data[i][j];
}
}
//Rotate until correct
while (!correct(data)){
data = rotate(data);
}
//Output answer
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
std::cout << data[i][j] << ' ';
}
std::cout << std::endl;
}
return 0;
}