회원가입
로그인
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
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
96%에서 틀렸습니다
7576번 - 토마토
toran8754
5년 전
0
모든 예제를 집어넣어도 맞는데 값이 나오는데 어디가 틀린건가요
못찾겠습니다 도와주세요 ㅠㅠ
import java.util.*; import java.io.*; public class Main { static int[][] check; static int[][] arr; static int row; static int column; static int[] dx = {-1,0,1,0}; static int[] dy = {0,1,0,-1}; public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); StringTokenizer st = new StringTokenizer(input); column = Integer.parseInt(st.nextToken()); row = Integer.parseInt(st.nextToken()); check = new int[row][column]; arr = new int[row][column]; for(int i = 0; i < row; i++) { String str = br.readLine(); StringTokenizer tk = new StringTokenizer(str); for(int j = 0; j < column; j++) { arr[i][j] = Integer.parseInt(tk.nextToken()); check[i][j] = arr[i][j]; } } bfs(); int result = 0; int err = 0; for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { if(arr[i][j] == 0) { err += 1; break; } result = Math.max(result, arr[i][j]); } } if(err == 1) { System.out.println("-1"); }else if(err != 1 && result == 1){ System.out.println("0"); }else if(err !=1 && result != 1){ System.out.println(result-1); } } public static void bfs() { Queue<Integer> q1 = new LinkedList<Integer>(); Queue<Integer> q2 = new LinkedList<Integer>(); for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { if(arr[i][j] == 1) { q1.add(i); q2.add(j); } } } while(!q1.isEmpty() && !q2.isEmpty()) { int ex = q1.poll(); int ey = q2.poll(); int rx; int ry; for(int i = 0; i < 4; i++) { rx = dx[i]+ex; ry = dy[i]+ey; if(rx >= 0 && ry >= 0 && rx < row && ry < column) { if(arr[rx][ry] == 0 && check[rx][ry] == 0) { q1.add(rx); q2.add(ry); check[rx][ry] = 1; arr[rx][ry] = arr[ex][ey] + 1; } } } } } }
댓글을 작성하려면
로그인
해야 합니다.
toran8754 5년 전
모든 예제를 집어넣어도 맞는데 값이 나오는데 어디가 틀린건가요
못찾겠습니다 도와주세요 ㅠㅠ