회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
반례를 찾아주세요!
4179번 - 불!
kyh5037
1년 전
0
질문게시판에 있는 반례들을 넣었을 때는 다 출력이 되는데, 무엇이 문제일까요?
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { static class Node { int x; int y; char place; int count; Node (int x, int y, char place, int count) { this.x = x; this.y = y; this.place = place; this.count = count; } } static int[] dirX = {0, 1, 0, -1}; static int[] dirY = {1, 0, -1, 0}; static char[][] map; static int R; static int C; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); R = Integer.parseInt(st.nextToken()); C = Integer.parseInt(st.nextToken()); map = new char[R][C]; int sX = 0; int sY = 0; int fX = 0; int fY = 0; for (int i = 0; i < R; i++) { String str = br.readLine(); for (int j = 0; j < C; j++) { map[i][j] = str.charAt(j); if (map[i][j] == 'J') { sX = i; sY = j; } else if (map[i][j] == 'F') { fX = i; fY = j; } } } int result = BFS(sX, sY, fX, fY); System.out.println(result != -1 ? result + 1 : "IMPOSSIBLE"); } static int BFS(int sX, int sY, int fX, int fY) { Queue<Node> queue = new LinkedList<>(); boolean[][] sVisited = new boolean[R][C]; boolean[][] fVisited = new boolean[R][C]; queue.offer(new Node(sX, sY, 'J', 0)); queue.offer(new Node(fX, fY, 'F', 0)); sVisited[sX][sY] = true; fVisited[fX][fY] = true; while(!queue.isEmpty()) { Node cur = queue.poll(); if (fVisited[cur.x][cur.y] && cur.place == 'J') { continue; } if ((cur.x == 0 || cur.x == R - 1 || cur.y == 0 || cur.y == C - 1) && map[cur.x][cur.y] == '.' && cur.place == 'J') { return cur.count; } for (int i = 0; i < 4; i++) { int nextX = cur.x + dirX[i]; int nextY = cur.y + dirY[i]; if (nextX >= 0 && nextX < R && nextY >= 0 && nextY < C && map[nextX][nextY] != '#') { if (cur.place == 'J') { if (map[nextX][nextY] == 'F') continue; if (!fVisited[nextX][nextY] && !sVisited[nextX][nextY]) { sVisited[nextX][nextY] = true; queue.offer(new Node(nextX, nextY, 'J', cur.count + 1)); } } else { if (!fVisited[nextX][nextY]) { fVisited[nextX][nextY] = true; queue.offer(new Node(nextX, nextY, 'F', 0)); } } } } } return -1; } }
jun83
1년 전
0
반례 드려요
1 5 J##F# ans 1 3 1 . J F ans 1
댓글을 작성하려면
로그인
해야 합니다.
kyh5037 1년 전
질문게시판에 있는 반례들을 넣었을 때는 다 출력이 되는데, 무엇이 문제일까요?