Files
python/TangDou/LuoGuBook/P1205.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

206 lines
5.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 15;
char a[N][N];//原始图案
char c[N][N];//目标图案
char t[N][N];//转换后的图案
char t2[N][N];
int n;
//判断两个二维数组是不是一致
bool equal() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (t[i][j] != c[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[i][j];
//读入目标图案
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
cin >> c[i][j];
//1、顺时针转90度
//通过实际例子,找规律,得i,j--->j,n-i+1
/**
1 2 3 7 4 1
4 5 6 --> 8 5 2
7 8 9 9 6 3
以数字1为例,原来的坐标(1,1),现在的坐标(1,3)
以数字2为例,原来的坐标(1,2),现在的坐标(2,3)
以数字3为例,原来的坐标(1,3),现在的坐标(3,3)
成功发现列坐标变成了行坐标。那么行坐标呢?它是怎么变化的呢?
以7,8,9为例,它们原来在不同的列,现在都是第1列,看来后期的
列与行有关,当然也得与n有关,所以猜测试为n-i,结果呢,3-3=0,不对啊,
再加个1试试,就是n-i+1,再验算一下,完全正确!
*/
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[j][n - i + 1] = a[i][j];
//如果通过,输出1,返回
if (equal()) {
cout << 1 << endl;
return 0;
}
//2、顺时针转180度
/**
1 2 3 9 8 7
4 5 6 --> 6 5 4
7 8 9 3 2 1
用纸画出来模拟一下
找规律
1,1--->n,n
1,2--->n,n-1
1,3--->n,n-2
2,1--->n-1,n
2,2--->n-1,n-1
总结 : i,j---> n-i+1,n-j+1
*/
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[n - i + 1][n - j + 1] = a[i][j];
//如果通过,输出2,返回
if (equal()) {
cout << 2 << endl;
return 0;
}
//3、顺时针转270度
//通过实际例子,找规律,得i,j---> n-j+1,i
/**
1 2 3 3 6 9
4 5 6 --> 2 5 8
7 8 9 1 4 7
用纸画出来模拟一下
找规律
1,1--->n,1
1,2--->n-1,1
1,3--->n-2,1
2,1--->n,2
2,2--->n-1,2
总结 : i,j---> n-j+1,i
*/
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[n - j + 1][i] = a[i][j];
//如果通过,输出3,返回
if (equal()) {
cout << 3 << endl;
return 0;
}
//4、图案在水平方向翻转
//通过实际例子,
/**
1 2 3 3 2 1
4 5 6 --> 6 5 4
7 8 9 9 8 7
用纸画出来模拟一下
找规律
1,1--->1,n
1,2--->1,n-1
1,3--->1,n-2
2,1--->2,n
2,2--->2,n-1
总结 : i,j---> i,n-j+1
*/
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[i][n - j + 1] = a[i][j];
//如果通过,输出4,返回
if (equal()) {
cout << 4 << endl;
return 0;
}
//5、组合 图案在水平方向翻转,然后再按照 1 ∼3 之间的一种再次转换。
//***********************************************************************************************
//5.1 水平+顺时针90度
//水平
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[i][n - j + 1] = a[i][j];
//复制出来
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t2[i][j] = t[i][j];
//顺时针90度
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[j][n - i + 1] = t2[i][j];
//如果通过,输出5,返回
if (equal()) {
cout << 5 << endl;
return 0;
}
//***********************************************************************************************
//***********************************************************************************************
//5.2 水平+180度
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[i][n - j + 1] = a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t2[i][j] = t[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[n - i + 1][n - j + 1] = t2[i][j];
//如果通过,输出5,返回
if (equal()) {
cout << 5 << endl;
return 0;
}
//***********************************************************************************************
//***********************************************************************************************
//5.3 水平+270度
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[i][n - j + 1] = a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t2[i][j] = t[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[n - j + 1][i] = t2[i][j];
//如果通过,输出5,返回
if (equal()) {
cout << 5 << endl;
return 0;
}
//***********************************************************************************************
//6、不改变
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
t[i][j] = a[i][j];
//如果通过,输出6,返回
if (equal()) {
cout << 6 << endl;
return 0;
}
//7、无效转换
cout << 7 << endl;
return 0;
}