【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
@[email protected]
---
@@-
@[email protected]
@--
[email protected]

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 Compiling...
5 Compile: OK
6
7 Executing...
8 Test 1: TEST OK [0.000 secs, 3496 KB]
9 Test 2: TEST OK [0.003 secs, 3496 KB]
10 Test 3: TEST OK [0.008 secs, 3496 KB]
11 Test 4: TEST OK [0.008 secs, 3496 KB]
12 Test 5: TEST OK [0.005 secs, 3496 KB]
13 Test 6: TEST OK [0.003 secs, 3496 KB]
14 Test 7: TEST OK [0.005 secs, 3496 KB]
15 Test 8: TEST OK [0.005 secs, 3496 KB]
16
17 All tests OK.
18 */
19 #include <iostream>
20 #include <fstream>
21 #include <stdlib.h>
22 using namespace std;
23
24 //旋转90度的操作
25 int **transiformation_one(int before[][11],int n){
26 int **tran = new int*[11];
27 for(int i = 0;i < 11;i++){
28 tran[i] = new int[11];
29 }
30 for(int i = 0;i < n;i++)
31 for(int j = 0;j < n;j ++){
32 tran[j][n-i-1] = before[i][j];
33 }
34 return tran;
35 }
36
37 //旋转180由两次旋转90度得到
38 int **transiformation_two(int before[][11],int n){
39 int **tran_1 = transiformation_one(before,n);
40 int temp[11][11];
41 for(int i = 0;i < n;i++)
42 for(int j = 0;j < n;j ++)
43 temp[i][j] = tran_1[i][j];
44 int **tran_2 = transiformation_one(temp,n);
45 return tran_2;
46 }
47
48 //旋转270由三次旋转90度得到
49 int **transiformation_three(int before[][11],int n){
50 int **tran_1 = transiformation_one(before,n);
51 int temp[11][11];
52 for(int i = 0;i < n;i++)
53 for(int j = 0;j < n;j ++)
54 temp[i][j] = tran_1[i][j];
55
56 int **tran_2 = transiformation_one(temp,n);
57 for(int i = 0;i < n;i++)
58 for(int j = 0;j < n;j ++)
59 temp[i][j] = tran_2[i][j];
60
61 int **tran_3 = transiformation_one(temp,n);
62 return tran_3;
63 }
64
65 //沿竖直方向翻转
66 int **transiformation_four(int before[][11],int n){
67 int **tran = new int*[11];
68 for(int i = 0;i < 11;i++){
69 tran[i] = new int[11];
70 }
71
72 for(int j = 0;j <= n/2;j++){
73 for(int i = 0;i < n;i ++){
74 tran[i][n-j-1] = before[i][j];
75 tran[i][j] = before[i][n-j-1];
76 }
77 }
78 return tran;
79 }
80
81 //判断两个矩阵是否相等
82 bool is_equal(int **tran,int after[][11],int n){
83 for(int i = 0;i < n;i ++)
84 for(int j = 0;j < n;j ++)
85 if(tran[i][j] != after[i][j]){
86 return false;
87 }
88 return true;
89 }
90
91 //没办法统一int**和inta[][11],只好写两个判断相等函数
92 bool another_equal(int tran[][11],int after[][11],int n){
93 for(int i = 0;i < n;i ++)
94 for(int j = 0;j < n;j ++)
95 if(tran[i][j] != after[i][j])
96 return false;
97 return true;
98 }
99
100 int main(){
101 ifstream cin("transform.in");
102 ofstream cout("transform.out");
103
104 int n;
105 char a;
106 int before[11][11];
107 int after[11][11];
108
109 cin >> n;
110 for(int i = 0;i < n;i++)
111 for(int j = 0;j < n;j++){
112 cin >> a;
113 if(a == ‘@‘)
114 before[i][j] = 0;
115 else
116 before[i][j] = 1;
117 }
118
119 for(int i = 0;i < n;i++)
120 for(int j = 0;j < n;j++)
121 {
122 cin >> a;
123 if(a == ‘@‘)
124 after[i][j] = 0;
125 else
126 after[i][j] = 1;
127 }
128
129 int **tran = transiformation_one(before,n);
130 if(is_equal(tran,after,n))
131 {
132 cout <<1<<endl;
133 free(tran);
134 return 0;
135 }
136
137 tran = transiformation_two(before,n);
138 if(is_equal(tran,after,n))
139 {
140 cout <<2<<endl;
141 free(tran);
142 return 0;
143 }
144
145 tran = transiformation_three(before,n);
146 if(is_equal(tran,after,n))
147 {
148 cout <<3<<endl;
149 free(tran);
150 return 0;
151 }
152
153 tran = transiformation_four(before,n);
154 if(is_equal(tran,after,n))
155 {
156 cout <<4<<endl;
157 free(tran);
158 return 0;
159 }
160
161 //组合操作,调用多个函数实现
162 tran = transiformation_four(before,n);
163
164 int temp[11][11];
165 for(int i = 0;i < n;i++)
166 for(int j = 0;j < n;j ++)
167 temp[i][j] = tran[i][j];
168 int **tran_2 = transiformation_one(temp,n);
169
170 if(is_equal(tran_2,after,n))
171 {
172 cout <<5<<endl;
173 free(tran);
174 free(tran_2);
175 return 0;
176 }
177 else{
178 tran_2 = transiformation_two(temp,n);
179 if(is_equal(tran_2,after,n))
180 {
181 cout <<5<<endl;
182 free(tran);
183 free(tran_2);
184 return 0;
185 }
186 }
187 tran_2 = transiformation_three(temp,n);
188 if(is_equal(tran_2,after,n))
189 {
190 cout <<5<<endl;
191 free(tran);
192 free(tran_2);
193 return 0;
194 }
195
196 if(another_equal(before,after,n))
197 {
198 cout << 6<<endl;
199 return 0;
200 }
201
202 cout <<7<<endl;
203 return 0;
204
205 }

