Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준 14246번 K보다 큰 구간
- DFS
- 월곡역 학원
- c++ 조합
- 백준 1049번
- C++ 9996
- 백준 9375번 패션왕 신해빈
- 백준 1049번 기타줄
- 고정 소수점
- 백준 한국이 그리울 땐 서버에 접속하지
- 상월곡역 학원
- 운영체제
- 백준 패션왕 신해빈
- 월곡중 학원
- C++ 문자열
- 월곡동 학원추천
- OS
- 상월곡동 학원
- 백준 10709
- c++ split
- 백준 2309번 일곱 난쟁이
- 백준 14246번
- C# 병합정렬
- 서울사대부고 학원
- 백준 dfs
- 백준 K보다 큰 구간
- 관리형 학원
- 백준 토마토
- 월곡중학교 학원추천
- 성북구 학원
Archives
- Today
- Total
키베이루's diary
[C++] 백준 3085번 사탕 게임 본문
1) 문제설명
2) 아이디어
가로/세로 인접한 글자가 다를 때마다 바꾸고 인접한 문자의 개수를 센다.
3) 코드
#include<iostream>
#include<queue>
#include<deque>
#include<string.h>
#include<math.h>
#include<cmath>
#include<stack>
#include<algorithm>
using namespace std;
int n;
char arr[51][51];
int check() { // 가로 세로 중복확인 함수
int maxt = 0;
int v1_cnt = 0, h1_cnt = 0;
for (int i = 0; i < n; i++) {
int v_cnt = 0, h_cnt = 0;
// 가로 중복확인
for (int j = 0; j < n; j++) {
if (arr[i][j] == arr[i][j + 1]) {
v_cnt++;
}
else {
v_cnt = 0;
}
v1_cnt = max(v_cnt, v1_cnt);
}
// 세로 중복확인
for (int j = 0; j < n; j++) {
if (arr[j][i] == arr[j + 1][i]) {
h_cnt++;
}
else {
h_cnt = 0;
}
h1_cnt = max(h_cnt, h1_cnt);
}
maxt = max(maxt, max(v1_cnt, h1_cnt));
}
return maxt;
}
int main() {
int result = 0;
int result1 = 0, result2 = 0;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j + 1 < n) {
if (arr[i][j] != arr[i][j + 1]) { // 가로가 다르면 위치를 바꾼다.
char temp;
temp = arr[i][j];
arr[i][j] = arr[i][j + 1];
arr[i][j + 1] = temp;
}
int maxt = check() + 1; // 가로/세로 중복 갯수 세기
result1 = max(result1, maxt);
char temp = arr[i][j]; // 다했으면 다시 원위치
arr[i][j] = arr[i][j + 1];
arr[i][j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j + 1 < n) {
if (arr[j][i] != arr[j+1][i]) { // 세로가 다르면 위치를 바꾼다.
char temp;
temp = arr[j][i];
arr[j][i] = arr[j+1][i];
arr[j+1][i] = temp;
}
int maxt = check() + 1; // 가로/세로 중복 갯수 세기
result2 = max(result2, maxt);
char temp;
temp = arr[j][i];
arr[j][i] = arr[j + 1][i];
arr[j + 1][i] = temp;
}
}
}
result = max(result1, result2);
cout << result;
}
'알고리즘 > 기타' 카테고리의 다른 글
[C++] Combination(조합) (0) | 2022.12.26 |
---|---|
[C#] Merge Sort (1) | 2022.12.23 |
[C++] 백준 1021번 회전하는 큐 (0) | 2022.09.30 |
[C++] 백준 2805번 나무 자르기 (0) | 2022.07.19 |
[C++] 백준 1654번 랜선 자르기 (0) | 2022.07.18 |
Comments