회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
뭐가문제인지 모르겠어요
7562번 - 나이트의 이동
jjhsun99
2년 전
0
질문게시판에 있는 반례를 다 적용해도 맞는데 왜..틀리지
public class B7562 { static int[][] graph; static boolean[][] visited; static int dx[] = {-2,-2,-1,1,2,2,-1,1}; static int dy[] = {1,-1,2,2,-1,1,-2,2}; static int n; static int bfs(int x,int y,int x1, int y1) { Queue<int []> queue = new LinkedList<>(); queue.add(new int[]{x, y, 0}); // The third value in the array is the count of moves while (!queue.isEmpty()) { int now[] = queue.poll(); visited[now[0]][now[1]]=true; if (now[0] == x1 && now[1] == y1) { return now[2]; // Return the count when the target is reached } for (int i = 0; i < 8; i++) { int nx = now[0]+dx[i]; int ny = now[1]+dy[i]; if (nx >= 0 && ny >= 0 && nx < n && ny < n && !visited[nx][ny]) { visited[nx][ny]=true; queue.add(new int[]{nx, ny, now[2] + 1}); } } } return -1; // 해당 테스트 케이스의 이동 횟수를 반환 } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int tc = Integer.parseInt(br.readLine()); for (int t=0; t < tc; t++) { n = Integer.parseInt(br.readLine()); graph = new int[n][n]; visited = new boolean[n][n]; StringTokenizer st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); graph[x][y]=1; st = new StringTokenizer(br.readLine()); int x1 = Integer.parseInt(st.nextToken()); int y1 = Integer.parseInt(st.nextToken()); graph[x1][y1]=1; int count = bfs(x, y, x1, y1); // 해당 테스트 케이스의 이동 횟수를 얻음 System.out.println(count); } } }
댓글을 작성하려면
로그인
해야 합니다.
jjhsun99 2년 전
질문게시판에 있는 반례를 다 적용해도 맞는데 왜..틀리지