7-6 1.2.2 方块转换 (70分)

    技术2022-07-11  86

    一块 N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案.

    写一个程序来找出将原始图案按照以下列转换方法转换成新图案的小方式:

    #1:转 90 度:图案按顺时针转 90 度.

    #2:转 180 度:图案按顺时针转 180 度.

    #3:转 270 度:图案按顺时针转 270 度.

    #4:反射:图案在水平方向翻转(形成原图案的镜像).

    #5:组合:图案在水平方向翻转,然后按照#1-#3 之一转换.

    #6:不改变:原图案不改变.

    #7:无效转换:无法用以上方法得到新图案. 如果有多种可用的转换方法,请选择序号小的那个.

    输入格式: 第一行: 单独的一个整数 N.

    第二行到第 N+1 行: N 行每行 N 个字符(不是“@”就是“-”);这是转换前的正方形.

    第 N+2 行到第 2*N+1 行: N 行每行 N 个字符(不是“@”就是“-”);这是转换后的正方形.

    输出格式: 单独的一行包括1到7之间的一个数字(在上文已描述)表明需要将转换前的正方形变为转换后的正方形的转换方法.

    输入样例1: 在这里给出一组输入。例如:

    3 @-@ --- @@- @-@ @-- --@

    输出样例1: 在这里给出相应的输出。例如:

    1

    输入样例2:

    5 -@@@- -@@-- -@--- ----- ----- ----- ----@ ---@@ --@@@ -----

    输出样例2:

    5

    输入样例3:

    5 @@@@@ @---@ @@@@@ @@@@@ @@@@@ @@@@@ @@@@@ @@@@@ @---@ @@@@@

    输出样例3:

    2

    【思路】 #3: 反向使用#1; #4: 各元素的行标不变,列标对称; #5: 创建新的二维数组储存,原图形反射后的图形,记为s3,再用s3与s2比较; #6: 直接判断各元素是否一一对应相等; #7: 前6个均不被调用即输出7;

    题目要求:如果有多种可用的转换方法,请选择序号小的那个 swith语句是个好想法:可以按从小到大的顺序调用转换函数,而且只需要调用一个即可,最后如果前6个均没有被调用,才会轮到#7。


    【源代码】

    #include <iostream> using namespace std; int Trans_fun1(const char s1[][10], const char s2[][10], const int n) // 顺时针旋转90度; { int flag = 1; // 函数成功运行的标志; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s1[i][j] == s2[j][n - i - 1]) { continue; } else { flag = 0; break; } } if (flag == 0) { break; } else { continue; } } return flag; } int Trans_fun2(const char s1[][10], const char s2[][10], const int n) // 顺时针旋转180度 { int flag = 1; // 函数成功运行的标志; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s1[i][j] == s2[n - i - 1][n - j - 1]) { continue; } else { flag = 0; break; } } if (flag == 0) { break; } else { continue; } } return flag; } int Trans_fun3(const char s1[][10], const char s2[][10], const int n) // 顺时针旋转270度 { if (Trans_fun1(s2, s1, n) == 1) //旋转后的图形继续旋转90即可得到原图形,可反向调用“函数1”; { return 1; } else { return 0; } } int Trans_fun4(const char s1[][10], const char s2[][10], const int n) // 反射函数; { int flag = 1; // 函数成功运行的标志; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s1[i][j] == s2[i][n-j-1]) //行标不变,列标对称; { continue; } else { flag = 0; break; } } if (flag == 0) { break; } else { continue; } } return flag; } int Trans_fun5(const char s1[][10], const char s2[][10], const int n) // 组合函数; { char s3[10][10] = {}; // 原图形反射后的图形; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { s3[i][j] = s1[i][n - j - 1]; } } if (Trans_fun1(s3, s2, n) || Trans_fun2(s3, s2, n) || Trans_fun3(s3, s2, n)) { return 1; } else { return 0; } } int Trans_fun6(const char s1[][10], const char s2[][10], const int n) // 不改变函数; { int flag = 1; // 函数成功运行的标志; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s1[i][j] == s2[i][j]) // 直接判断各元素是否相等; { continue; } else { flag = 0; break; } } if (flag == 0) { break; } else { continue; } } return flag; } int main() { int n; cin >> n; char s1[10][10] = {}; // 转换前正方形 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> s1[i][j]; } } char s2[10][10] = {}; // 转换后正方形 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> s2[i][j]; } } for (int x = 1; x <= 7; x++) { int flag = 0; // 标记是否成功调用了转换函数; switch (x) { case 1: if (Trans_fun1(s1, s2, n)) { cout << "1" << endl; flag = 1; }break; case 2: if (Trans_fun2(s1, s2, n)) { cout << "2" << endl; flag = 1; }break; case 3: if (Trans_fun3(s1, s2, n)) { cout << "3" << endl; flag = 1; }break; case 4: if (Trans_fun4(s1, s2, n)) { cout << "4" << endl; flag = 1; }break; case 5: if (Trans_fun5(s1, s2, n)) { cout << "5" << endl; flag = 1; }break; case 6: if (Trans_fun6(s1, s2, n)) { cout << "6" << endl; flag = 1; }break; case 7: cout << "7" << endl; break; } if (flag == 1) // 成功调用转换函数,终止程序,保证调用的序号最小; { break; } else { continue; } } return 0; }
    Processed: 0.011, SQL: 9