728x90
*복잡하더라도 그 방법밖에 없으면 그것이 정답이다*
my full code
// vector<string> 으로 받아오면됨
// row로 검사했다가 column방향으로 검사하면 됨
// 길이 1인 회문을 검사하라할때는 string 길이 * vector 사이즈 하면됨
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
for (int tc = 0; tc < 10; tc++) {
int N, answer = 0;
string str;
vector<string> vec;
cin >> N;
for (int i = 0; i < 8; i++) {
cin >> str;
vec.push_back(str);
}
// N==1 인경우
if (N == 1) {
cout << "#" << tc + 1 << " 64" << endl;
continue;
}
// row
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
bool found = true;
if (j + N - 1 >= 8) break;
int start = j, end = j + N - 1;
while (true) {
if (start >= end) break;
if (vec[i][start] != vec[i][end]) {
found = false;
break;
}
start++;
end--;
}
if (found) answer++;
}
}
// column
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
bool found = true;
if (i + N - 1 >= 8) break;
int start = i, end = i + N - 1;
while (true) {
if (start >= end) break;
if (vec[start][j] != vec[end][j]) {
found = false;
break;
}
start++;
end--;
}
if (found) answer++;
}
}
cout << "#" << tc + 1 << " " << answer << endl;
}
}
문제
https://swexpertacademy.com/main/code/problem/problemDetail.do
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
728x90
[접근1]
N이 1인경우 굳이 하나하나 다 돌 필요가 없다.
게다가 row랑 column을 다 돌면 중복이되버림.
따라서 따로 처리해준다. 모든 char들이 회문이므로 바로 64를 출력해주면된다.
// N==1 인경우
if (N == 1) {
cout << "#" << tc + 1 << " 64" << endl;
continue;
}
[접근2]
행 안에서 회문이 있는지 확인한다.
시작 인덱스와 끝 인덱스를 정하고, 시작인덱스와 끝 인덱스를 비교해나가며
시작 인덱스는 1씩 더하고 끝 인덱스는 1씩 뺀다.
만약 같거나 시작 인덱스가 끝 인덱스보다 커지면 비교가 끝난것이므로 while문을 빠져나가고 answer값을 1 증가시킨다.
그 전에 시작 인덱스와 끝 인덱스가 다르면 회문이 아니므로 found를 false로 처리해서 palin이 아님을 나타내고, while문을 빠져나간다.
column도 같은 방법으로 진행한다.
// row
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
bool found = true;
if (j + N - 1 >= 8) break;
int start = j, end = j + N - 1;
while (true) {
if (start >= end) break;
if (vec[i][start] != vec[i][end]) {
found = false;
break;
}
start++;
end--;
}
if (found) answer++;
}
}
// column
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
bool found = true;
if (i + N - 1 >= 8) break;
int start = i, end = i + N - 1;
while (true) {
if (start >= end) break;
if (vec[start][j] != vec[end][j]) {
found = false;
break;
}
start++;
end--;
}
if (found) answer++;
}
}728x90
'coding > algorithm' 카테고리의 다른 글
| [programmers] Lv.1 소수 만들기(c++) (0) | 2022.06.27 |
|---|---|
| [programmers] Lv.2 소수 찾기(c++) (0) | 2022.06.27 |
| [SW Expert Academy] D3 2805. 농작물 수확하기 (0) | 2022.05.28 |
| [백준 && SW] gold5/D3 N-Queen (0) | 2022.05.27 |
| [백준/baekjoon] Silver1 2667. 단지번호붙이기 (0) | 2022.05.27 |
댓글