본문 바로가기

CodingTest/Programmers

[ 프로그래머스 ] 거리두기

/  제출 1  /

def check_if_partitions(place, a, b):
    #there is partitions
    if abs(a[0] - b[0]) == 2:
        if place[max(a[0],b[0])-1][a[1]] == ('X' or 'P'):
            return True
    elif abs(a[1]-b[1]) == 2:
        if place[a[0]][max(a[1],b[1])-1] == ('X' or 'P'):
            return True
    else: # 1, 1 씩 차이
        if place[max(a[0],b[0])][min(a[1],b[1])]  == ('X' or 'P') and place[min(a[0], b[0])][max(a[1],b[1])] == ('X' or 'P'):
            return True
    return False

def solution(places):
    answer = []
    for place in places:
        print(places.index(place)+1)
        result = 1
        people = []
        for i in range(len(place)):
            for j in range(len(place)):
                if place[i][j] == 'P':
                    people.append((i,j))
                    
        for i in range(len(people)-1):
            for person in people[i+1:]:
                # calc manhat dist with people[i] and person
                man_dist = abs(person[0] - people[i][0]) + abs(person[1] - people[i][1])
                
                # 문제되는 상황
                if man_dist <= 2:
                    # 문제 구제 기회
                    # check all partitions between them
                    if not check_if_partitions(place, person, people[i]):
                        # if there is any partition: result = 0
                        result = 0
                        print(person, people[i],'not')
                    else:
                        result = 1
                        print(person, people[i],'yes')
        answer.append(result)
        
            
    return answer

 

채점 결과
정확성: 51.1
합계: 51.1 / 100.0

 

 

44 줄 쓰고.. 스스로 잘 읽히는 코드도 아니면서 13/31 맞는 내가 싫다.. 하..

 

테스트케이스는 다 통과했지만, 채점테케에서 처참한 성적입니다.

 

어쩌겠어요. 반복하면 좀 나아지겠죠.

 

이건 시간 많이 잡아먹었으니 다른 분 코드 찾아보고 정리하렵니다.

 


/  다른 분들 코드  /

 

https://velog.io/@sem/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-LEVEL2-%EA%B1%B0%EB%A6%AC%EB%91%90%EA%B8%B0-%ED%99%95%EC%9D%B8%ED%95%98%EA%B8%B0-Python

 

[프로그래머스] LEVEL2 거리두기 확인하기 (Python)

프로그래머스 알고리즘 풀이

velog.io

 

잘 정리해두셔서 해당 내용을 보고 공부했습니다.