회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
python 시간초과, pypy3 메모리 초과
11627번 - Godzilla
dkdlel6887
3년 전
0
풀이는 맞는것 같은데 시간초과나 메모리 초과가 발생하네요..
맞는 풀이일까요?
tc = int(input()) for _ in range(tc): m, n = map(int, input().split()) maps = [list(input().strip()) for _ in range(n)] visited = [[0] * m for _ in range(n)] directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] # 북 동 남 서 mech = [] resi = 0 end = False for i in range(n): for j in range(m): if maps[i][j] == "G": gozila = (i, j) if maps[i][j] == "M": mech.append((i, j)) if maps[i][j] == "R": visited[i][j] = -1 # 고질라 움직이는 함수 def gozila_move(gozila_loc): global resi y, x = gozila_loc moved = False for dy, dx in directions: ny, nx = y + dy, x + dx if 0 <= ny < n and 0 <= nx < m and visited[ny][nx] == -1: visited[ny][nx] = 1 resi += 1 gozila_loc = (ny, nx) moved = True break if not moved: for dy, dx in directions: ny, nx = y + dy, x + dx if 0 <= ny < n and 0 <= nx < m and visited == 0: visited[ny][nx] = 1 gozila_loc = (ny, nx) break return gozila_loc # 메카 움직이고 공격하는 함수 def mech_move(mech_list): global end for y, x in mech_list: for dy, dx in directions: for k in range(0, n): ny, nx = y + dy * k, x + dx * k if 0 <= ny < n and 0 <= nx < m: if visited[ny][nx] == -1: break if (ny, nx) == gozila: end = True return new_mech = [] while mech_list: y, x = mech_list.pop() moved = False for dy, dx in directions: ny, nx = y + dy, x + dx if 0 <= ny < n and 0 <= nx < m and visited[ny][nx] != -1: new_mech.append((ny, nx)) moved = True if not moved: new_mech.append((y, x)) for y, x in new_mech: for dy, dx in directions: for k in range(1, n): ny, nx = y + dy * k, x + dx * k if 0 <= ny < n and 0 <= nx < m: if visited[ny][nx] == -1: break if (ny, nx) == gozila: end = True return return new_mech # 방문했거나 거주지 아니어서 이동할 곳 없으면 북, 동, 남, 서 순으로 갈 수 있는 곳 그냥 움직임 # 일직선 상에 있으면 고질라 죽이기 가능, 단 중간에 거주지 없어야함 while not end: gozila = gozila_move(gozila) mech = mech_move(mech) print(resi)
댓글을 작성하려면
로그인
해야 합니다.
dkdlel6887 3년 전
풀이는 맞는것 같은데 시간초과나 메모리 초과가 발생하네요..
맞는 풀이일까요?