자료구조와 알고리듬 With Java/[Study] BAEKJOON 프로그래머스 CodeUp LeetCode 40

Java로 BFS 구현하기

Ex) package first; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class BFSpractice0 { static int MAX_COUNT = 101; static int [][] AdjMatrix = new int [MAX_COUNT][MAX_COUNT]; static int[] VisitMatrix = new int[MAX_COUNT]; static int VertaxCount, EdgeCount; static Queue queue = new LinkedList(); public static void main(String[] args) { int StartV = 1; Scanne..

Code up 4060 전광판 전구 조작

Ex) /* 5 6 0 0 1 0 1 1 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 1 1 1 0 0 0 1 0 0 */ import java.util.Scanner; public class Billboard { static int M, N; static int MAX_COUNT = 501; static int[][] AdjMatrix = new int[MAX_COUNT][MAX_COUNT]; static int[][] VisitMatrix = new int[MAX_COUNT][MAX_COUNT]; static int[][] DirMatrix = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; static int DIRECTION = 4; static int cnt = 0; s..

Code up 4572 영역 구하기 / 오류 수정하기

Ex) package first; /* 5 7 3 0 2 4 4 1 1 2 5 4 0 6 2 */ import java.util.Scanner; public class CountTerritory { static int Width; static int Height; static int MAX_COUNT = 101; static int[][] VisitMatrix = new int[MAX_COUNT][MAX_COUNT]; static int[][] AdjMatrix = new int[MAX_COUNT][MAX_COUNT]; static int DIRECTION = 4; static int[][] DirMatrix = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; static int cnt ..

Code up 4714 BAEKJOON 2458 키 순서

Ex) 자신이 키가 몇 번째인지 알 수 있는 학생이 모두 몇 명인지를 출력한다. - 1번부터 N번까지 번호가 붙여져 있는 학생들에 대하여 두 학생끼리 키를 비교한 결과의 일부가 주어져 있다. - 단, N명의 학생들의 키는 모두 다르다고 가정한다. 예를 들어, 6명의 학생들에 대하여 6번만 키를 비교하였고, 그 결과가 다음과 같다고 하자. 1번 학생의 키 < 5번 학생의 키 3번 학생의 키 < 4번 학생의 키 5번 학생의 키 < 4번 학생의 키 4번 학생의 키 < 2번 학생의 키 4번 학생의 키 < 6번 학생의 키 5번 학생의 키 < 2번 학생의 키 - 이 비교 결과로부터 모든 학생 중에서 키가 가장 작은 학생부터 자신이 몇 번째인지 알 수 있는 학생들도 있고 그렇지 못한 학생들도 있다는 사실을 아래처럼 ..

Code up 2605 캔디팡

Ex) import java.util.Arrays; import java.util.Scanner; public class CandyPang { /* 7 2 1 5 1 1 3 4 2 1 5 1 3 5 3 2 3 4 5 2 2 4 4 4 3 2 3 1 3 4 3 5 3 1 4 3 5 4 4 3 3 5 5 2 1 3 5 1 1 2 */ static int uCnt = 0; static int Size = 7; static int[][] VisitMatrix = new int[ 200][200]; static int[][]AdjMatrix = new int[200][200]; static int DIRECTION = 4; static int[][] DirMatrix = {{-1, 0}, {1, 0}, {0, -..

DFS 촌수 찾기 (오류 수정 전)

Ex) import java.util.Scanner; public class DegreeOfKinship { /* 입력코드 9 7 3 7 1 2 1 3 2 7 2 8 2 9 4 5 4 6 출력 3 */ static int MAX_COUNT = 101; static int VCnt; static int StartVertax; static int TargetVertax; static int flag; static int[][] AdjMatrix = new int[MAX_COUNT][MAX_COUNT]; static int[] VisitMarix = new int[MAX_COUNT]; static int ECnt, uCnt = 0; public static void main(String[] args) { Sc..

친구 찾기

Ex) import java.util.Scanner; public class Friend { static int MAX_COUNT = 56; static int[][] g_nAdjMatrix = new int[MAX_COUNT][MAX_COUNT]; static int[] g_nVisitMatrix = new int[MAX_COUNT]; static int g_EdgeCnt; static int g_StartVertax, g_FisishVertax, g_Flag = 0; static int change; public static void main(String[] args) { int V1, V2; char trash, ch1, ch2; Scanner scan = new Scanner(System.in);..

Codeup 4421 BaekJoon 2667 단지번호 붙이기

Ex) 첫 번째 줄에는 총 단지수를 출력하시오. 그리고 각 단지내 집의 수를 오름차순으로 정렬하여 한 줄에 하나씩 출력하시오 - 과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. - 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. - 여기서 연결되었다는 것은 어떤 집이 좌우, 혹은 아래위로 다른 집이 있는 경우를 말한다. - 대각선상에 집이 있는 경우는 연결된 것이 아니다. 는 을 단지별로 번호를 붙인 것이다. - 지도를 입력하여 단지수를 출력하고, 각 단지에 속하는 집의 수를 오름차순으로 정렬하여 출력하는 프로그램을 작성하시오. - 첫 번째 줄에는 지도의 크기 N(정사각형이므로 가로와 세로의 크기는 같으며 5≤N≤2..

Code up 4024 호수의 수 구하기

Ex) 주어진 지도를 분석하여 호수의 개수를 구하는 프로그램을 작성하시오. - 다음은 어느 호수 지역의 지도를 간략하게 표현한 것이다. 지도는 'L'또는 '.'으로 이루어 진다. - 'L'이 의미하는 것은 호수 영역이고 '.'은 호수 이외의 영역을 의미한다. - 단, 호수가 상, 하, 좌, 우, 대각선으로 이어진 것은 하나의 호수로 간주한다. L . . . . . . . . L . . . L . . . . . . . L L . L L . . . . . . . . L . . L . . . . . . . . . L . . L . . . . . . . . L . . . . . . L . . . . . . . . . . L . L . . . . . . . . L . L . L . . . . . . . . L ...

Java로 BFS 구현하기

Ex) BFS 출력하기 - vertaxCount(노드의 개수), edgeCount(간선의 개수), startVertax(시작 노드), targerVertax(목적지 노드)를 차례대로 입력 받는다. - 그 다음에는 엣지의 순서쌍을 차례대로 입력 받는다. - 방문 순서대로 list에 넣고 list에 출력한다. import java.util.*; public class BFS1 { static int vertaxCount; static int edgeCount; static int targetVertax; static int startVertax; static Queue queue = new LinkedList(); static ArrayList list = new ArrayList(); static bool..