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
- JavaScript
- Math
- 백준
- BASIC
- Python
- 생활코딩
- BFS
- Cleancode
- programmers
- 인프런
- Algorithm
- C언어
- 종만북
- BOJ
- 따라하면서 배우는 C언어
- sorting
- greedy
- dfs
- udemy
- DP
- server
- C
- php
- 따배씨
- graph
- Algospot
- 따라하며 배우는 C언어
- web
- 정수론
- String
Archives
- Today
- Total
몽상실현개발주의
[프로그래머스] level2 / 거리두기 확인하기 / Python 파이썬 본문
[프로그래머스] level2 / 거리두기 확인하기 / Python 파이썬
https://programmers.co.kr/learn/courses/30/lessons/81302
코딩테스트 연습 - 거리두기 확인하기
[["POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"], ["POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"], ["PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"], ["OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"], ["PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"]] [1, 0, 1, 1, 1]
programmers.co.kr
풀이
주어진 지도를 탐색하고 조건에 부합하는지 확인하는 문제이다.
거리두기 조건은 직선 1칸, 직선 2칸 그리고 대각선 거리로 나누어 확인해 주었다.
answer = []
oneBlock_dy = [1, -1, 0, 0]
oneBlock_dx = [0, 0, 1, -1]
diagonal_dy = [1, 1, -1, -1]
diagonal_dx = [1, -1, 1, -1]
# 직선 한칸
def check_oneBlock(place, y, x):
for i in range(4):
if place[y][x] == 'P':
Y = y + oneBlock_dy[i]
X = x + oneBlock_dx[i]
if 0 <= Y < 5 and 0 <= X < 5 and place[Y][X] == 'P':
return False
return True
# 직선 두칸
def check_twoBlock(place, y, x):
for i in range(4):
if place[y][x] == 'P':
Y = y + (oneBlock_dy[i] * 2)
X = x + (oneBlock_dx[i] * 2)
if 0 <= Y < 5 and 0 <= X < 5 and place[Y][X] == 'P':
if place[y + oneBlock_dy[i]][x + oneBlock_dx[i]] != 'X':
return False
return True
# 대각선
def check_diagonal(place, y, x):
for i in range(4):
if place[y][x] == 'P':
Y = y + diagonal_dy[i]
X = x + diagonal_dx[i]
if 0 <= Y < 5 and 0 <= X < 5 and place[Y][X] == 'P':
if place[y][x+diagonal_dx[i]] != 'X' or place[y+diagonal_dy[i]][x] != 'X':
return False
return True
def checkPlace(place):
for y in range(5):
for x in range(5):
if place[y][x] == 'P':
if not check_oneBlock(place, y, x):
answer.append(0)
return
if not check_twoBlock(place, y, x):
answer.append(0)
return
if not check_diagonal(place, y, x):
answer.append(0)
return
answer.append(1)
return
def solution(places):
for place in places:
checkPlace(place)
return answer
'Algorithm PS > 프로그래머스' 카테고리의 다른 글
[프로그래머스] level1 / 없는 숫자 더하기 / Python 파이썬 (0) | 2021.11.16 |
---|---|
[프로그래머스] level1 / 약수의 개수와 덧셈 / Python 파이썬 (0) | 2021.11.16 |
[프로그래머스] level1 / 숫자 문자열과 영단어 / Python 파이썬 (0) | 2021.11.16 |
[프로그래머스] level2 / 프렌즈 4블록 / Python 파이썬 (0) | 2021.06.04 |
[프로그래머스] 후보키 / Python (0) | 2021.05.03 |
Comments