检查图案是否是中心对称的 C++ 代码
假设我们有一个包含“X”和“.”的 3 x 3 矩阵。我们需要检查此图案是否中心对称。(有关中心对称的详细信息 − http://en.wikipedia.org/wiki/Central_symmetry
因此,如果输入如下所示
| X | X | . |
| . | . | . |
| . | X | X |
则输出为 True。
步骤
为解决这个问题,我们将遵循以下步骤:
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;
}输入
{ { 'X', 'X', '.' }, { '.', '.', '.' }, { '.', 'X', 'X' } }输出
1
广告
数据结构
网络连接
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP