250x250
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 |
Tags
- 자바 코테
- BFS
- 정보처리기사
- 완전탐색
- 재귀함수
- 백준2251
- 코테
- 중복조합
- 자바
- 전화번호속의암호
- java
- 에라토스테네스의채
- 23288
- D드라이브생성
- 정올 1620
- 백준15652
- 삼성역테
- 볼륨 만들기
- 백준13458
- 코테준비
- 알고리즘개념
- 백준
- N과M
- 순열
- Bfs와DFS
- 알고리즘
- 주사위굴리기2
- 파티션 크기 조정
- 완탐
- 중복순열
Archives
- Today
- Total
뚱땅뚱땅
[문제] SWEA 1238 Contact 본문
728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
내 풀이
BFS에서 레벨별로 노드를 나눠야한다.
public class SWEA_1238 {
static boolean[][] adjMatrix;
static int start;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
StringBuilder sb = new StringBuilder();
int T = 10;
for (int tc = 1; tc <= T; tc++) {
st = new StringTokenizer(in.readLine(), " ");
int N = Integer.parseInt(st.nextToken());// 입력받는 데이터의길이
start = Integer.parseInt(st.nextToken()); //
// 인접행렬
adjMatrix = new boolean[101][101]; // 1 based index
st = new StringTokenizer(in.readLine(), " ");
for (int i = 0; i < N/2; i++) {
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
adjMatrix[from][to] = true; // 연결시켜주기
}
int ans = bfs();
sb.append('#').append(tc).append(' ').append(ans).append('\n');
}
System.out.println(sb);
}
// 마지막 레벨에 있는 노드들 중 최대값 구하기
static int bfs() {
int ans = 0;
boolean[] visited = new boolean[101]; //방문배열
int size = 0; // 큐 크기
Queue<Integer> q = new LinkedList<>();
q.offer(start); // 시작점
visited[start] = true;
while (!q.isEmpty()) {
size = q.size();
int maxNode = 0;
// 같은 레벨에 대한 노드들 구분하기
while (--size >= 0) {
int curr = q.poll();
for(int i=1;i<=100;i++) {
if(adjMatrix[curr][i]&&!visited[i]) {
q.offer(i);
if(maxNode<i) maxNode = i; // 최대 노드번호 저장
visited[i] = true; // 방문 처리
}
}
}
if(maxNode>0)
ans = maxNode; //레벨별 최대 노드번호 갱신
}
return ans;
}
}
728x90
'알고리즘 > swea' 카테고리의 다른 글
[문제] SWEA 1249번 보급로 (0) | 2021.04.13 |
---|---|
[문제] SWEA 3307번 최장 증가 부분 수열 (0) | 2021.03.25 |
[문제] SWEA 1219번 길찾기 (0) | 2021.03.05 |
[문제] SWEA 1227번 미로2 (0) | 2021.03.05 |
[문제] SWEA 1486 장훈이의 높은 선반 (0) | 2021.03.05 |
Comments