P1205 [USACO1.2] 方块转换 Transformations题解

· · 题解

P1205 [USACO1.2] 方块转换 Transformations题解 本题的题干看似需要很多运算,但是仔细分析就发现只需要构建两个函数就可以完成。一个函数是把方形数组顺时针转90度,另外一个就是把方形数组左右镜像。剩下的几种操作就可以用这两个函数来组合使用就可以了。那么问题来了,C++不能返回数组,最多只能返回数组指针。我就想了一个方法,构建一个三维数组,函数形参就是修改前的层数和修改后的层数,采用有参无返函数。经过函数操作以后把目标数组和三维数组里面各个层(二维数组)对比。以下是ac code: //luoguP1205

include <bits/stdc++.h>

using namespace std; int const N = 12; int n; char a[5][12][12],destination[12][12]; void fun1(int yuanlai, int houlai ){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ a[houlai][i][j] = a[yuanlai][n + 1 - j][i]; } } } void fun2(int yuanlai,int houlai){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ a[houlai][i][j] = a[yuanlai][i][n + 1 -j]; } } } bool compare(char x[N][N],char y[N][N]){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(x[i][j] != y[i][j]){ return false; } } } return true; } int main(){ cin>>n; for(int i = 1; i <= n; i++ ){ for(int j = 1; j <= n; j++){ cin>>a[0][i][j]; } } for(int i = 1; i <= n; i++ ){ for(int j = 1; j <= n; j++){ cin>>destination[i][j]; } } fun1(0,1); fun1(1,2); fun1(2,3); fun2(0,4); fun1(4,5); fun1(5,6); fun1(6,7); if(compare(a[1],destination))cout<<1; else if(compare(a[2],destination))cout<<2; else if(compare(a[3],destination))cout<<3;