본문 바로가기

Java/Java 알고리즘 모음

[Java 알고리즘] 8. DFS/BFS 모음 (+백트래킹)

반응형

DFS

https://cote.inflearn.com/contest/10/problem/08-01

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/647

 

[Ch.08 - DFS] 03. 합이 같은 부분집합

1. 합이 같은 부분집합(DFS : 아마존 인터뷰) 설명 N개의 원소로 구성된 자연수 집합이 주어지면, 이 집합을 두 개의 부분집합으로 나누었을 때 두 부분집합의 원소의 합이 서로 같은 경우가 존재

and-some.tistory.com

 

 

https://cote.inflearn.com/contest/10/problem/08-02

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/648

 

[Ch.08 - DFS] 04. 바둑이 승차

2. 바둑이 승차(DFS) 설명 철수는 그의 바둑이들을 데리고 시장에 가려고 한다. 그런데 그의 트럭은 C킬로그램 넘게 태울수가 없다. 철수는 C를 넘지 않으면서 그의 바둑이들을 가장 무겁게 태우고

and-some.tistory.com

 

https://cote.inflearn.com/contest/10/problem/08-03

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/649

 

[Ch.08 - DFS] 05. 최대점수 구하기

3. 최대점수 구하기(DFS) -> 냅색 알고리즘으로 풀기 https://and-some.tistory.com/663 [Ch.10 - DP] 06. 최대점수 구하기 [+냅색 알고리즘] 6. 최대점수 구하기(냅색 알고리즘) 설명 이번 정보올림피아드대회에

and-some.tistory.com

 

 

https://cote.inflearn.com/contest/10/problem/08-05

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/651

 

[Ch.08 - DFS] 07. 동전교환 #

5. 동전교환 -> 냅색 알고리즘으로 풀기 https://and-some.tistory.com/662 [Ch.10 - DP] 05. 동전교환 [+냅색 알고리즘] 5. 동전교환(냅색 알고리즘) 냅색 알고리즘 : 담을 수 있는 무게가 정해진 백팩에 가장 비

and-some.tistory.com

 

#순열 공식

	static int[] pm, ch, arr;
	// pm: 결과 배열, ch : 중복 체크 배열, arr : 값 배열

	public void DFS(int L) {
		// 3 2-> 3 6 9 -> 3 6, 3 9, 6 3, 6 9, 9 3, 9 6
		if (L == m) {
			// 한 개의 순열 완성
			for (int x : pm) {
				System.out.print(x + " ");
			}
			System.out.println();
		} else {
			for (int i = 0; i < n; i++) {
				if (ch[i] == 0) {
					// 중복체크 하면서 값 배열에서 가져오기
					ch[i] = 1;
					// 사용하기 전 체크리스트에 1로 변경
					pm[L] = arr[i];
					// 값 배열에서 가져와 결과 배열에 넣기
					DFS(L + 1);
					// 기준 값 다음 값 가져오기
					ch[i] = 0;
					// 사용 후 체크리스트 0으로 초기화
				}
			}
		}
	}

 

https://and-some.tistory.com/652

 

[Ch.08 - DFS] 08. 순열 구하기

