12:变幻的矩阵

来源:互联网 发布:明特量化大数据笔试题 编辑:程序博客网 时间:2024/05/16 11:41

12:变幻的矩阵



总时间限制: 
1000ms 
内存限制: 
65536kB
描述

有一个N x N(N为奇数,且1 <= N <= 10)的矩阵,矩阵中的元素都是字符。这个矩阵可能会按照如下的几种变幻法则之一进行变幻(只会变幻一次)。 

现在给出一个原始的矩阵,和一个变幻后的矩阵,请编写一个程序,来判定原始矩阵是按照哪一种法则变幻为目标矩阵的。 

1. 按照顺时针方向旋转90度; 
如: 

1 2 3        7 4 14 5 6 变幻为  8 5 27 8 9        9 6 3

2. 按照逆时针方向旋转90度; 
如:
1 2 3        3 6 94 5 6 变幻为  2 5 87 8 9        1 4 7 

3. 中央元素不变(如下例中的 5),其他元素(如下例中的3)与“以中央元素为中心的对应元素”(如下例中的7)互换; 
如:
1 2 3       9 8 74 5 6 变幻为 6 5 47 8 9       3 2 1 

4. 保持原始矩阵,不变幻; 

5. 如果 从原始矩阵 到 目标矩阵 的变幻,不符合任何上述变幻,请输出5

输入
第一行:矩阵每行/列元素的个数 N;
第二行到第N+1行:原始矩阵,共N行,每行N个字符;
第N+2行到第2*N+1行:目标矩阵,共N行,每行N个字符;
输出
只有一行,从原始矩阵 到 目标矩阵 的所采取的 变幻法则的编号。
样例输入
5a b c d ef g h i jk l m n op q r s tu v w x yy x w v ut s r q po n m l kj i h g fe d c b a
样例输出
3
#include <stdio.h>#include <iostream>#include <stack>#include <string.h>#include <queue>#include <cmath>#include <vector>#include <algorithm>#include <map>#include <set>#include <string>using namespace std;typedef long long LL;#define MAX 1001char a[MAX][MAX];char b[MAX][MAX];int n;int fun1() {    char c[MAX][MAX];    for(int i = 1; i <= n; i++) {        int k = 0;        for(int j = 1; j <= n; j++) {            c[i][j] =  a[n - k][i];            k++;        }    }    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            if(b[i][j] != c[i][j]) {                return 0;            }        }    }    return 1;}int fun2() {    int k = 0;    char c[MAX][MAX];    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            c[i][j] =  a[j][n - k];        }        k++;    }    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            if(b[i][j] != c[i][j]) {                return 0;            }        }    }    return 1;}int fun3() {    char c[MAX][MAX];    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            c[i][j] = a[n - i + 1][n - j + 1];        }    }    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            if(b[i][j] != c[i][j]) {                return 0;            }        }    }    return 1;}int fun4(){    char c[MAX][MAX];    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            c[i][j] = a[i][j];        }    }    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            if(b[i][j] != c[i][j]) {                return 0;            }        }    }    return 1;}int main() {    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    cin >> n;    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            cin >> a[i][j];        }    }    for(int i = 1; i <= n; i++) {        for(int j = 1; j <= n; j++) {            cin >> b[i][j];        }    }    if(fun1()){        cout << "1" << endl;    }else if(fun2()){        cout << "2" << endl;    }else if(fun3()){        cout << "3" << endl;    }else if(fun4()){        cout << "4" << endl;    }else {        cout << "5" << endl;    }    return 0;}


原创粉丝点击