回転と平行移動後に2つの画像が一致するかどうかを確認するC++プログラム
最初と2番目に2つのn*nピクセルの正方形の画像があるとします。ピクセルは黒または白のいずれかです。画像はマトリックス表現で提供され、ピクセルが黒の場合は「x」として表され、白の場合は「。」として表されます。 90°の回転と平行移動の後、2番目の画像が最初の画像と一致することを確認する必要があります。そうである場合はtrueを返し、そうでない場合はfalseを返します。
したがって、入力がn =4の場合、first ={"..x。"、 "x.x。"、 "x.xx"、 "xx .."}、second ={".. xx"、"x。 xx "、" .x.x "、"..x。"}の場合、出力はFalseになります。
これを解決するために、次の手順に従います-
Define a function find(), this will take an array of pairs x, an array of pairs y,
d1 := first value of y[0] - first value of x[0]
d2 := second value of y[1] - second value of x[1]
for initialize i := 1, when i < size of x, update (increase i by 1), do:
if first value of y[i] - first value of x[i] is not equal to d1 or second value of y[i] - second value of x[i] is not equal to d2, then:
return false
return true
Define a function rotate(), this will take n, an array of pairs a, an array of pairs b,
for initialize i := 0, when i < size of b, update (increase i by 1), do:
b[i] := make_pair(second value of b[i], n - first value of b[i] - 1)
Define two arrays a, b that can contain integer pairs
for initialize i := 0, when i < n, update (increase i by 1), do:
s := first[i]
for initialize j := 0, when j < n, update (increase j by 1), do:
if s[j] is same as 'x', then:
insert pair(i, j) at the end of a
for initialize i := 0, when i < n, update (increase i by 1), do:
s := second[i]
for initialize j := 0, when j < n, update (increase j by 1), do:
if s[j] is same as 'x', then:
insert pair(i, j) at the end of b
if size of a is not equal to size of b, then:
return false
if size of a is same as 0, then:
return true
check := false
sort the array a
for initialize i := 0, when i < 4, update (increase i by 1), do:
sort the array b
if find(a, b), then:
check := true
rotate(n, a, b)
if check is true, then:
return true
Otherwise
return false 例
理解を深めるために、次の実装を見てみましょう-
#include <bits/stdc++.h>
using namespace std;
bool find(vector<pair<int, int>> x, vector<pair<int, int>> y){
int d1 = y[0].first - x[0].first;
int d2 = y[1].second - x[1].second;
for(int i = 1; i < x.size(); i++){
if(y[i].first - x[i].first != d1 || y[i].second - x[i].second != d2){
return false;
}
}
return true;
}
void rotate(int n, vector<pair<int, int>> a, vector<pair<int, int>> b){
for(int i = 0; i < b.size(); i++){
b[i] = make_pair(b[i].second, n - b[i].first - 1);
}
}
bool solve(int n, vector<string> first, vector<string> second){
vector<pair<int, int>> a, b;
for(int i = 0; i < n; i++){
string s = first[i];
for(int j = 0; j < n; j++){
if(s[j] == 'x'){
a.push_back(make_pair(i, j));
}
}
}
for(int i = 0; i < n; i++){
string s = second[i];
for(int j = 0; j < n; j++){
if(s[j] == 'x'){ b.push_back(make_pair(i,j));
}
}
}
if(a.size() != b.size()){
return false;
}
if(a.size() == 0){
return true;
}
bool check = false;
sort(a.begin(),a.end());
for(int i = 0; i < 4; i++){
sort(b.begin(),b.end());
if(find(a,b)){
check = true;
}
rotate(n, a, b);
}
if(check){
return true;
}else{
return false;
}
}
int main() {
int n = 4; vector<string> first = {"..x.", "x.x.", "x.xx", "xx.."}, second = {"..xx", "x.xx", ".x.x", "..x."};
cout<< solve(n, first, second);
return 0;
} 入力
4, {"..x.", "x.x.", "x.xx", "xx.."}, {"..xx", "x.xx", ".x.x", "..x."}
出力
0
-
C++で対合行列をチェックするプログラム
行列M[r][c]が与えられた場合、「r」は行数を示し、「c」はr=cが正方行列を形成するような列数を示します。与えられた正方行列が対合行列であるかどうかを確認する必要があります かどうか。 対合行列 行列は非自発的と呼ばれます 行列がそれ自体と乗算され、その結果が単位行列である場合に限り、行列。行列Iは、その主対角線が1であり、主対角線以外の要素がゼロである場合にのみ、単位行列です。したがって、行列は対合行列であると言えます。 M * M =Iの場合のみ 、ここで M はいくつかの行列であり、私は単位行列です。 以下の例のように- ここで、行列にそれ自体を乗算すると、結果は単
-
C++で対角行列とスカラー行列をチェックするプログラム
行列M[r][c]が与えられた場合、「r」は行数を示し、「c」はr=cが正方行列を形成するような列数を示します。与えられた正方行列が対角であるかどうかを確認する必要があります およびスカラー 対角の場合、行列かどうか およびスカラー マトリックスを作成し、結果にyesを出力します。 対角行列 正方行列m[][]は、主対角を除く要素がゼロの場合にのみ対角行列になります。 下の図のように- ここで、赤の要素は主対角線であり、主対角線がゼロであることを除いてゼロ以外の残りの要素であり、対角行列になっています。 。 例 Input: m[3][3] = { {7, 0, 0},