[Programmers] 조이스틱

Time Lapse :1hour 11min 13sec

solution.cpp

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
31
32
33
34
35
36
37
#include <string>
#include <memory.h>
#include <iostream>
using namespace std;
int max_move, len;
void DFS(int moved,bool visited[20],int idx){
if(moved>=max_move)
return;
bool flag = true;
visited[idx] = true;
for(int i=0;i<=len;i++)
if(!visited[i]){
flag = false;
break;
}
if(flag){
max_move = moved;
return ;
}
bool n_visited[20];
memcpy(n_visited,visited,sizeof(n_visited));
DFS(moved+1,n_visited,idx+1>len ? 0 : idx+1);
memcpy(n_visited,visited,sizeof(n_visited));
DFS(moved+1,n_visited,idx-1==-1 ? len : idx-1);
}
int solution(string name) {
int answer = 0;
bool visit[20] = {false, };
for(int i=0;i<name.length();i++){
answer += name[i]-'A' < 'A'-name[i]+26 ? name[i]-'A' : 'A'-name[i]+26;
if(name[i]=='A')
visit[i]=true;
}
max_move = len = name.length()-1;
DFS(0,visit, 0);
return answer + max_move;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/joystick/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.