-
백준 23031번: 으어어... 에이쁠 주세요..(Python/Java)Algorithm/Algorithm Problem 2022. 2. 12. 18:21
https://www.acmicpc.net/problem/23031
23031번: 으어어… 에이쁠 주세요..
밤이 되면 어두워지는 다솔관에는 좀비가 나온다는 괴담이 있다. 그 좀비들의 정체는 바로 시험 기간에 공부하느라 지친 학생들이었다. 지친 학생들은 멀리서 보면 흡사 좀비이므로 학생 좀비
www.acmicpc.net
접근
다른 알고리즘을 활용할 필요도 없이 조건에 맞춰 구현만 해주면 되기 때문에 특별히 어려운 부분은 없다. 다만 주의할 점이 있다면 대학원 생을 만났을 때와 스위치에 접근했을 때, 동시에 사건이 일어난다면 무조건 스위치 키는 행위가 먼저 일어난다는 부분. 이 부분을 간과하고 순서를 다르게 풀이한다면 한동안 오류에서 헤맬 수 있다.
한 가지 더 체크해야 하는 부분. 내가 현재 위치를 탐색했을 때, 이미 해당 위치에 대학원생이 있을 수 있다. 그리고 내가 해당 위치를 탐색할 때에는 대학원생이 없다가 탐색을 끝낸 뒤 대학원생이 올 수 있다. 즉, 대학원생을 각각 한 번씩 체크해주는 방식으로 풀었다. 이 부분, 즉 대학원생을 체크해야 하는 부분을 정확히 파악하고 체크해야 하는 것 또한 확인해두자. 덧붙여 대학원생들을 너무 불쌍히 여기지 말자. 그저 한 순간 잘못된 선택을 한 사람들일 뿐...
풀이
Python
length = int(input()) orderList = list(input()) board = [[["", "D"] for i in range(length)] for i in range(length)] zombieBoard = [[0 for i in range(length)] for i in range(length)] zombieList = [] for i in range(length): firstLine = input() for j in range(length): board[i][j][0] = firstLine[j] if board[i][j][0] == "Z": zombieBoard[i][j] += 1 zombieList.append([i, j, 0]) upDown = [1, 0, -1, 0, -1, -1, 1, 1] leftRight = [0, 1, 0, -1, -1, 1, -1, 1] thisLocation = [0, 0, 0] resultCheck = True for i in orderList: if i == "F": subUpDown = (thisLocation[0] + upDown[thisLocation[2]]) subLeftRight = (thisLocation[1] + leftRight[thisLocation[2]]) if 0 <= subUpDown < length and 0 <= subLeftRight < length: thisLocation[0] = subUpDown thisLocation[1] = subLeftRight if board[thisLocation[0]][thisLocation[1]][0] == "S": board[thisLocation[0]][thisLocation[1]][1] = "B" for j in range(8): lightUpDown = (thisLocation[0] + upDown[j]) lightLeftRight = (thisLocation[1] + leftRight[j]) if 0 <= lightUpDown < length and 0 <= lightLeftRight < length: board[lightUpDown][lightLeftRight][1] = "B" if zombieBoard[thisLocation[0]][thisLocation[1]] != 0: if board[thisLocation[0]][thisLocation[1]][1] == "D": resultCheck = False break elif i == "L": if thisLocation[2] == 3: thisLocation[2] = 0 else: thisLocation[2] += 1 elif i == "R": if thisLocation[2] == 0: thisLocation[2] = 3 else: thisLocation[2] -= 1 for j in range(len(zombieList)): zombieUpDown = (zombieList[j][0] + upDown[zombieList[j][2]]) if 0 <= zombieUpDown < length: zombieBoard[zombieList[j][0]][zombieList[j][1]] -= 1 zombieList[j][0] = zombieUpDown zombieBoard[zombieList[j][0]][zombieList[j][1]] += 1 if zombieBoard[thisLocation[0]][thisLocation[1]] != 0: if board[thisLocation[0]][thisLocation[1]][1] == "D": resultCheck = False break else: if zombieList[j][2] == 0: zombieList[j][2] = 2 else: zombieList[j][2] = 0 if not resultCheck: break if resultCheck: print("Phew...") else: print("Aaaaaah!")
Java
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int length = Integer.parseInt(scan.nextLine()); String[] orderList = (scan.nextLine()).split(""); String[][][] board = new String[length][length][2]; int[][] zombieBoard = new int[length][length]; ArrayList<int[]> zombieList = new ArrayList<int[]>(); for(int i = 0 ; i < length ; i++){ String[] firstLine = (scan.nextLine()).split(""); for(int j = 0 ; j < length ; j++){ board[i][j][0] = firstLine[j]; board[i][j][1] = "D"; if(board[i][j][0].equals("Z")){ zombieBoard[i][j]++; zombieList.add(new int[] {i, j, 0}); } } } int[] upDown = {1, 0, -1, 0, -1, -1, 1, 1}; int[] leftRight = {0, 1, 0, -1, -1, 1, -1, 1}; int[] thisLocation = new int[3]; boolean resultCheck = true; loop: for(String i: orderList){ if(i.equals("F")){ int subUpDown = (thisLocation[0] + upDown[thisLocation[2]]); int subLeftRight = (thisLocation[1] + leftRight[thisLocation[2]]); if(subUpDown >= 0 && subUpDown < length && subLeftRight >= 0 && subLeftRight < length){ thisLocation[0] = subUpDown; thisLocation[1] = subLeftRight; if(board[thisLocation[0]][thisLocation[1]][0].equals("S")){ board[thisLocation[0]][thisLocation[1]][1] = "B"; for(int j = 0 ; j < 8 ; j++){ int lightUpDown = (thisLocation[0] + upDown[j]); int lightLeftRight = (thisLocation[1] + leftRight[j]); if(lightUpDown >= 0 && lightUpDown < length && lightLeftRight >= 0 && lightLeftRight < length){ board[lightUpDown][lightLeftRight][1] = "B"; } } } if(zombieBoard[thisLocation[0]][thisLocation[1]] != 0){ if(board[thisLocation[0]][thisLocation[1]][1].equals("D")){ resultCheck = false; break; } } } }else if(i.equals("L")){ if(thisLocation[2] == 3){ thisLocation[2] = 0; }else{ thisLocation[2]++; } }else if(i.equals("R")){ if(thisLocation[2] == 0){ thisLocation[2] = 3; }else{ thisLocation[2]--; } } for(int j = 0 ; j < zombieList.size() ; j++){ int zombieUpDown = (zombieList.get(j)[0] + upDown[zombieList.get(j)[2]]); if(zombieUpDown >= 0 && zombieUpDown < length){ zombieBoard[zombieList.get(j)[0]][zombieList.get(j)[1]]--; zombieList.get(j)[0] = zombieUpDown; zombieBoard[zombieList.get(j)[0]][zombieList.get(j)[1]]++; if(zombieBoard[thisLocation[0]][thisLocation[1]] != 0){ if(board[thisLocation[0]][thisLocation[1]][1].equals("D")){ resultCheck = false; break loop; } } }else{ if(zombieList.get(j)[2] == 0){ zombieList.get(j)[2] = 2; }else{ zombieList.get(j)[2] = 0; } } } } if(resultCheck){ System.out.println("Phew..."); }else{ System.out.println("Aaaaaah!"); } } }
후기
다른 알고리즘을 쓸 필요 없이 구현만 잘하면 되는 문제였기 때문에 어렵지 않게 풀 수 있었다. 물론 그 구현해내는 부분에서 자잘한 실수들이 많긴 했지만...
방향을 바꾸는 부분에서 원하는 방향을 숫자로 바꾸고 1씩 가감해 방향 테이블의 인덱스로 사용하는 방식은 너비 우선 탐색 알고리즘을 풀 때 사용했던 방식을 가져와 응용한 것이다. 앞으로도 비슷한 구현 방식을 때에 맞춰 활용하는 연습을 더 해야겠다.
'Algorithm > Algorithm Problem' 카테고리의 다른 글
백준 9204번: 체스(Python/Java) (0) 2022.02.17 백준 12931번: 두 배 더하기(Python/Java) (0) 2022.02.13 백준 17939번: Gazzzua(Python/Java) (0) 2022.02.11 백준 1930번: 정사면체 (Python/Java) (0) 2022.02.07 백준 19952번: 인성 문제 있어?? (Python/Java) (0) 2022.02.05