회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
[java] 문제에서 주어진 예시는 다 통과하는데 .. 도와주세요 ㅠㅠ
16236번 - 아기 상어
diddbtjr7
2년 전
0
예제는 다 통과하는데
제출시 제출하자마자 틀렸습니다가 나오네요..
감도 안오는데 도와주시면 정말 감사하겠습니다 ㅠㅠ
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static class Node implements Comparable<Node> { int x; int y; int distance; public Node(int x, int y, int distance) { this.x = x; this.y = y; this.distance = distance; } @Override public int compareTo(Node o) { if (this.distance != o.distance) return Integer.compare(this.distance, o.distance); if (this.x == o.x) return Integer.compare(this.y, o.y); else return Integer.compare(this.x, o.x); } } private static int[][] arr; private static int[][] dist; private static boolean[][] visit; // 북 동 남 서 private static int[] dx = new int[] {-1, 0, 1, 0}; private static int[] dy = new int[] {0, 1, 0, -1}; private static int n; private static List<Node> list = new ArrayList<>(); private static Queue<Node> queue = new LinkedList<>(); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine()); arr = new int[n][n]; dist = new int[n][n]; visit = new boolean[n][n]; for (int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); for (int j = 0; j < n; j++) { int num = Integer.parseInt(st.nextToken()); arr[i][j] = num; } } int answer = 0; int size = 2; int count = 0; while (true) { // 먹을 수 있는 물고기가 존재하는지 확인 if (func(size)) break; // 먹을 수 있는 물고기들의 위치 담아주기 Node shark = func2(size); // 모든 물고기들의 거리 계산하기 func3(shark, size); for (int i = 0; i < list.size(); i++) { Node node = list.get(i); int distance = dist[node.x][node.y]; node = new Node(node.x, node.y, distance); list.set(i, node); } Collections.sort(list); Node node = list.get(0); answer += node.distance; arr[node.x][node.y] = 9; arr[shark.x][shark.y] = 0; count++; if (count == size) { count = 0; size++; } list.clear(); } System.out.println(answer); } // 먹을 수 있는 물고기가 존재하는지 확인 private static boolean func(int size) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (arr[i][j] != 0 && arr[i][j] < size) return false; return true; } // 먹을 수 있는 물고기들의 위치 담아주기 private static Node func2(int size) { Node shark = new Node(0, 0, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[i][j] != 0 && arr[i][j] < size) { Node node = new Node(i, j, 0); list.add(node); } if (arr[i][j] == 9) { shark = new Node(i, j, 0); } } } return shark; } // 먹을 수 있는 물고기들의 거리 계산하기 private static void func3(Node shark, int size) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { visit[i][j] = false; dist[i][j] = 0; } } queue.add(shark); while (!queue.isEmpty()) { shark = queue.poll(); int x = shark.x; int y = shark.y; int distance = shark.distance; visit[x][y] = true; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue; if (arr[nx][ny] > size) continue; if (visit[nx][ny] == true) continue; dist[nx][ny] = distance + 1; queue.add(new Node(nx, ny, distance + 1)); visit[nx][ny] = true; } } } }
댓글을 작성하려면
로그인
해야 합니다.
diddbtjr7 2년 전
예제는 다 통과하는데
제출시 제출하자마자 틀렸습니다가 나오네요..
감도 안오는데 도와주시면 정말 감사하겠습니다 ㅠㅠ