USACO Section 1.2 Transformations 解题报告
题目
题目描述
一块 N x N
正方形的黑白瓦片的图案要被转换成新的正方形图案。
写一个程序来找出将原始图案按照以下列转换方法转换成新图案的最小方式:
- 转 90 度:图案按顺时针转 90 度.
- 转 180 度:图案按顺时针转 180 度.
- 转 270 度:图案按顺时针转 270 度.
- 翻转:图案在水平方向翻转(形成原图案的镜像).
- 组合:图案在水平方向翻转,然后按照1~3 之一转换.
- 不改变:原图案不改变.
- 无效转换:无法用以上方法得到新图案.
如果有多种可用的转换方法,请选择序号最小的那个。
数据范围
1 <= N <= 10
样例输入
3
@-@
---
@@-
@-@
@--
--@
样例输出
1
解题思路
这就是一个模拟,我们按照从小到达的序号依次枚举每一种操作,最后哪一种操作成功就输出哪种操作的序号。
关键是要细心,模块化编程,仔细处理好各种操作即可。
总结
在开始写代码的时候我遇到了一个以前没有考虑过的情况,那就是如何用函数返回一个二维字符数组,在百度了很久之后我找到了一种办法,就是下面的第一份解题代码,利用返回一个指向指针数组的指针来实现,这种方法写起来比较麻烦一点,但是程序的可读性还是不错的。
之后我看了官方标程,发现了一个更加巧妙的思路,那就是将图案保存在一个结构体中,我们每次操作直接返回一个结构体类型的变量,这样就避免了直接返回一个二维字符数组,并且这种写法可以使得程序更加的简短。这种思想我写在了下面的解题代码(Type 2)中。
解题代码
/*
ID: yinzong2
PROG: transform
LANG: C++11
*/
#define MARK
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn = 15;
int n;
char plat[maxn][maxn], finalPlat[maxn][maxn];
char **init() {
char **a;
a = new char*[n];
for(int i = 0; i < n; i++) {
a[i] = new char[n];
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
a[i][j] = plat[i][j];
}
}
return a;
}
char **rotate90(char **a, int row, int col) {
char **p;
p = new char*[row];
for(int i = 0; i < row; i++) {
p[i] = new char[col];
}
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
p[j][row-1-i] = a[i][j];
}
p[i][col] = '\0';
}
return p;
}
char **rotate180(char **a, int row, int col) {
char **p;
p = new char*[row];
for(int i = 0; i < row; i++) {
p[i] = new char[col];
}
p = rotate90(a, row, col);
p = rotate90(p, row, col);
return p;
}
char **rotate270(char **a, int row, int col) {
char **p;
p = new char*[row];
for(int i = 0; i < row; i++) {
p[i] = new char[col];
}
p = rotate180(a, row, col);
p = rotate90(p, row, col);
return p;
}
char **reflect(char **a, int row, int col) {
char **p;
p = new char*[row];
for(int i = 0; i < row; i++) {
p[i] = new char[col];
}
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
p[i][col-1-j] = a[i][j];
}
p[i][col] = '\0';
}
return p;
}
bool cmp(char **a) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(a[i][j] != finalPlat[i][j]) {
return false;
}
}
}
return true;
}
bool work(char **a, int row, int col) {
char **p;
p = new char*[row];
for(int i = 0; i < row; i++) {
p[i] = new char[col];
}
p = reflect(a, row, col);
if(cmp( rotate90(p, n, n) )) return true;
if(cmp( rotate180(p, n, n) )) return true;
if(cmp( rotate270(p, n, n) )) return true;
return false;
}
int main() {
#ifdef MARK
freopen("transform.in", "r", stdin);
freopen("transform.out", "w", stdout);
#endif // MARK
while(~scanf("%d", &n)) {
for(int i = 0; i < n; i++) {
scanf("%s", plat[i]);
}
for(int i = 0; i < n; i++) {
scanf("%s", finalPlat[i]);
}
char **a = init();
int ans;
if(cmp( rotate90(a, n, n) )) {
ans = 1;
} else if(cmp( rotate180(a, n, n) )) {
ans = 2;
} else if(cmp( rotate270(a, n, n) )) {
ans = 3;
} else if(cmp( reflect(a, n, n) )) {
ans = 4;
} else if(work(a, n, n)) {
ans = 5;
} else if(cmp(a)) {
ans = 6;
} else {
ans = 7;
}
printf("%d\n", ans);
delete(a);
}
return 0;
}
解题代码(Type 2)
/*
ID: yinzong2
PROG: transform
LANG: C++11
*/
#define MARK
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn = 15;
int n;
struct Plat {
char p[maxn][maxn];
};
Plat plat, finalPlat;
Plat rotate90(Plat a) {
Plat np;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
np.p[j][n-1-i] = a.p[i][j];
}
np.p[i][n] = '\0';
}
return np;
}
Plat reflect(Plat a) {
Plat np;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
np.p[i][n-1-j] = a.p[i][j];
}
np.p[i][n] = '\0';
}
return np;
}
bool cmp(Plat a, Plat b) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(a.p[i][j] != b.p[i][j]) {
return false;
}
}
}
return true;
}
int main() {
#ifdef MARK
freopen("transform.in", "r", stdin);
freopen("transform.out", "w", stdout);
#endif // MARK
while(~scanf("%d", &n)) {
for(int i = 0; i < n; i++) {
scanf("%s", plat.p[i]);
}
for(int i = 0; i < n; i++) {
scanf("%s", finalPlat.p[i]);
}
int ans;
if(cmp(finalPlat, rotate90(plat))) {
ans = 1;
} else if(cmp(finalPlat, rotate90(rotate90(plat)))) {
ans = 2;
} else if(cmp(finalPlat, rotate90(rotate90(rotate90(plat))))) {
ans = 3;
} else if(cmp(finalPlat, reflect(plat))) {
ans = 4;
} else if(cmp(finalPlat, rotate90(reflect(plat)))
||cmp(finalPlat, rotate90(rotate90(reflect(plat))))
||cmp(finalPlat, rotate90(rotate90(rotate90(plat))))) {
ans = 5;
} else if(cmp(finalPlat, plat)) {
ans = 6;
} else {
ans = 7;
}
printf("%d\n", ans);
}
return 0;
}
USACO Section 1.2 Transformations 解题报告的更多相关文章
- USACO Section 1.3 Wormholes 解题报告
题目 题目描述 在一个二维平面上有N个点,这N个点是(N/2)个虫洞的端点,虫洞的特点就是,你以什么状态从某个端点进去,就一定会以什么状态从另一端的端点出来.现在有一头牛总是沿着与X轴正方向平行的直线 ...
- USACO Section1.2 Transformations 解题报告
transform解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...
- USACO Section2.2 Preface Numbering 解题报告 【icedream61】
preface解题报告----------------------------------------------------------------------------------------- ...
- USACO Section2.1 Hamming Codes 解题报告 【icedream61】
hamming解题报告----------------------------------------------------------------------------------------- ...
- USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】
holstein解题报告 --------------------------------------------------------------------------------------- ...
- USACO Section2.1 The Castle 解题报告
castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.5 Prime Palindromes 解题报告
pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section2.3 Controlling Companies 解题报告 【icedream61】
concom解题报告------------------------------------------------------------------------------------------ ...
- USACO Section2.3 Money Systems 解题报告 【icedream61】
money解题报告------------------------------------------------------------------------------------------- ...
随机推荐
- hdu_5753_Permutation Bo(找规律)
题目连接:hdu_5753_Permutation Bo 题意: 给你一个有n个数的数列c1~cn,h1~hn为1~n的排列,求ci[hi>hi-1 and hi>hi+1]的期望和. ...
- 安装lnmp(linux nginx mysql php)
下载或者在云盘里找lnmp1.2-full.tar.gz 用 tar -zxvf lnmp1.2-full.tar.gz解压 进入 ,运行./install.sh安装.根据提示. 如果出现yum锁定, ...
- Java中判断字符串中相同字符的个数
public static int countStr(String str1, String str2) { int counter=0; if (str1.indexOf(str2) == -1) ...
- code.google.com
https://github.com/couchbase/sync_gateway/issues/492 This list shows the current base import paths, ...
- MyBatis 返回新增数据的自增id
<insert id="save" parameterType="Vote" useGeneratedKeys="true" keyP ...
- 【jsp网站计数功能】 application session
在jsp页面中实现网站计数器的方法有很多,其中比较普遍的做法是利用application 和session对象.application对象可被所有用户共享:session是单用户共享,用户从访问系统开 ...
- ios开发使用lipo命令合并真机库和模拟器库
在开发ios时,我们经常会遇到编译两套库文件,使用模拟器时链接模拟器库,使用真机时使用真机库,这样操作会对后期的维护带来麻烦,所以Apple提供了一个把多个不同平台的.a库文件合并成一个适用于多平台的 ...
- TexturePacker license Key免费获取方式
TexturePacker是一款功能非常强大的图片制作工具.是一款付费软件,但是TexturePacker的作者Andreas Löw先生也给出获得免费key 的方法...大家可以到这个网站去申请 h ...
- [Python]网络爬虫(四):Opener与Handler的介绍和实例应用
在开始后面的内容之前,先来解释一下urllib2中的两个个方法:info and geturl urlopen返回的应答对象response(或者HTTPError实例)有两个很有用的方法info() ...
- download下载excel模板的代码
<%-- 直接在JSP页面中进行文件下载的代码(改 Servlet 或者 JavaBean 的话自己改吧), 支持中文附件名(做了转内码处理). 事实上只要向 out 输出字节就被认为是附件内容 ...