Algorithm, Data structure/Solved Algorithmic Problem

USACO 1.2 - Transformations

JaykayChoi 2016. 6. 6. 16:59

A square pattern of size N x N (1 <= N <= 10) black and white square tiles is transformed into another square pattern. Write a program that will recognize the minimum transformation that has been applied to the original pattern given the following list of possible transformations:

  • #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.
  • #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.
  • #3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.
  • #4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).
  • #5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3).
  • #6: No Change: The original pattern was not changed.
  • #7: Invalid Transformation: The new pattern was not obtained by any of the above methods.

In the case that more than one transform could have been used, choose the one with the minimum number above.

PROGRAM NAME: transform

INPUT FORMAT

Line 1:A single integer, N
Line 2..N+1:N lines of N characters (each either `@' or `-'); this is the square before transformation
Line N+2..2*N+1:N lines of N characters (each either `@' or `-'); this is the square after transformation

SAMPLE INPUT (file transform.in)

3
@-@
---
@@-
@-@
@--
--@

OUTPUT FORMAT

A single line containing the the number from 1 through 7 (described above) that categorizes the transformation required to change from the `before' representation to the `after' representation.

SAMPLE OUTPUT (file transform.out)

1


출처: http://train.usaco.org/



n*n의 배열이 두 개 주어지는데 7가지 변형 방법이 있다고 할 때 해당되는 방법의 수를 구하는 문제입니다. 답이 여러 개일 경우 작은 숫자를 출력하면 됩니다.

동적 2차 배열을 더블 포인터로 표현하였고 직접 완전 탐색으로 회전 또는 반전 시키는 방법을 사용해 풀었습니다.




my solving

c++

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <fstream>
#include <iostream>
using namespace std;
 
char** createMatrix(int row, int column)
{
    char **matrix = new char*[row];
 
    for (int i = 0; i < row; i++
        matrix[i] = new char[column];
    
    return matrix;
}
 
void deleteMatrix(char** matrix, int row)
{
    for (int i = 0; i < row; i++)
        delete[] matrix[i];
    
    delete[]matrix;
}
 
 
void rotateMatrix90Degree(char **matrix, int N)
{
    for (int i = 0; i < N / 2; i++)
    {
        for (int j = i; j < N - i - 1; j++)
        {
            char temp = matrix[i][j];
            matrix[i][j] = matrix[N - - j][i];
            matrix[N - - j][i] = matrix[N - - i][N - - j];
            matrix[N - - i][N - - j] = matrix[j][N - - i];
            matrix[j][N - - i] = temp;
        }
    }
}
 
void reflectMatrix(char **matrix, int row, int column)
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column / 2; j++)
        {
            char temp = matrix[i][j];
            matrix[i][j] = matrix[i][column - - j];
            matrix[i][column - - j] = temp;
        }
    }
}
 
bool isSame(char **a, char **b, int row, int column)
{
    bool ok = true;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)
        {
            if (a[i][j] != b[i][j])
            {
                ok = false;
                break;
            }
        }
        if (ok == false)
            break;
    }
    return ok;
}
 
int main() 
{
    ofstream fout("transform.out");
    ifstream fin("transform.in");
 
    int N;
 
    fin >> N;
 
    char **before = createMatrix(N, N);
    char **after = createMatrix(N, N);
 
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            fin >> before[i][j];
        }
    }
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            fin >> after[i][j];
        }
    }
 
    while (true)
    {
        rotateMatrix90Degree(before, N);
        if (isSame(before, after, N, N))
        {
            fout << << endl;
            break;
        }
 
        rotateMatrix90Degree(before, N);
        if (isSame(before, after, N, N))
        {
            fout << << endl;
            break;
        }
 
        rotateMatrix90Degree(before, N);
        if (isSame(before, after, N, N))
        {
            fout << << endl;
            break;
        }
 
        rotateMatrix90Degree(before, N);
        if (isSame(before, after, N, N))
        {
            fout << << endl;
            break;
        }
 
        reflectMatrix(before, N, N);
        if (isSame(before, after, N, N))
        {
            fout << << endl;
            break;
        }
        
        rotateMatrix90Degree(before, N);
        if (isSame(before, after, N, N))
        {
            fout << << endl;
            break;
        }
 
        rotateMatrix90Degree(before, N);
        if (isSame(before, after, N, N))
        {
            fout << << endl;
            break;
        }
 
        rotateMatrix90Degree(before, N);
        if (isSame(before, after, N, N))
        {
            fout << << endl;
            break;
        }
 
        fout << << endl;
        break;
    }
 
    deleteMatrix(before, N);
    deleteMatrix(after, N);
 
    return 0;
}
cs