자료구조와 알고리듬 With Java 70

Part 01. 연습문제 풀이 2 [zerobase]

1. 로마 숫자 표기를 정수로 바꾸기 - map을 이용해서 각각의 문자에 해당하는 값을 함께 넣어준다. - s를 chararray로 바꿔서 작은 값을 가지는 키가 앞에 오면 그 값을 마이너스하도록 sum 식을 세운다. - 가장 마지막 글자는 더한다. import java.util.HashMap; public class Practice1 { public static void solution(String s) { HashMap map = new HashMap(); map.put('I', 1); map.put('V', 5); map.put('X', 10); map.put('L', 50); map.put('C', 100); map.put('D', 500); map.put('M', 1000); int sum = ..

Part.01연습문제 풀이 [zerobase]

1. 숫자를 거꾸로 출력하기 package src; public class Practice1 { public static void solution(int num) { int answer = 0; if(0 == num) { System.out.println(answer); return; } String str = ""; if(num < 0) { num *= -1; while (0 != num) { str += num % 10; num /= 10; } answer = Integer.parseInt(str); System.out.println(-1 * answer); return; } while(0 != num){ str += num % 10; num /= 10; } answer = Integer.parseI..

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