Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- 코테
- dfs
- 건강
- priorityqueue
- 러닝일지
- 문제해결
- 투포인터
- Java
- 자바
- greedy
- 문제풀이
- 이분탐색
- 프로그래머스
- 문자열
- 백준
- 알고리즘
- binary search
- math
- 시뮬레이션
- BOJ
- 코딩테스트
- SWEA
- BFS
- DP
- SQL
- COS PRO
- 운동기록
- MySQL
- oracle
- db
Archives
- Today
- Total
슈콩
[BOJ] 2664 촌수계산 본문
[문제]
https://www.acmicpc.net/problem/2644
[소스 코드]
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
boolean[][] connect = new boolean[n+1][n+1];
int m = Integer.parseInt(br.readLine());
for(int i=0;i<m;i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
connect[a][b] = connect[b][a] = true;
}
Queue<int[]> q = new LinkedList<>();
boolean[] visit = new boolean[n+1];
q.offer(new int[] {x,0});
visit[x] = true;
while(!q.isEmpty()) {
int[] curr = q.poll();
if(curr[0] == y) {
System.out.println(curr[1]);
return;
}
for(int i=1;i<=n;i++) {
if(connect[curr[0]][i] && !visit[i]) {
q.offer(new int[] {i,curr[1]+1});
visit[i] = true;
}
}
}
System.out.println(-1);
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 1697 숨바꼭질 (0) | 2026.01.28 |
|---|---|
| [BOJ] 7569 토마토 (0) | 2026.01.26 |
| [BOJ] 2667 단지번호붙이기 (0) | 2026.01.22 |
| [BOJ] 2606 바이러스 (0) | 2026.01.20 |
| [BOJ] 2178 미로 탐색 (0) | 2026.01.09 |