반응형
https://www.acmicpc.net/problem/17144
17144번: 미세먼지 안녕!
미세먼지를 제거하기 위해 구사과는 공기청정기를 설치하려고 한다. 공기청정기의 성능을 테스트하기 위해 구사과는 집을 크기가 R×C인 격자판으로 나타냈고, 1×1 크기의 칸으로 나눴다. 구사
www.acmicpc.net



시뮬레이션 문제.
pypy3으로는 통과하지만 Python3으로는 시간초과가 나는데,
Python3 통과코드를 보고 어떻게 수정해봐도 내 코드는 더 이상 최적화가 안 된다. 왜지..?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from itertools import chain | |
r, c, t = map(int, sys.stdin.readline().split()) | |
maps = [list(map(int, sys.stdin.readline().split())) for _ in range(r)] | |
pos = 0 | |
# 공기청정기 위치 확인 | |
for y in range(r): | |
if maps[y][0] == -1: | |
pos = y | |
break | |
def rotate(cy): | |
# 반시계 | |
for y in range(cy-1, 0, -1): | |
maps[y][0] = maps[y-1][0] | |
maps[0][:-1] = maps[0][1:] | |
for y in range(cy): | |
maps[y][-1] = maps[y+1][-1] | |
maps[cy][2:] = maps[cy][1:-1] | |
maps[cy][1] = 0 | |
cy += 1 | |
# 시계 | |
for y in range(cy+1, len(maps)-1): | |
maps[y][0] = maps[y+1][0] | |
maps[-1][:-1] = maps[-1][1:] | |
for y in range(len(maps)-1, cy, -1): | |
maps[y][-1] = maps[y-1][-1] | |
maps[cy][2:] = maps[cy][1:-1] | |
maps[cy][1] = 0 | |
dirs = [(0,1),(0,-1),(1,0),(-1,0)] | |
for _ in range(t): | |
update_value = [[0 for _ in range(c)] for _ in range(r)] | |
# 미세먼지 이동 | |
for y in range(r): | |
for x in range(c): | |
if maps[y][x] >= 5: | |
for dy, dx in dirs: | |
ny, nx = y + dy, x + dx | |
if 0 <= ny < r and 0 <= nx < c and maps[ny][nx] != -1: | |
update_value[ny][nx] += maps[y][x] // 5 | |
update_value[y][x] -= maps[y][x] // 5 | |
# 이동한 미세먼지 반영 | |
for y in range(r): | |
for x in range(c): | |
maps[y][x] += update_value[y][x] | |
# 공기청정기 작동 | |
rotate(pos) | |
print(sum(chain(*maps))+2) |
반응형
'프로그래밍 > 코딩테스트 문제풀이' 카테고리의 다른 글
[Python] 백준 1244. 스위치 켜고 끄기 (0) | 2020.07.08 |
---|---|
[Python] 백준 19236. 청소년 상어 (0) | 2020.07.06 |
[Python] 프로그래머스. 2019 카카오 recruit - 블록 게임 (Level 4) (0) | 2020.05.19 |
[Python] 백준 15684. 사다리 조작 (0) | 2020.05.18 |
[Python] 백준 11003. 최솟값 찾기 (0) | 2020.05.16 |