【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**复制到一个数组里面,再作为参数传递给下一个函数,下午去找找资料吧。
/*ID:Moment1991
PROG:transform
LANG:C++
Compiling...
Compile: OK Executing...
Test 1: TEST OK [0.000 secs, 3496 KB]
Test 2: TEST OK [0.003 secs, 3496 KB]
Test 3: TEST OK [0.008 secs, 3496 KB]
Test 4: TEST OK [0.008 secs, 3496 KB]
Test 5: TEST OK [0.005 secs, 3496 KB]
Test 6: TEST OK [0.003 secs, 3496 KB]
Test 7: TEST OK [0.005 secs, 3496 KB]
Test 8: TEST OK [0.005 secs, 3496 KB] All tests OK.
*/
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std; //旋转90度的操作
int **transiformation_one(int before[][],int n){
int **tran = new int*[];
for(int i = ;i < ;i++){
tran[i] = new int[];
}
for(int i = ;i < n;i++)
for(int j = ;j < n;j ++){
tran[j][n-i-] = before[i][j];
}
return tran;
} //旋转180由两次旋转90度得到
int **transiformation_two(int before[][],int n){
int **tran_1 = transiformation_one(before,n);
int temp[][];
for(int i = ;i < n;i++)
for(int j = ;j < n;j ++)
temp[i][j] = tran_1[i][j];
int **tran_2 = transiformation_one(temp,n);
return tran_2;
} //旋转270由三次旋转90度得到
int **transiformation_three(int before[][],int n){
int **tran_1 = transiformation_one(before,n);
int temp[][];
for(int i = ;i < n;i++)
for(int j = ;j < n;j ++)
temp[i][j] = tran_1[i][j]; int **tran_2 = transiformation_one(temp,n);
for(int i = ;i < n;i++)
for(int j = ;j < n;j ++)
temp[i][j] = tran_2[i][j]; int **tran_3 = transiformation_one(temp,n);
return tran_3;
} //沿竖直方向翻转
int **transiformation_four(int before[][],int n){
int **tran = new int*[];
for(int i = ;i < ;i++){
tran[i] = new int[];
} for(int j = ;j <= n/;j++){
for(int i = ;i < n;i ++){
tran[i][n-j-] = before[i][j];
tran[i][j] = before[i][n-j-];
}
}
return tran;
} //判断两个矩阵是否相等
bool is_equal(int **tran,int after[][],int n){
for(int i = ;i < n;i ++)
for(int j = ;j < n;j ++)
if(tran[i][j] != after[i][j]){
return false;
}
return true;
} //没办法统一int**和inta[][11],只好写两个判断相等函数
bool another_equal(int tran[][],int after[][],int n){
for(int i = ;i < n;i ++)
for(int j = ;j < n;j ++)
if(tran[i][j] != after[i][j])
return false;
return true;
} int main(){
ifstream cin("transform.in");
ofstream cout("transform.out"); int n;
char a;
int before[][];
int after[][]; cin >> n;
for(int i = ;i < n;i++)
for(int j = ;j < n;j++){
cin >> a;
if(a == '@')
before[i][j] = ;
else
before[i][j] = ;
} for(int i = ;i < n;i++)
for(int j = ;j < n;j++)
{
cin >> a;
if(a == '@')
after[i][j] = ;
else
after[i][j] = ;
} int **tran = transiformation_one(before,n);
if(is_equal(tran,after,n))
{
cout <<<<endl;
free(tran);
return ;
} tran = transiformation_two(before,n);
if(is_equal(tran,after,n))
{
cout <<<<endl;
free(tran);
return ;
} tran = transiformation_three(before,n);
if(is_equal(tran,after,n))
{
cout <<<<endl;
free(tran);
return ;
} tran = transiformation_four(before,n);
if(is_equal(tran,after,n))
{
cout <<<<endl;
free(tran);
return ;
} //组合操作,调用多个函数实现
tran = transiformation_four(before,n); int temp[][];
for(int i = ;i < n;i++)
for(int j = ;j < n;j ++)
temp[i][j] = tran[i][j];
int **tran_2 = transiformation_one(temp,n); if(is_equal(tran_2,after,n))
{
cout <<<<endl;
free(tran);
free(tran_2);
return ;
}
else{
tran_2 = transiformation_two(temp,n);
if(is_equal(tran_2,after,n))
{
cout <<<<endl;
free(tran);
free(tran_2);
return ;
}
}
tran_2 = transiformation_three(temp,n);
if(is_equal(tran_2,after,n))
{
cout <<<<endl;
free(tran);
free(tran_2);
return ;
} if(another_equal(before,after,n))
{
cout << <<endl;
return ;
} cout <<<<endl;
return ; }
【USACO】Transformations的更多相关文章
- 【USACO】Transformations(模拟)
Transformations A square pattern of size N x N (1 <= N <= 10) black and white square tiles is ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- 1642: 【USACO】Payback(还债)
1642: [USACO]Payback(还债) 时间限制: 1 Sec 内存限制: 64 MB 提交: 190 解决: 95 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 &quo ...
- 1519: 【USACO】超级书架
1519: [USACO]超级书架 时间限制: 1 Sec 内存限制: 64 MB 提交: 1735 解决: 891 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 Farmer Jo ...
- Java实现【USACO】1.1.2 贪婪的礼物送礼者 Greedy Gift Givers
[USACO]1.1.2 贪婪的礼物送礼者 Greedy Gift Givers 题目描述 对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for th ...
- 【CPLUSOJ】【USACO】【差分约束】排队(layout)
[题目描述] Robin喜欢将他的奶牛们排成一队.假设他有N头奶牛,编号为1至N.这些奶牛按照编号大小排列,并且由于它们都很想早点吃饭,于是就很可能出现多头奶牛挤在同一位置的情况(也就是说,如果我们认 ...
- 【USACO】Dining
[题目链接] [JZXX]点击打开链接 [caioj]点击打开链接 [算法] 拆点+网络流 [代码] #include<bits/stdc++.h> using namespace std ...
- 【USACO】Optimal Milking
题目链接 : [POJ]点击打开链接 [caioj]点击打开链接 算法 : 1:跑一遍弗洛伊德,求出点与点之间的最短路径 2:二分答案,二分”最大值最小“ 3.1:建边,将 ...
- 【USACO】 Balanced Photo
[题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...
随机推荐
- 增强学习————K-摇臂赌博机
探索与利用增强学习任务的最终奖赏是在多步动作之后才能观察到,于是我们先考虑最简单的情形:最大化单步奖赏,即仅考虑一步操作.不过,就算这样,强化学习仍与监督学习有显著不同,因为机器要通过尝试来发现各个动 ...
- Samy XSS Worm之源码讲解
说到Web安全和XSS跨站脚本技术,几乎所有的书都会提到Samy Worm,这是在2005年感染了mySpace社交网络上百万用户的蠕虫.正如Morris蠕虫是互联网第一个蠕虫, Samy Worm则 ...
- 《深入浅出JavaScript》
第一章JS入门 第二章数据和判定常用的转义序列\b 回退 \f换页 \n换行 \r回车 \t制表符 \'单引 \"双引 \\反斜乘除求余的优先级相同,从左向右执行string对象indexO ...
- PE文件结构
PE头 typedef struct _IMAGE_NT_HEADERS { DWORD Signature; PE头标识 为固定的ascii码 PE\\ IMAGE_FILE_HEADER File ...
- spark分片个数的确定及Spark内存错误(GC error)的迂回解决方式
我们知道,spark中每个分片都代表着一部分数据,那么分片数量如何被确认的呢? 首先我们使用最常见的HDFS+Spark,sparkDeploy的方式来讨论,spark读取HDFS数据使用的是spar ...
- 【leetcode】Contains Duplicate & Rectangle Area(easy)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- UITextField输入中文限制
[self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEve ...
- Python之socketserver源码分析
一.socketserver简介 socketserver是一个创建服务器的框架,封装了许多功能用来处理来自客户端的请求,简化了自己写服务端代码.比如说对于基本的套接字服务器(socket-based ...
- poj 3635(bfs+优先队列)
题目链接:http://poj.org/problem?id=3635 思路:本题主要运用的还是贪心思想,由于要求st->ed的最小花费,那么每经过一个城市,能不加油就尽量不加油,用dp[i][ ...
- 妙味课堂——HTML+CSS(第四课)(一)
这一课学的东西真是太多了,还不赶快记下来,留待以后慢慢回味! 首先我们回顾一下inline-block的特性: 使块元素在一行显示 使内嵌支持宽高 换行被解析了(问题) 不设置宽度的时候,宽度由内容撑 ...