【USACO】Transformations

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

一A的题,好happy。
不难,就是很麻烦。我是把前四个操作写成四个函数。其中旋转90度作为基本的函数,旋转180和旋转270都是由两次和三次旋转90度得到。
主要的问题就是int**和int a[][11]这两种传递参数时候遇到的麻烦,不知道怎么把两者统一起来,所以每次都要先把int**复制到一个数组里面,再作为参数传递给下一个函数,下午去找找资料吧。
  1 /*ID:Moment1991
  2   PROG:transform
  3   LANG:C++
  4  */
  5 #include <iostream>
  6 #include <fstream>
  7 #include <stdlib.h>
  8 using namespace std;
  9 
 10 //旋转90度的操作
 11 int **transiformation_one(int before[][11],int n){
 12     int **tran = new int*[11];
 13     for(int i = 0;i < 11;i++){
 14         tran[i] = new int[11];
 15     }
 16     for(int i = 0;i < n;i++)
 17         for(int j = 0;j < n;j ++){
 18             tran[j][n-i-1] = before[i][j];
 19         }
 20     return tran;
 21 }
 22 
 23 //旋转180由两次旋转90度得到
 24 int **transiformation_two(int before[][11],int n){
 25     int **tran_1 = transiformation_one(before,n);
 26     int temp[11][11];
 27     for(int i = 0;i < n;i++)
 28         for(int j = 0;j < n;j ++)
 29             temp[i][j] = tran_1[i][j];
 30     int **tran_2 = transiformation_one(temp,n);
 31     return tran_2;
 32 }
 33 
 34 //旋转270由三次旋转90度得到
 35 int **transiformation_three(int before[][11],int n){
 36     int **tran_1 = transiformation_one(before,n);
 37     int temp[11][11];
 38     for(int i = 0;i < n;i++)
 39         for(int j = 0;j < n;j ++)
 40             temp[i][j] = tran_1[i][j];
 41 
 42     int **tran_2 = transiformation_one(temp,n);
 43     for(int i = 0;i < n;i++)
 44         for(int j = 0;j < n;j ++)
 45             temp[i][j] = tran_2[i][j];
 46 
 47     int **tran_3 = transiformation_one(temp,n);
 48     return tran_3;
 49 }
 50 
 51 //沿竖直方向翻转
 52 int **transiformation_four(int before[][11],int n){
 53     int **tran = new int*[11];
 54     for(int i = 0;i < 11;i++){
 55         tran[i] = new int[11];
 56     }
 57 
 58     for(int j = 0;j <= n/2;j++){
 59         for(int i = 0;i < n;i ++){
 60             tran[i][n-j-1] = before[i][j];
 61             tran[i][j] = before[i][n-j-1];
 62         }
 63     }
 64     return tran;
 65 }
 66 
 67 //判断两个矩阵是否相等
 68 bool is_equal(int **tran,int after[][11],int n){
 69     for(int i = 0;i < n;i ++)
 70         for(int j = 0;j < n;j ++)
 71             if(tran[i][j] != after[i][j])
 72                 return false;
 73     return true;
 74 }
 75 
 76 //没办法统一int**和inta[][11],只好写两个判断相等函数
 77 bool another_equal(int tran[][11],int after[][11],int n){
 78     for(int i = 0;i < n;i ++)
 79         for(int j = 0;j < n;j ++)
 80             if(tran[i][j] != after[i][j])
 81                 return false;
 82     return true;
 83 }
 84 
 85 int main(){
 86     ifstream cin("transform.in");
 87     ofstream cout("transform.out");
 88 
 89     int n;
 90     int before[11][11];
 91     int after[11][11];
 92 
 93     cin >> n;
 94     for(int i = 0;i < n;i++)
 95         for(int j = 0;j < n;j++)
 96             cin >> before[i][j];
 97 
 98     for(int i = 0;i < n;i++)
 99         for(int j = 0;j < n;j++)
100             cin >> after[i][j];
101 
102     int **tran = transiformation_one(before,n);
103     if(is_equal(tran,after,n))
104     {
105         cout <<1<<endl;
106         free(tran);
107         return 0;
108     }
109 
110     tran = transiformation_two(before,n);
111     if(is_equal(tran,after,n))
112     {
113         cout <<2<<endl;
114         free(tran);
115         return 0;
116     }
117 
118     tran = transiformation_three(before,n);
119     if(is_equal(tran,after,n))
120     {
121         cout <<3<<endl;
122         free(tran);
123         return 0;
124     }
125 
126     tran = transiformation_four(before,n);
127     if(is_equal(tran,after,n))
128     {
129         cout <<4<<endl;
130         free(tran);
131         return 0;
132     }
133 
134     //组合操作,调用多个函数实现
135     tran = transiformation_four(before,n);
136     int temp[11][11];
137     for(int i = 0;i < n;i++)
138         for(int j = 0;j < n;j ++)
139             temp[i][j] = tran[i][j];
140     int **tran_2 = transiformation_one(temp,n);
141     if(is_equal(tran_2,after,n))
142     {
143         cout <<5<<endl;
144         free(tran);
145         free(tran_2);
146         return 0;
147     }
148     else{
149         tran_2 = transiformation_two(temp,n);
150         if(is_equal(tran_2,after,n))
151         {
152             cout <<5<<endl;
153             free(tran);
154             free(tran_2);
155             return 0;
156         }
157     }
158     tran_2 = transiformation_three(temp,n);
159     if(is_equal(tran_2,after,n))
160     {
161         cout <<5<<endl;
162         free(tran);
163         free(tran_2);
164         return 0;
165     }
166 
167     if(another_equal(before,after,n))
168     {
169         cout << 6<<endl;
170         return 0;
171     }
172 
173     cout <<7<<endl;
174     return 0;
175 
176 }

 

posted @ 2014-05-15 11:11  SunshineAtNoon  阅读(180)  评论(0编辑  收藏  举报