检查图案是否是中心对称的 C++ 代码
假设我们有一个包含“X”和“.”的 3 x 3 矩阵。我们需要检查此图案是否中心对称。(有关中心对称的详细信息 − http://en.wikipedia.org/wiki/Central_symmetry
X | X | . |
. | . | . |
. | X | X |
步骤
if M[0, 0] is same as M[2, 2] and M[0, 1] is same as M[2, 1] and M[0, 2] is same as M[2, 0] and M[1, 0] is same as M[1, 2], then: return true Otherwise return false
示例
#include <bits/stdc++.h> using namespace std; bool solve(vector<vector<char>> M){ if (M[0][0] == M[2][2] && M[0][1] == M[2][1] && M[0][2] == M[2][0] && M[1][0] == M[1][2]) return true; else return false; } int main(){ vector<vector<char>> matrix = { { 'X', 'X', '.' }, { '.', '.', '.' }, { '.', 'X', 'X' } }; cout << solve(matrix) << endl; }
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输入
{ { 'X', 'X', '.' }, { '.', '.', '.' }, { '.', 'X', 'X' } }
输出
1
广告