아직 계정이 없으신가요? 회원가입

Dev. Talk

좌석관리, 런타임에러가 나옵니다...

회원사진zaza1994
45 views2022-01-09 15:17

예제 테스트케이스는 잘 돌아갑니다.

제가 만든 테스트케이스들도 잘 돌아가고요.

근데 제출을 하면 1번 빼고는 나머지는 전부 런타임에러입니다.

메모리 관리를 못한건가요...?

계산해봐도 그건 아닌거 같은데 ㅠㅠ

도와주세요




import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {
	static int[][] map;
	static LinkedList<Pair> list;
	static HashSet<Integer> isIn, isOut;
	static int N, M;
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		isIn = new HashSet<>();
		isOut = new HashSet<>();
		list = new LinkedList<Pair>();
		int Q = Integer.parseInt(st.nextToken());
		map = new int[N+1][M+1];
		
		for(int q = 0; q < Q; q++) {
			st = new StringTokenizer(br.readLine());
			String cmd = st.nextToken();
			int id = Integer.parseInt(st.nextToken());
			if(cmd.equals("In")) in(id);
			else out(id);
		}
		
	}
	static void out(int id) {
		if(!isIn.contains(id)) {
			System.out.println(id + " didn't eat lunch"); return;
		}
		if(isOut.contains(id)) {
			System.out.println(id + " already left seat."); return;
		}
		
		for(Pair p : list) {
			if(p.id == id) {
				System.out.println(id + " leaves from the seat (" + p.r + ", " + p.c + ").");
				isOut.add(id);
				list.remove(p);
				map[p.r][p.c]= 0; 
			}
		
		}
		
	}
	static void in(int id) {
		if(isIn.contains(id)) {
			if(isOut.contains(id)) {
				System.out.println(id + " already ate lunch"); return;
			} else {
				System.out.println(id + " already seated."); return;
			}
		}
		
		checkIn(id);
	}
	static int[] dr = {-1,0,1,0};
	static int[] dc = {0,1,0,-1};
	static void checkIn(int id) {
		double maxD = 0;
		int max_i = 0;
		int max_j = 0;
		
		for(int i = 1; i < N+1; i++) {
			for(int j = 1; j < M+1; j++) {
				boolean isNear = false;
				for(int d = 0; d < 4; d++) {
					int nr = i + dr[d];
					int nc = j + dc[d];
					if(nr > 0 && nr < N+1 && nc > 0 && nc < M+1 && map[nr][nc] != 0) {
						isNear = true;
						break;
					}
				}
				
				if(!isNear && map[i][j] == 0) {
					double minD = Double.MAX_VALUE;
					for(Pair p : list) {
						int r1 = Math.abs(p.r - i);
						int r2 = Math.abs(p.c - j);
						double D = Math.sqrt(r1*r1 + r2*r2);
						if(D < minD) minD = D;
					}
					if(minD > maxD) {
						maxD = minD;
						max_i = i;
						max_j = j;
					}
				}
			}
		}
		if(max_i == 0 && max_j == 0) System.out.println("There are no more seats.");
		else {
			System.out.println(id + " gets the seat (" + max_i + ", " + max_j + ").");
			list.add(new Pair(max_i,max_j,id));
			map[max_i][max_j] = id;
			isIn.add(id);
		}
		
	}
	static class Pair{
		int r, c, id;
		Pair(int r, int c, int id){
			this.r = r; this.c = c; this.id = id;
		}
	}
	
}