회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
44%에서 계속 틀립니다 ㅠㅠ 반례 찾아주시면 감사하겠습니다
25307번 - 시루의 백화점 구경
ansdnd1500
1년 전
0
틀린점이나 반례 찾아주시면 감사하겠습니다
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { static int[] nextx = {1,-1,0,0}; static int[] nexty = {0,0,1,-1}; static boolean visited[][]; static int [][] distance; static int [][] map; static int N; static int M; static int K; static int O; public static void BFS(int x, int y) { int count = 0; int [] start = {x,y,count}; Queue<int[]> queue = new LinkedList<>(); queue.add(start); while(!queue.isEmpty()) { int[] now = queue.poll(); int nowx = now[0]; int nowy = now[1]; int count1 = now[2]; if(map[nowx][nowy] == 2) { System.out.print(count1); return; } for(int i =0; i<4;i++) { int next_x = nowx + nextx[i]; int next_y = nowy + nexty[i]; if(next_x <0 || next_y < 0|| next_x>=N || next_y>=M) { continue; } if(map[next_x][next_y] == 1 || visited[next_x][next_y] || map[next_x][next_y] == 3) continue; visited[next_x][next_y] = true; queue.add(new int[] {next_x, next_y,count1+1}); } } } public static void DistanceBFS(Queue<int[]> queue) { while(!queue.isEmpty()) { int now[] = queue.poll(); int nowx = now[0]; int nowy = now[1]; if(map[nowx][nowy] == 2) { O--; } map[nowx][nowy] = 1; for(int i=0; i<4;i++) { int next_x = nowx + nextx[i]; int next_y = nowy + nexty[i]; if(next_x<0 || next_y<0 || next_x>=N || next_y>=M) continue; if(distance[nowx][nowy] > distance[next_x][next_y]) continue; if(distance[nowx][nowy] < K) { queue.add(new int[] {next_x, next_y}); distance[next_x][next_y] = distance[nowx][nowy] + 1; } } } } public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st= new StringTokenizer(br.readLine() + " "); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); K = Integer.parseInt(st.nextToken()); map = new int[N][M]; visited = new boolean[N][M]; int x = 0; int y = 0; distance = new int[N][M]; Queue<int[]> maneking = new LinkedList<>(); for(int i =0; i<N;i++) { StringTokenizer st1 = new StringTokenizer(br.readLine()); for(int j =0; j<M;j++) { map[i][j] = Integer.parseInt(st1.nextToken()); distance[i][j] = Integer.MAX_VALUE; if(map[i][j] == 4) { x = i; y = j; } else if(map[i][j] == 2) { O++; } else if(map[i][j] == 3) { maneking.add(new int[] {i,j}); distance[i][j] = 0; } } } DistanceBFS(maneking); if(O == 0) System.out.print("-1"); else { BFS(x, y); } } }
댓글을 작성하려면
로그인
해야 합니다.
ansdnd1500 1년 전
틀린점이나 반례 찾아주시면 감사하겠습니다