회원가입
로그인
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
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
2%에서 틀렸다고 나옵니다 ㅠ 반례 부탁드립니다.(자바)
16234번 - 인구 이동
bravery0730
3년 전
0
테스트케이스와 게시판에 올라온 반례 모두 맞지만 2%에서 틀렸다고 합니다ㅠ.. 반례 부탁드리겠습니다.
public class Main { static int[] dx = { 0, -1, 0, 1 }; static int[] dy = { 1, 0, -1, 0 }; static int[][] map; static boolean[][] visited; static int N, L, R, row; public static void main(String[] args) throws Exception { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] strings = br.readLine().split(" "); N = Integer.parseInt(strings[0]); L = Integer.parseInt(strings[1]); R = Integer.parseInt(strings[2]); row = N + (N - 1); map = new int[row][row]; for (int i = 0; i < row; i += 2) { String[] strs = br.readLine().split(" "); int idx = 0; for (int j = 0; j < row; j += 2) { map[i][j] = Integer.parseInt(strs[idx]); idx++; } } int cnt = 0; while (true) { visited = new boolean[row][row]; if (openBarrier()) { for (int i = 0; i < row; i += 2) { for (int j = 0; j < row; j += 2) { if (!visited[i][j]) { visited[i][j] = true; bfs(i, j); } } } } else { break; } cnt++; } bw.write(Integer.toString(cnt)); bw.flush(); bw.close(); br.close(); } public static void bfs(int x, int y) { Queue<Pair> q = new LinkedList<>(); ArrayList<Pair> country = new ArrayList<>(); country.add(new Pair(x, y)); q.add(new Pair(x, y)); int sum = map[x][y]; while (!q.isEmpty()) { Pair pair = q.poll(); for (int i = 0; i < 4; i++) { int nx = pair.x + dx[i]; int ny = pair.y + dy[i]; if (nx < 0 || ny < 0 || nx >= row || ny >= row) { continue; } if (map[nx][ny] == Integer.MAX_VALUE) { continue; } if (!visited[nx][ny]) { visited[nx][ny] = true; q.add(new Pair(nx, ny)); if (map[nx][ny] != 0) { sum += map[nx][ny]; country.add(new Pair(nx, ny)); } } } } for (int i = 0; i < country.size(); i++) { Pair p = country.get(i); map[p.x][p.y] = sum / country.size(); } } public static boolean openBarrier() { boolean flag = false; for (int i = 0; i < row; i++) { for (int j = 0; j < row; j++) { if (i % 2 == 0 && j % 2 != 0) { int num = Math.abs(map[i][j - 1] - map[i][j + 1]); if (num >= L && num <= R) { flag = true; map[i][j] = 0; } else map[i][j] = Integer.MAX_VALUE; } else if (i % 2 != 0 && j % 2 == 0) { int num = Math.abs(map[i - 1][j] - map[i + 1][j]); if (num >= L && num <= R) { flag = true; map[i][j] = 0; } else map[i][j] = Integer.MAX_VALUE; } } } if (flag) return true; else return false; } static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } }
댓글을 작성하려면
로그인
해야 합니다.
bravery0730 3년 전
테스트케이스와 게시판에 올라온 반례 모두 맞지만 2%에서 틀렸다고 합니다ㅠ.. 반례 부탁드리겠습니다.