회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
도통 어디서 틀린지 모르겠습니다;
3197번 - 백조의 호수
ghdtmdwo123
4년 전
0
어디서 틀렸는지 감이 안옵니다;
package algorithm; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; public class b3197 { static int N; static int M; static int b1; static int b2; static int c1; static int c2; static int count; static boolean check; static boolean check2; static int mid; static Queue<dot7> q = new LinkedList<dot7>(); static int dx[] = { 1, 0, -1, 0 }; static int dy[] = { 0, -1, 0, 1 }; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str[] = br.readLine().trim().split(" "); N = Integer.parseInt(str[0]); M = Integer.parseInt(str[1]); char map[][] = new char[N][M]; boolean check3 = false; for (int i = 0; i < N; i++) { char temp[] = br.readLine().toCharArray(); for (int j = 0; j < M; j++) { if(temp[j] == '.') { map[i][j] = '0'; }else { map[i][j] = temp[j]; } if (temp[j] == 'L') { if (!check3) { b1 = i; b2 = j; check3 = true; } else { c1 = i; c2 = j; } } } } //while (!check) { boolean globalvisited[][] = new boolean[N][M]; check =true; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (map[i][j]-'0' == count) { q.add(new dot7(i, j)); } } } bfs(map, globalvisited); /* for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { System.out.print(map[i][j]); } System.out.println(); }*/ int start = 0; int end = count - 1; while(start <= end) { check2 = false; mid = (start + end)/2; q.add(new dot7(b1, b2)); bfs(map); if(check2 == true) { end = mid-1; }else { start = mid+1; } } System.out.print(start); } // main static void bfs(char map[][]) { boolean visited[][] = new boolean[N][M]; while (!q.isEmpty()) { dot7 d = q.poll(); int x = d.x; int y = d.y; if (x == c1 && y == c2) { check2 = true; } for (int i = 0; i < 4; i++) { int dx2 = x + dx[i]; int dy2 = y + dy[i]; if (dx2 >= N || dy2 >= M || dx2 < 0 || dy2 < 0) { continue; } if (visited[dx2][dy2] ) { continue; } if( map[dx2][dy2] == 'L') { visited[dx2][dy2] = true; q.add(new dot7(dx2,dy2)); continue; } if ( map[dx2][dy2]-'0' > mid ) { continue; } visited[dx2][dy2] = true; q.add(new dot7(dx2, dy2)); } } } static void bfs(char map[][], boolean globalvisited[][]) { boolean visited[][] = new boolean[N][M]; while (!q.isEmpty()) { int size = q.size(); count++; for(int k = 0 ; k < size; k++) { dot7 d = q.poll(); int x = d.x; int y = d.y; visited[x][y] = true; for (int i = 0; i < 4; i++) { int dx2 = x + dx[i]; int dy2 = y + dy[i]; if (dx2 >= N || dy2 >= M || dx2 < 0 || dy2 < 0) { continue; } if ( map[dx2][dy2] == 'L' || visited[dx2][dy2]) { continue; } if (map[dx2][dy2] == 'X') { visited[dx2][dy2] = true; char a = Character.forDigit(count, 10); map[dx2][dy2] = a; q.add(new dot7(dx2, dy2)); continue; } visited[dx2][dy2] = true; q.add(new dot7(dx2, dy2)); } } } // while } } // class class dot7 { int x; int y; dot7(int x, int y) { this.x = x; this.y = y; } }
댓글을 작성하려면
로그인
해야 합니다.
ghdtmdwo123 4년 전
어디서 틀렸는지 감이 안옵니다;