[프로그래머스] 조이스틱
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(string name) {
int answer = 0;
int nameLen = name.length();
int updown = 0;
for (int i = 0; i < nameLen; ++i) {
int up = int(name[i]) - int('A');
int down = 26 - up;
updown += min(up, down);
}
int leftright = name.length() - 1;
string AAA = "";
for (int i = 0; i < nameLen; ++i) {
AAA += "A";
}
for (int i = 0; i < nameLen; ++i) {
string tmpName = name;
int case1 = 0;
tmpName[0] = 'A';
if (tmpName != AAA) {
for (int j = 1; j <= i; ++j) {
case1++;
tmpName[j] = 'A';
if (tmpName == AAA) break;
}
}
if (tmpName != AAA) case1 *= 2;
if (tmpName != AAA) {
for (int j = nameLen-1; j > i; --j) {
case1++;
tmpName[j] = 'A';
if (tmpName == AAA) break;
}
}
tmpName = name;
int case2 = 0;
tmpName[0] = 'A';
if (tmpName != AAA) {
for (int j = nameLen - 1; j > i; --j) {
case2++;
tmpName[j] = 'A';
if (tmpName == AAA) break;
}
}
if (tmpName != AAA) case2 *= 2;
if (tmpName != AAA) {
for (int j = 1; j <= i; ++j) {
case2++;
tmpName[j] = 'A';
if (tmpName == AAA) break;
}
}
leftright = min(leftright,min(case1, case2));
}
return answer = updown + leftright;
}
📌참고자료