회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
2
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
자바 런타임에러
14502번 - 연구소
wnsfuf0121
4년 전
0
3%에서 런타임에러가 나옵니다. 도와주세요!
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.StringTokenizer; public class Main { static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static int N, M, map[][], ans, possible; static ArrayList<node> zero_list, virus_list; static boolean used_list[]; static LinkedList<node> queue; static int dx[] = { -1, 1, 0, 0 }; static int dy[] = { 0, 0, -1, 1 }; static class node { int n1, n2; node(int n1, int n2) { this.n1 = n1; this.n2 = n2; } } public static void main(String[] args) throws Exception { input(); process(); System.out.println(ans); } static void input() throws Exception { st = new StringTokenizer(bf.readLine()); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); map = new int[N][M]; zero_list = new ArrayList<>(); virus_list = new ArrayList<>(); for (int i = 0; i < N; i++) { st = new StringTokenizer(bf.readLine()); for (int j = 0; j < N; j++) { int temp = Integer.parseInt(st.nextToken()); map[i][j] = temp; if (temp == 0) zero_list.add(new node(i, j)); else if (temp == 2) virus_list.add(new node(i, j)); } } used_list = new boolean[zero_list.size()]; possible = zero_list.size() - 3 ; ans = 0 ; } static void process() { int zero_size = zero_list.size(); for (int i = 0; i < zero_size; i++) { used_list[i] = true; for (int j = 0; j < zero_size; j++) { if (used_list[j]) continue; used_list[j] = true; for (int k = 0; k < zero_size; k++) { if (used_list[k]) continue; used_list[k] = true; spread(i, j, k); used_list[k] = false; } used_list[j] = false; } used_list[i] = false; } } static void spread(int a, int b, int c) { map[zero_list.get(a).n1][zero_list.get(a).n2] = 1; map[zero_list.get(b).n1][zero_list.get(b).n2] = 1; map[zero_list.get(c).n1][zero_list.get(c).n2] = 1; queue = new LinkedList<>(); for (node virus : virus_list) queue.add(virus); int cnt = possible; while (!queue.isEmpty()) { if(cnt < ans) break ; node cur = queue.poll(); for (int i = 0; i < 4; i++) { int next_n1 = cur.n1 + dx[i] ; int next_n2 = cur.n2 + dy[i] ; if(next_n1 < 0 || next_n1 > N-1 || next_n2 < 0 || next_n2 > M-1 || map[next_n1][next_n2] != 0 ) continue ; cnt -= 1 ; map[next_n1][next_n2] = 2 ; queue.offer(new node(next_n1, next_n2)) ; } } for(node zero : zero_list) map[zero.n1][zero.n2] = 0 ; ans = Math.max(ans, cnt) ; } }
djm03178
4년 전
1
43번째 줄은 N이 아니라 M이어야 합니다.
댓글을 작성하려면
로그인
해야 합니다.
wnsfuf0121 4년 전
3%에서 런타임에러가 나옵니다. 도와주세요!