BFS 14

2022-08-28 [11회차] 알고리즘 스터디

1. 팔 - 실버1 08-28 일 출처 https://www.acmicpc.net/problem/1105 1105번: 팔 첫째 줄에 L과 R이 주어진다. L은 2,000,000,000보다 작거나 같은 자연수이고, R은 L보다 크거나 같고, 2,000,000,000보다 작거나 같은 자연수이다. www.acmicpc.net MySol) ======= 미해결 코드 ======== import java.util.Scanner; public class Test { static int findEight(String s){ int eightCnt = 0; for(char c : s.toCharArray()) { if (c == '8') { ++eightCnt; } } return eightCnt; } public s..

[3주차] Coding Test 해시 / BFS / DFS / 동적계획법

1. 위장 import java.util.*; class Solution { public static int solution(String[][] clothes) { Map map = new HashMap(); int answer = Arrays.stream(clothes) // 모든 옷의 종류에 대해 .map(c->c[1]) // 각 type은 1번 index에 있는 값이다. (종류만 얻어와서) .distinct() // 중복을 제거한다. (타입을 얻어 온다.) .map(type -> (int) Arrays.stream(clothes).filter(c-> c[1].equals(type)).count()) // 타입에 해당하는 것들만 필터링해서 카운트 얻어 온다. .map(c -> c + 1) // 타입 ..

Backjoon 7576 BFS 토마토

Ex) 며칠 후에 상자 안의 토마토가 모두 익는지 구하라. 창고에 보관되는 토마토들 중에는 잘 익은 것도 있지만, 아직 익지 않은 토마토들도 있을 수 있다. 보관 후 하루가 지나면, 익은 토마토들의 인접한 곳에 있는 익지 않은 토마토들은 익은 토마토의 영향을 받아 익게 된다. 하나의 토마토의 인접한 곳은 왼쪽, 오른쪽, 앞, 뒤 네 방향에 있는 토마토를 의미한다. 대각선 방향에 있는 토마토들에게는 영향을 주지 못하며, 토마토가 혼자 저절로 익는 경우는 없다고 가정한다. 철수는 창고에 보관된 토마토들이 며칠이 지나면 다 익게 되는지, 그 최소 일수를 알고 싶어 한다. 토마토를 창고에 보관하는 격자모양의 상자들의 크기와 익은 토마토들과 익지 않은 토마토들의 정보가 주어졌을 때, 며칠이 지나면 토마토들이 모두..

BFS 미로 찾기 (small)

Ex) /* 5 5 #S### #...# #.#.# #.... ###G# */ import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Maze { static int MAX_COUNT = 101; static char [][] AdjMatrix = new char[MAX_COUNT][MAX_COUNT]; static int [][] VisitMatrix = new int[MAX_COUNT][MAX_COUNT]; static int Width; static int Height; static int DIRECTION = 4; static int [][] DirMatrix = {{-1, 0}, {1, ..

BFS 알리바바 미로

Ex) package first; /* [입력] 5 5 10110 00010 01010 01000 00011 [출력] 9 */ import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class AlibabaMaze { static int MAX_COUNT = 101; static int DIRECTION = 4; static int[][] DirMatrix = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; static int ColumnCnt; static int RowCnt; static int cnt = 0; static int[][] AdjMatrix = new int[MAX_COUN..

BFS 마법의 비료

/* 5 4 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 1 */ package first; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class MagiFertilizer0 { static int MAX_COUNT = 101; static int [][] AdjMatrix = new int[MAX_COUNT][MAX_COUNT]; static int [][] VisitMatrix = new int[MAX_COUNT][MAX_COUNT]; static int Width; static int Height; static int DIRECTION = 4; static int [..

Java 2차원 맵 탐색을 위한 BFS

package first; import java.security.KeyPair; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.lang.*; public class TwoDimensionDFS { static int MAX_COUNT = 101; static int DIRECTION = 4; static int[][] DirMatrix = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; static int ColumnCnt; static int RowCnt; static int cnt = 0; static int[][] Array = new int[MAX_COUNT][2];..

Code up 4503 바이러스 (BFS)

Ex) import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Set; 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(); static Set set = new HashSet()..

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..

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..