-
장애물 인식 프로그램(java)코딩테스트/softeer 2023. 9. 3. 16:06
https://softeer.ai/practice/info.do?idx=1&eid=409
Softeer
연습문제를 담을 Set을 선택해주세요. 취소 확인
softeer.ai
import java.util.*; import java.io.*; public class Main { public static int checkValue = 999; public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); int[][] pan = new int[n][n]; for(int i = 0; i < n; i++){ String value = in.readLine(); for (int j = 0; j < n; j++){ pan[i][j] = value.charAt(j) - '0'; } } ArrayList<Integer> resultList = new ArrayList<Integer>(); for(int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ if (pan[i][j] == 1){ resultList.add(getArea(pan, i, j, n)); } } } Collections.sort(resultList); System.out.println(resultList.size()); for(int result: resultList){ System.out.println(result); } } public static int getArea(int[][] pan, int x, int y, int n){ pan[x][y] = checkValue; int result = 1; int[] moveX = {-1, 0, 1, 0}; int[] moveY = {0, 1, 0, -1}; for (int i = 0; i<4; i++){ int nextX = x + moveX[i]; int nextY = y + moveY[i]; if(nextX < 0 || nextY < 0 || nextX >= n || nextY >= n){ continue; } if (pan[nextX][nextY] == 1){ result += getArea(pan, nextX, nextY, n); } } return result; } }
'코딩테스트 > softeer' 카테고리의 다른 글
비밀 메뉴(java) (0) 2023.09.03 지도 자동 구축(java) (0) 2023.09.03 8단 변속기(java) (0) 2023.09.03 금고털이 (0) 2023.09.03 주행거리 비교하기 (java) (0) 2023.09.03