檢查圖案是否是中心對稱的 C++ 程式碼


假設我們有一個包含“X”和“.”的 3 x 3 矩陣。我們需要檢查此圖案是否中心對稱。(有關中心對稱的詳細資訊 − http://en.wikipedia.org/wiki/Central_symmetry


因此,如果輸入如下所示

XX.
...
.XX

則輸出為 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

更新於: 30-03-2022

301 瀏覽量

開啟你的 職業生涯

完成課程獲得認證

開始
廣告