【USACO】Transformations,布布扣,bubuko.com

时间: 2024-07-31 14:34:36

【USACO】Transformations的相关文章

【USACO】Transformations(模拟)

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

【USACO】checker

一看题目 经典的8皇后问题 不过是皇后数量可变而已 不用想 回溯法. 需要个生成每次可选择序列的函数, 在存储可选择的序列时按照先大后小的顺序排的.这样每次找最小和去掉最小都很方便,只要有个记录数量的变量 每次减1就好了.  写完后,居然悲剧了. 在皇后数量达到13时, 在自己电脑上跑 内存溢出了 在评分系统上超时了.需要优化. #include <stdio.h> //k计算第几层从0开始 x已经摆好的位置 S存放产生的位置 l存放产生的数量 N一共有多少位置可以选择 int calcula

【USACO】milk3

倒牛奶的问题, 开始看感觉跟倒水的问题很像, 想直接找规律, 写个类似于循环取余的代码. 但后来发现不行,因为这道题有三个桶,水量也是有限制的.只好用模拟的方法把所有的情况都试一遍. 建一个state[21][21][21]的数组存储出现过的状态.对于遍历状态,对每一种状态, 分别采用六种处理方法,若有新状态出现这将新状态置为1,同时标记flag++:若所有循环之后,flag == 0, 就说明遍历完成了. 开始脑子抽筋了, 写了个多出口的程序, 显然是错的.如下: int mothersmil

【USACO】calfflac

关键:以回文中心位置为变量进行遍历 //必须把纯字母先提出来 否则肯能会出现错误 比如: lvlv= 在检查长度4时 lvlv认为不是回文 vlv=认为是回文 但实际上 lvl 出现的要更早一些 //判断回文的方法 可以输入字符串后 左右比较 或者分别正序 逆序 存储 判断是否相等 //我的思路不对 严重超时了 我是以长度为变量进行循环 对于每个长度 每一个不同起始点的序列都需要对 整个序列重新判断一次是否为回文 O(n^3) //答案中 以中心字母为变量进行循环 只需要对每一个字母做为中心变量

【USACO】第一章总结

做了大半个月,终于把第一章做完了 有的题遇到了不小的坎儿,看着网上一群高中生都做得那么好,心理还是有些小郁闷的.不禁感慨我过去的四年真是虚度啊.总结一下第一章学习到的知识吧. ①闰年判断 int isleapyear(int N) { if((N%100 != 0 && N%4 ==0) || (N%400 == 0)) return 1; else return 0; } 计算闰年日期时可以用两个数组存放每个月的时间 int Year[12] = {31, 28, 31, 30, 31,

【USACO】奶牛跑步2

P1443 - [USACO]奶牛跑步2 Description FJ的N(1 <= N <= 100,000)头奶牛们又兴高采烈地出来运动了!她们在一条无限长的小路上跑步,每头牛起跑的位置都不同,速度也不尽相同. 道路中划出了若干条跑道,以便她们能快速"超车",同一跑道中的任意两头牛都不会出现在相同的位置.不过FJ不愿让任何一头牛更换跑道或者调整速度,他想 知道如果让牛们跑足T(1 <= T <= 1,000,000,000)分钟的话,至少需要多少条跑道才能满

【USACO】beads

题目: You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29: 1 2 1 2 r b b r b r r b r b b b r r b r r r w r b r w w b b r r b b b b b b

【USACO】ariprog

输入 : N  M 要找到长度为 N 的等差数列,要求数列中每个数字都可以表达成 a^2 + b^2 的和, 数字大小不超过M^2 + M^2 输出: 等差数列首元素 间隔 (多组答案分行输出) 解题思路:因为等差数列的数字都是平房和数  所以先生成所有的 从0 - M^2 + M^2的平方和数 去掉相同的并从小到大排序 然后对 所有间隔 . 首元素 做循环 判断能否找到以该首元素和间隔为条件的其他N-1个需要的数字 可以就存成答案: 提交后超时了.... test 5的时候 超了5s 正在想简

【USACO】Mother&#39;s Milk(搜索)

一开始还在想去重的问题,结果发现后台数据貌似没有重复的情况= = /* ID: 18906421 LANG: C++ PROG: milk3 */ #include<cmath> #include<cstdio> #include<vector> #include<algorithm> using namespace std; const int maxn = 25; int vis[maxn][maxn][maxn] = {0}; vector<in