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_1950_Bridging signals(LIS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1950 题意:实际就是求最长递增子序列 题解:有两种解法,一种是利用二分,一种是用线段树 这个是这题的二 ...
- vb脚本自动更新版本信息
使用的串口显示软件为secureCrt,支持脚本功能,今天写了一个简单的软件升级脚本(VB脚本). 如下: # $language = "VBScript" # $interfac ...
- vs设计界面出现“建控件时出错 响应在此上下文中不可用”
使用VS2010设计Asp.net时出现: 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态.还请确保在应用程序配置的 \\ 节中包括 ...
- 传统 Ajax 已死,Fetch 永生
原谅我做一次标题党,Ajax 不会死,传统 Ajax 指的是 XMLHttpRequest(XHR),未来现在已被 Fetch 替代. 最近把阿里一个千万级 PV 的数据产品全部由 jQuery 的 ...
- 关于iOS socket都在这里了
socket(套接字)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元,包含进行网络通信必须的五种信息:连接使用的协议,本地主机的IP地址,本地进程的协议端口,远地主机的IP地址,远地进程 ...
- vs2010在进行数据架构比较时报'text lines should not be null'错误
通过VS2010进行服务器数据库和本地数据库比较架构(都是sql server 2008 R2)时,弹出“text lines should be not null”错误,如下图: 解决方法:在Vis ...
- awk 用法小结
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...
- POJ 2234 Matches Game 尼姆博弈
题目大意:尼姆博弈,判断是否先手必胜. 题目思路: 尼姆博弈:有n堆各a[]个物品,两个人轮流从某一堆取任意多的物品,规定每次至少取一个,多者不限,最后取光者得胜. 获胜规则:ans=(a[1]^a[ ...
- vps安装wordpress遇到的问题(lnmp)
1.要执行请求的操作,WordPress 需要访问您网页服务器的权限. 请输入您的 FTP 登录XXXX完美解决方法 因为在wordpress中新上传的插件的权限都是www用户的,而不是root或其他 ...
- Android 开发——如何显示 GIF 动画
gif 图动画在 android 中还是比较常用的,比如像新浪微博中,有很多 gif 图片,而且展示非常好,所以我也想弄一个.经过我多方的搜索资料和整理,终于弄出来了,其实 github 上有很多开源 ...