순열 구하기 10이하의 N개의 자연수가 주어지면 이 중 M개를 뽑아 일렬로 나열하는 방법을 모두 출력합 니다. 입력설명 첫 번째 줄에 자연수 N(3

and-some.tistory.com

 

 

https://cote.inflearn.com/contest/10/problem/08-07

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/653

 

[Ch.08 - DFS] 09. 조합의 경우수(메모이제이션)

7. 조합의 경우수(메모이제이션) 설명 로 계산합니다. 하지만 여러분은 이 공식을 쓰지않고 다음 공식을 사용하여 재귀를 이용해 조합수를 구해주는 프로그램을 작성하세요. 입력 첫째 줄에 자

and-some.tistory.com

 

 

https://cote.inflearn.com/contest/10/problem/08-08

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/654

 

[Ch.08 - DFS] 10. 수열 추측하기 #

8. 수열 추측하기 설명 가장 윗줄에 1부터 N까지의 숫자가 한 개씩 적혀 있다. 그리고 둘째 줄부터 차례대로 파스칼의 삼각형처럼 위의 두개를 더한 값이 저장되게 된다. 예를 들어 N이 4 이고 가

and-some.tistory.com

 

 


 

 

BFS

 

https://cote.inflearn.com/contest/10/problem/07-08

 

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/644

 

[Ch.07 - BFS] 02. 송아지 찾기 (+BFS : 상대트리탐색)

8. 송아지 찾기 1(BFS : 상태트리탐색) 설명 현수는 송아지를 잃어버렸다. 다행히 송아지에는 위치추적기가 달려 있다. 현수의 위치와 송아지의 위치가 수직선상의 좌표 점으로 주어지면 현수는

and-some.tistory.com

 

 

[Ch.07 - BFS] 02. 송아지 찾기 (+BFS : 상대트리탐색)

8. 송아지 찾기 1(BFS : 상태트리탐색) 설명 현수는 송아지를 잃어버렸다. 다행히 송아지에는 위치추적기가 달려 있다. 현수의 위치와 송아지의 위치가 수직선상의 좌표 점으로 주어지면 현수는

and-some.tistory.com

 

https://cote.inflearn.com/contest/10/problem/08-11

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/646

 

[Ch.08 - BFS] 03. 미로의 최단거리 통로

11. 미로의 최단거리 통로(BFS) 설명 7*7 격자판 미로를 탈출하는 최단경로의 길이를 출력하는 프로그램을 작성하세요. 경로의 길이는 출발점에서 도착점까지 가는데 이동한 횟수를 의미한다. 출

and-some.tistory.com

 

https://cote.inflearn.com/contest/10/problem/08-12

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/656

 

[Ch.08 - BFS] 04. 토마토

12. 토마토(BFS 활용) 설명 현수의 토마토 농장에서는 토마토를 보관하는 큰 창고를 가지고 있다. 토마토는 아래의 그림과 같이 격자 모양 상자의 칸에 하나씩 넣어서 창고에 보관한다. 창고에 보

and-some.tistory.com

 

 


DFS/BFS

 

 

https://cote.inflearn.com/contest/10/problem/08-13

 

OnlineJudge

 

cote.inflearn.com

https://and-some.tistory.com/657

 

[Ch.08 - DFS / BFS] 13. 섬나라 아일랜드 #

13. 섬나라 아일랜드 설명 N*N의 섬나라 아일랜드의 지도가 격자판의 정보로 주어집니다. 각 섬은 1로 표시되어 상하좌우와 대각선으로 연결되어 있으며, 0은 바다입니다. 섬나라 아일랜드에 몇

and-some.tistory.com


 

https://leetcode.com/problems/number-of-islands/

 

Number of Islands - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/909

 

[LeetCode- Ch7. DFS & BFS] 1. 섬의 수

https://leetcode.com/problems/number-of-islands/ Number of Islands - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. BFS 이용 -> 큐 ->

and-some.tistory.com

 

 

https://leetcode.com/problems/max-area-of-island/submissions/

 

Max Area of Island - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/911

 

[LeetCode- Ch7. DFS & BFS] 3. 섬의 최대 면적

https://leetcode.com/problems/max-area-of-island/submissions/ Max Area of Island - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 섬의

and-some.tistory.com

 

https://leetcode.com/problems/word-ladder/

 

Word Ladder - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/912

 

[LeetCode- Ch7. DFS & BFS] 4. 단어 사다리 #

https://leetcode.com/problems/word-ladder/ Word Ladder - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 2. DFS 시도 3. BFS 시도 DFS 시

and-some.tistory.com

 

 

https://leetcode.com/problems/word-search/

 

Word Search - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/913

 

[LeetCode- Ch7. DFS & BFS] 5. 단어 검색 #

https://leetcode.com/problems/word-search/ Word Search - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 원하는 단어를 배열에서

and-some.tistory.com

 

 

https://leetcode.com/problems/remove-invalid-parentheses/

 

Remove Invalid Parentheses - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/914

 

[LeetCode- Ch7. DFS & BFS] 6. 유효하지 않은 괄호 제거

https://leetcode.com/problems/remove-invalid-parentheses/ Remove Invalid Parentheses - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1.

and-some.tistory.com

 

https://www.lintcode.com/problem/787/

 

LintCode 炼码

 

www.lintcode.com

https://and-some.tistory.com/915

 

[LeetCode- Ch7. DFS & BFS] 7. 구슬 미로

https://www.lintcode.com/problem/787/ LintCode 炼码 www.lintcode.com BFS 기본 방향 설정 & 배열 사이즈 변수 설정 맞는 조건 탐색 큐 생성 조건 체크해서 큐 넣기 마이너스 좌표 체크 m*n 범위 체크 grid[x][y]값 체

and-some.tistory.com

 


 

https://leetcode.com/problems/path-with-maximum-gold/submissions/

 

Path with Maximum Gold - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/930

 

 

[LeetCode- Part. 1] 7. 금광 찾기 #

https://leetcode.com/problems/path-with-maximum-gold/submissions/ Path with Maximum Gold - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

and-some.tistory.com

 

 

https://and-some.tistory.com/936

 

[LeetCode- Part. 2] 5. 음식을 구하기위한 최단 경로 (+BFS)

package Part2.Q6; import java.util.LinkedList; import java.util.Queue; public class Solution { public static void main(String[] args) { char[][] grid = { { 'X', 'X', 'O', 'O', 'O', 'X' }, { 'X', 'I', 'O', 'X', 'O', 'X' }, { 'X', 'O', 'O', 'X', 'F', 'X' },

and-some.tistory.com

 

https://leetcode.com/problems/number-of-provinces/

 

Number of Provinces - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/946

 

[LeetCode- Part. 3] 5. Province의 수 (+DFS)

https://leetcode.com/problems/number-of-provinces/ Number of Provinces - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 연결된 도시

and-some.tistory.com

 

 

https://leetcode.com/problems/shortest-path-in-binary-matrix/

 

Shortest Path in Binary Matrix - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/952

 

[LeetCode- Part. 4] 5. 이진 매트릭스 최단경로 (+BFS)

https://leetcode.com/problems/shortest-path-in-binary-matrix/ Shortest Path in Binary Matrix - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.c

and-some.tistory.com


 

백트래킹

 

https://leetcode.com/problems/generate-parentheses/

 

Generate Parentheses - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/920

 

[LeetCode- Ch9. 백트래킹] 1. 생성된 괄호 #

https://leetcode.com/problems/generate-parentheses/ Generate Parentheses - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 특정 조건

and-some.tistory.com

 

 

https://leetcode.com/problems/permutations/

 

Permutations - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/921?category=1011285 

 

[LeetCode- Ch9. 백트래킹] 2. 순열 #

https://leetcode.com/problems/permutations/ Permutations - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 배열에 [1,2,3]이 존재한

and-some.tistory.com

 

 

https://leetcode.com/problems/subsets/submissions/

 

Subsets - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/922

 

[LeetCode- Ch9. 백트래킹] 3. 부분집합

https://leetcode.com/problems/subsets/submissions/ Subsets - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 2^n개의 부분집합을

and-some.tistory.com

 

 

https://leetcode.com/problems/letter-combinations-of-a-phone-number/submissions/

 

Letter Combinations of a Phone Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

https://and-some.tistory.com/923

 

[LeetCode- Ch9. 백트래킹] 4. 핸드폰 숫자의 문자 조합

https://leetcode.com/problems/letter-combinations-of-a-phone-number/submissions/ Letter Combinations of a Phone Number - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your

and-some.tistory.com

 

 

 

반응형