I have created a code that will classify a 2D array rotation / reflection into one of these categories →
#1: The arrangement is rotated 90 degrees clockwise.
#2: The arrangement is rotated 180 degrees clockwise.
#3: The arrangement is rotated 270 degrees clockwise.
#4: The arrangement is reflected horizontally (see the sample input).
#5: The arrangement is reflected horizontally and then one of actions #1 to #3 is performed.
#6: No change.
#7: None of the above.
Unfortunately, some of the test cases I have inputted are giving me the wrong answers. Will anyone please help me understand what I did wrong in my code?
The first line of any input contains a single integer ‘N’. The next N lines contain a string of N characters that represent the original arrangement of the array. The final N lines each contain a string of N characters that represent the rearrangement of the array.
For example, the input below is supposed to give me ‘5’ →
5
-@@@-
-@@--
-@---
-----
-----
-----
----@
---@@
--@@@
-----
but my current code is giving me ‘3’.
The input below is supposed to give me ‘1’ →
6
-@-@-@
@-@-@-
-@-@-@
@-@-@-
-@-@-@
@-@-@-
@-@-@-
-@-@-@
@-@-@-
-@-@-@
@-@-@-
-@-@-@
but my code is currently giving me 5.
Here is my full code, annotated with some of the comments I have made →
# include <iostream>
using namespace std;
// Strategy ->
// 1. Go over all the possible solutions IN ORDER + stop at the one that corresponds to the current input
// 2. Print out the number of the solution
int main() {
char a[10][10];
char b[10][10];
char input;
int N, output = 7;
cin >> N;
// Take in the inputs for the original grid
for(int i = 0; i < N; i ++) {
for(int j = 0; j < N; j ++) {
cin >> input;
a[i][j] = input;
}
}
// Take in the inputs for the changed grid
for(int i = 0; i < N; i ++) {
for(int j = 0; j < N; j ++) {
cin >> input;
b[i][j] = input;
}
}
// The output variable is originally set to "none of the above" (number 7)
// Go through all of the "if" statements below to see if the grids match any of the other cases
// 1. 90 degrees clockwise
bool trueCase = true;
for(int i = 0; i < N; i ++) {
for(int j = 0; j < N; j ++) {
if(b[i][j] != a[N - j - 1][i]) {
trueCase = false;
}
}
}
if(trueCase) {
output = 1;
}
// 2. 180 degrees clockwise
trueCase = true;
for(int i = 0; i < N; i ++) {
for(int j = 0; j < N; j ++) {
if(b[i][j] != a[N - 1 - i][N - 1 - j]) {
trueCase = false;
}
}
}
if(trueCase) {
output = 2;
}
// 3. 270 degrees clockwise
trueCase = true;
for(int i = 0; i < N; i ++) {
for(int j = 0; j < N; j ++) {
if(b[i][j] != a[N - j - 1][N - i - 1]) {
trueCase = false;
}
}
}
if(trueCase) {
output = 3;
}
// 4. Reflected horizontally
trueCase = true;
for(int i = 0; i < N; i ++) {
for(int j = 0; j < N; j ++) {
if(b[i][j] != a[i][N - j - 1]) {
trueCase = false;
}
}
}
// If there is horizontal reflection + one of the actions #1 to #3 is performed, set "output" to 5
// If there is just a horizontal reflection and nothing more, set "output" to 4
if(trueCase && (output == 1 || output == 2 || output == 3)) {
output = 5;
} else if (trueCase) {
output = 4;
}
// 6. No change
trueCase = true;
for(int i = 0; i < N; i ++) {
for(int j = 0; j < N; j ++) {
if(b[i][j] != a[i][j]) {
trueCase = false;
}
}
}
if(trueCase) {
output = 6;
}
cout << output << endl;
return 0;
}