您的位置:首页 > 编程语言 > C语言/C++

12:变幻的矩阵

2017-07-29 15:22 417 查看
12:变幻的矩阵

总时间限制: 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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cpp 水题 矩阵翻转