250420

Regular Kim·2025년 4월 20일

회고

목록 보기
88/115

250420 회고 💬

SSAFY에서 스프링 과정을 시작했다. 서블릿 때와 마찬가지로 pom.xml로 프로젝트 설정을 했다. 설정 작업 오랜만에 했다. 할 때마다 괴로운 xml 설정,,,, 💀

Keep 👍

  • 알고리즘 문제 풀이를 계속 하고있다. 그날그날 기분따라서 아무 주제나 잡고 문제를 푼다.
    - 줄 세우기 : 전형적인 위상정렬 문제다. 어렵지 않음.
    - 소수의 연속합 : 에라토스테네스의 채 사용하는 문제. 해당 개념을 알고 있다면 어렵지 않다.
    - 벽 부수고 이동하기 4 : 어려웠다. 기존 BFS 방식으로는 풀이 안된다. 시간 단축을 하기 위한 아이디어 떠올리기가 쉽지 않음.
  • 싸피에서 스프링 과정을 시작한 만큼 스프링 개념 공부를 다시 하고있다.
    - 객체지향 원칙

Try 🧚

  • 서류 작성하기
    - 이력서
    - 포트폴리오
  • 기업 1곳 이상 지원하기
  • 토이 프로젝트 작업
    - 백엔드
    - 레스트 독스 구현
    - 테스트 코드 고도화
    - 프론트엔드
    - 비디오 관리 기능 화면

독서 목록

서평 완료 목록

서평 예정 목록 (읽는 중)

  • 면접을 위한 CS 전공지식 노트

독서 예정 목록

  • 오브젝트
  • HTTP 완벽 가이드
  • 자바/스프링 개발자를 위한 실용주의 프로그래밍
  • 모던 자바 인 액션
  • 자바 성능 튜닝 이야기
  • 헤드 퍼스트 서블릿
  • 파이브 라인스 오브 코드

Extra

벽 부수고 이동하기 4


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

	public static void main(String[] args) {
		new Main().run();
	}

	private void run() {
		System.out.println(new Solution().solution(readInput()));
	}

	private char[][] readInput() {
		try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

			StringTokenizer st = new StringTokenizer(br.readLine());
			int height = Integer.parseInt(st.nextToken());
			int width = Integer.parseInt(st.nextToken());
			char[][] result = new char[height][width];

			for (int i = 0; i < height; i++) {
				result[i] = br.readLine().toCharArray();
			}
			return result;
		} catch (IOException e) {
			throw new RuntimeException();
		}
	}
}

class Solution {

	private static final int[][] DIRECTIONS = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
	private static final int WALL = '1';
	private static final int PLAIN = '0';

	private char[][] field;
	private Map<Integer, Integer> groupMap;
	private Queue<int[]> walls;
	private int[][] visited;
	private int[][] result;


	public String solution(char[][] field) {
		init(field);
		explore();
		return getAnswer();
	}

	private void init(char[][] field) {
		this.field = field;
		this.groupMap = new HashMap<>();
		this.walls = new ArrayDeque<>();
		this.visited = new int[field.length][field[0].length];
		this.result = new int[field.length][field[0].length];


		int groupId = 1;
		for (int i = 0; i < field.length; i++) {
			for (int j = 0; j < field[i].length; j++) {
				if (field[i][j] == PLAIN && visited[i][j] == 0) {
					groupMap.put(groupId, BFS(i, j, groupId));
					groupId++;
				} else if(field[i][j] == WALL) {
					walls.add(new int[]{i, j});
				}
			}
		}
	}

	private void explore() {
		for(int[] wall : walls) {
			Set<Integer> groupAround = new HashSet<>();
			int x = wall[0];
			int y = wall[1];
			for(int[] d : DIRECTIONS) {
				int nx = x + d[0];
				int ny = y + d[1];
				if(out(nx, ny)) continue;
				if(field[nx][ny] == WALL) continue;

				groupAround.add(visited[nx][ny]);
			}
			int width = 1;
			for (Integer groupId : groupAround) {
				width += groupMap.get(groupId);
			}
			result[x][y] = width % 10;
		}
	}

	private int BFS(int x, int y, int groupId) {
		Queue<int[]> q = new ArrayDeque<>();
		q.offer(new int[]{x, y});
		visited[x][y] = groupId;

		int chck = 1;

		while (!q.isEmpty()) {
			int[] cur = q.poll();

			for (int[] d : DIRECTIONS) {
				int nx = cur[0] + d[0];
				int ny = cur[1] + d[1];

				if(out(nx, ny)) continue;
				if(field[nx][ny] == WALL) continue;
				if(visited[nx][ny] == groupId) continue;

				visited[nx][ny] = groupId;
				q.offer(new int[]{nx, ny});
				chck++;
			}
		}
		return chck;
	}

	private boolean out(int x, int y) {
		return field.length <= x || x < 0 || field[x].length <= y || y < 0;
	}

	private String getAnswer() {
		StringBuilder answer = new StringBuilder();
		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[i].length; j++) {
				answer.append(result[i][j] % 10);
			}
			answer.append("\n");
		}
		return answer.toString();
	}
}
profile
What doesn't kill you, makes you stronger

0개의 댓글