SSAFY/SWEA

[SWEA] 4408 : 자기 방으로 돌아가기

믕비 2023. 5. 3. 15:47

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=4&contestProbId=AWNcJ2sapZMDFAV8&categoryId=AWNcJ2sapZMDFAV8&categoryType=CODE&problemTitle=&orderBy=SUBMIT_COUNT&selectCodeLang=JAVA&select-1=4&pageSize=10&pageIndex=1 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

쉽다고 생각했는데 정답률이 낮았던 문제. 복도배열을 생각해내는 게 중요했던 것 같다.

 

[풀이과정]

같은 곳을 지날 때 1초씩 기다려야 한다 했으니 같은 곳을 지나는 학생의 최대수가 답이 된다.

방이 마주보는 형태로 2개의 방이 하나의 복도를 공유하니까 복도를 의미하는 배열을 하나 만들어서 사용했다.

현재방의 번호가 돌아가야 하는 방의 번호보다 클 수 있는데, 방향은 중요하지 않기 때문에 작은 번호부터 큰 번호까지 for문을 돌게 했다.

 

내 코드

메모리 : 17696 KB

시간 : 101 ms

코드길이 : 1445 B

 

[내 코드]

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

public class solution {
	static int[] corridor;

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		StringTokenizer st;
		
		int T = Integer.parseInt(br.readLine());
		for(int t = 1; t <= T; t++) {
			sb.append('#').append(t).append(' ');
			int N = Integer.parseInt(br.readLine());
			corridor = new int[201];
			
			for(int n = 0; n < N; n++) {
				st = new StringTokenizer(br.readLine());
				int nowRoom = Integer.parseInt(st.nextToken());
				int myRoom = Integer.parseInt(st.nextToken());
				
				int nowCorridor = nowRoom % 2 == 0 ? nowRoom / 2 : nowRoom / 2 + 1;
				int myCorridor = myRoom % 2 == 0 ? myRoom / 2 : myRoom / 2 + 1;
				
				route(nowCorridor, myCorridor);
			}
			
			sb.append(getMax()).append('\n');
		}
		System.out.print(sb);
	}
	
	public static void route(int nowCorridor, int myCorridor) {
		int start = Math.min(nowCorridor, myCorridor);
		int end = Math.max(nowCorridor, myCorridor);
		
		for(int i = start; i <= end; i++) {
			corridor[i]++;
		}
		return;
	}
	
	public static int getMax() {
		int result = 1;
		for(int i = 0; i < 201; i++) {
			result = Math.max(corridor[i], result);
		}
		return result;
	}
}

 

'SSAFY > SWEA' 카테고리의 다른 글

[SWEA] 11315 : 오목판정  (0) 2023.05.03
[SWEA] 2817 : 부분 수열의 합  (0) 2023.05.03
[SWEA] 6485 : 삼성시의 버스 노선  (0) 2023.05.02
[SWEA] 9229 : 한빈이와 spot Mart  (0) 2023.05.02
[SWEA] 5607 : 조합 (풀이중)  (0) 2023.05.02