def solution(dirs):
    answer = 0
    
    dx = [ 1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    strs = {"U": 0, "R": 1, "D": 2, "L": 3}
    
    visited = set()
    x, y = 0, 0
    
    for dir in dirs:
        
        i = strs[dir]
        nx, ny = x + dx[i], y + dy[i]
        if(nx < 6 and nx > -6 and ny < 6 and ny > -6):
            if (x, y, nx, ny) not in visited:
                visited.add((x, y, nx, ny))
                visited.add((nx, ny, x, y))
                answer +=1
            x, y = nx, ny            
    return answer

파이썬으로 푸는 것이 더 쉬워서 파이썬으로 풀이한 문제

 

#include <string>
#include <vector>
using namespace std;

int solution(string skill, vector<string> skill_trees) {
    int answer = 0;
    string newline = "";
    string tt = "";
    int check = 0;
    for(int i=0; i< skill_trees.size(); i++){
        tt = skill_trees[i];
        newline ="";
        check = 0;
        for(int j =0; j < tt.length(); j++){
            for(int l =0; l < skill.length(); l++){
                if(skill[l]==tt[j])   
                  newline+=tt[j];
            }
            
        }
        for(int k =0 ;k < newline.length(); k++){
            cout << newline[k];
            if(newline[k] !=  skill[k]){
                check = -1;
                break;
            }
        }
        if(check!=-1)
            answer+=1;
    }
    return answer;
}

'자윤이와고리즘 > Code' 카테고리의 다른 글

[프로그래머스] 쇠막대기  (0) 2019.07.22
[프로그래머스] 기능개발  (0) 2019.07.22
[프로그래머스] 탑  (0) 2019.07.22
[프로그래머스] 주식가격  (0) 2019.07.22
[백준] 10819 | 차이를 최대로  (0) 2019.05.14

+ Recent posts