C++
패턴찾기 이차원 배열 [2][2] 4,5,4,5 찾기 (A,B,B,T)
2sac
2024. 10. 11. 14:56
찾을 때 아예 arr 배열의 찾을 범위를 던져주는 for문의 첨자에서 제한하고 던져줌.
#include <iostream>
#include <cstring>
using namespace std;
int arr[2][4] = {
4,5,4,5,
5,5,4,5,
};
int brr[2][2] = {
4,5,
4,5,
};
int isSame(int a, int b) {
for (int y = 0; y < 2; y++) {
for (int x = 0; x < 2; x++) {
if (brr[y][x] != arr[a+y][b+x]) return 0;
}
}
return 1;
}
int main() {
int cnt = 0;
for (int y = 0; y < 1; y++) {
for (int x = 0; x <= 2; x++) {
cnt += isSame(y, x);
}
}
cout << "arr 패턴개수 : " << cnt;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
char arr[4][5] = {
"ABAB",
"BTBT",
"KABK",
"KBTK"
};
char brr[2][2] = {
'A','B',
'B','T',
};
int isSame(int a, int b) {
for (int y = 0; y < 2; y++) {
for (int x = 0; x < 2; x++) {
if (brr[y][x] != arr[a + y][b + x]) return 0;
}
}
return 1;
}
int main() {
int cnt = 0;
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
if (isSame(y, x)) cnt++;
}
}
cout << "패턴의 개수" << cnt;
return 0;
}