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 | 29 | 30 |
Tags
- 디자인 패턴
- 모던C++디자인패턴
- 팩터리
- 그루비 스타일 빌더
- 동적 데커레이터
- 싱글톤 패턴
- 추상 팩터리
- 브릿지 패턴
- 흐름식 빌더
- 빌더 패턴
- 함수형 팩터리
- 동적 데코레이터
- 빌더
- 싱글턴 패턴
- 디자인패턴
- 컴포지트 패턴
- 브릿지
- 프로토타입 중복처리
- 데커레이터
- 팩터리 패턴
- 프로토타입 패턴
- 팩터리 메서드
- 컴포지트 빌더
- 프로토타입
- 데커레이터 패턴
- 내부 팩터리
- 컴포지트
- 단순한 빌더
- 싱글턴
- 싱글톤
Archives
- Today
- Total
GGym's Practice Notes
프로그래머스 Level2_ 조이스틱 (C++) 본문
분류는 그리디라고 되어있는데 최적의 해를 찾기 위해선 브루트포스 방식으로 코드를 짜야하는듯하다.
그리고 백준에도 같은 문제가 있는데 테스트케이스 조건이 더 빡빡해서 이 코드 그대로 썼다가 시간초과가 나서 백준에서는 코드를 수정하여 제출 했다.
백준 문제 : www.acmicpc.net/problem/3663
3663번: 고득점
현수는 조이스틱을 이용해 지렁이를 미로에서 탈출시키는 게임을 하고 있다. 최고 점수를 얻은 경우에는 조이스틱을 이용해서 이름을 입력해야 한다. 이름을 입력하는 과정은 다음과 같다. 가��
www.acmicpc.net
#include <string> // Level2_ 조이스틱 [그리디]
#include <vector>
#include <string.h>
#include <iostream>
#define calc(c) c - 'A' < 'Z' - c + 1 ? c - 'A' : 'Z' - c +1
using namespace std;
int right(int n, int start, string name, string target){
int r = 0;
if(name == target) {
return 0;
}
if(n == target.length()) {
n = 0;
}
name[n] = target[n];
r += calc(target[n]);
r += right(n+1, start, name, target) + 1;
return r;
}
int left(int n, int start, string name, string target){
int r = 0;
if(name == target) {
return 0;
}
if(n < 0) {
n = target.length()-1;
};
name[n] = target[n];
r += calc(target[n]);
r += left(n-1, start, name, target) +1;
return r;
}
int solution(string name) {
int answer = 10000000;
for(int i=0; i<name.length(); i++){
char s[21] = "AAAAAAAAAAAAAAAAAAAA";
s[name.length()] = '\0';
int n = calc(name[i]);
s[i] = name[i];
n += left(i-1, i, s, name) < right(i+1, i, s, name) ? left(i-1, i, s, name) : right(i+1, i, s, name);
n += i;
if (answer > n) answer = n;
}
return answer;
}
'Algorithm' 카테고리의 다른 글
프로그래머스 Level1_ 크레인 인형뽑기 게임 (C++) (0) | 2020.10.08 |
---|---|
프로그래머스 Level2_ 주식가격 (C++) (0) | 2020.10.08 |
프로그래머스 Level2_ 스킬 트리 (C++) (0) | 2020.10.08 |
프로그래머스 Level3_ 단어 변환 (C++) (0) | 2020.10.08 |
프로그래머스 Level2_ 다리를 지나는 트럭 (C++) (0) | 2020.10.08 |