12:变幻的矩阵
2017-07-29 15:22
417 查看
12:变幻的矩阵
总时间限制: 1000ms 内存限制: 65536kB
描述
有一个N x N(N为奇数,且1 <= N <= 10)的矩阵,矩阵中的元素都是字符。这个矩阵可能会按照如下的几种变幻法则之一进行变幻(只会变幻一次)。
现在给出一个原始的矩阵,和一个变幻后的矩阵,请编写一个程序,来判定原始矩阵是按照哪一种法则变幻为目标矩阵的。
1. 按照顺时针方向旋转90度;
如:
2. 按照逆时针方向旋转90度;
如:
3. 中央元素不变(如下例中的 5),其他元素(如下例中的3)与“以中央元素为中心的对应元素”(如下例中的7)互换;
如:
4. 保持原始矩阵,不变幻;
5. 如果 从原始矩阵 到 目标矩阵 的变幻,不符合任何上述变幻,请输出5
输入第一行:矩阵每行/列元素的个数 N;
第二行到第N+1行:原始矩阵,共N行,每行N个字符;
第N+2行到第2*N+1行:目标矩阵,共N行,每行N个字符;
输出只有一行,从原始矩阵 到 目标矩阵 的所采取的 变幻法则的编号。
样例输入
样例输出
总时间限制: 1000ms 内存限制: 65536kB
描述
有一个N x N(N为奇数,且1 <= N <= 10)的矩阵,矩阵中的元素都是字符。这个矩阵可能会按照如下的几种变幻法则之一进行变幻(只会变幻一次)。
现在给出一个原始的矩阵,和一个变幻后的矩阵,请编写一个程序,来判定原始矩阵是按照哪一种法则变幻为目标矩阵的。
1. 按照顺时针方向旋转90度;
如:
1 2 3 7 4 1 4 5 6 变幻为 8 5 2 7 8 9 9 6 3
2. 按照逆时针方向旋转90度;
如:
1 2 3 3 6 9 4 5 6 变幻为 2 5 8 7 8 9 1 4 7
3. 中央元素不变(如下例中的 5),其他元素(如下例中的3)与“以中央元素为中心的对应元素”(如下例中的7)互换;
如:
1 2 3 9 8 7 4 5 6 变幻为 6 5 4 7 8 9 3 2 1
4. 保持原始矩阵,不变幻;
5. 如果 从原始矩阵 到 目标矩阵 的变幻,不符合任何上述变幻,请输出5
输入第一行:矩阵每行/列元素的个数 N;
第二行到第N+1行:原始矩阵,共N行,每行N个字符;
第N+2行到第2*N+1行:目标矩阵,共N行,每行N个字符;
输出只有一行,从原始矩阵 到 目标矩阵 的所采取的 变幻法则的编号。
样例输入
5 a b c d e f g h i j k l m n o p q r s t u v w x y y x w v u t s r q p o n m l k j i h g f e 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 1001 char 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; }
相关文章推荐
- 12:变幻的矩阵
- OpenJudge noi 12变幻的矩阵
- 12_变幻的矩阵
- 12:变幻的矩阵
- 12:变幻的矩阵( 1.8编程基础之多维数组)
- 【剑指offer】面试题12:矩阵中的路径
- 《VR入门系列教程》之12---转换矩阵
- 12.高斯消去法(1)——矩阵编程基础
- day-12,多层感知机-矩阵计算 <C版>
- Unity手游之路<四>3d旋转-四元数,欧拉角和变幻矩阵
- 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
- 《剑指offer》面试题12:矩阵中的路径
- [原]Unity手游之路<四>3d旋转-四元数,欧拉角和变幻矩阵
- 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8
- [原]Unity手游之路 四 3d旋转-四元数,欧拉角和变幻矩阵
- 12 蛇形打印矩阵中的数据
- 变幻的矩阵
- 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8
- opengl 教程(12) 投影矩阵
- Unity手游之路<四>3d旋转-四元数,欧拉角和变幻矩阵