본문 바로가기

전공지식/알고리즘

(12)
[알고리즘][그래프][9466] 팀프로젝트 - URL : https://www.acmicpc.net/problem/9466 - 소스코드 import java.util.Scanner; class Main { static int[] graph; static int[] check; static int[] startVertex; static int dfs(int start, int cnt, int step) { if (check[start] != 0) { if (step != startVertex[start]) { // 이미 방문했고, 정점 시작점이 다를 경우 사이클 x return 0; } return cnt - check[start]; } check[start] = cnt; startVertex[start] = step; return dfs(grap..
[백준][11729] 하노이탑 이동 문제 URL = https://www.acmicpc.net/problem/11729 package hanoitop; import java.util.Scanner; public class HanoiTop { public static int count = 0;public static void main(String[] args){//1~20의 조건이 맞는 수int onePan = new Scanner(System.in).nextInt();StringBuilder sb = new StringBuilder();solve(onePan,1,3,sb);System.out.println(count);System.out.println(sb.toString());}/*소스해설... 위주로*/public static void so..
[알고리즘]Scanner vs BufferReader 콘솔 입출력에는 2가지 방법이 있다. 바로 BufferReader 와 Scanner 클래스를 이용하는 방법이다. 나온 순서는 BufferReader -> Scanner 순이다. (즉, 최신 클래스는 Scanner 클래스) [사용 방법] Scanner sc = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); [차이점] 1.버퍼 크기 차이 (Scanner는 1024 chars, BufferReader는 8192 chars ) 2.파싱의 차이 (BufferReader는 단순한 문자열을 읽고 저장하는 반면 Scanner는 파싱 기능이 있음) 3.동기화의 차이 (BufferReader는 ..
[백준][그래프][2667] 단지번호붙이기 import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class Pair{ int x; int y; Pair(int x, int y){ this.x = x; this.y = y; } } public class Graph { public static final int[] dx = {0, 0, 1, -1}; public static final int[] dy = {1, -1, 0, 0}; static void bfs(int[][] arr, int[][] group, int x, int y, int n, int cnt){ Queue queue = new LinkedList(..
[백준][DP][9465]스티커 URL : https://www.acmicpc.net/problem/9465 - 소스코드 public class Main { public static void main(String[] args) throws FileNotFoundException { //Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(new FileInputStream("input.txt")); int cases = sc.nextInt(); int[][] answerArr; int N; int[][] arr; while(cases-- > 0){ N=sc.nextInt(); arr=new int[2][N]; for(int i=0; i
[백준][DP][2011] 암호코드 - URL : https://www.acmicpc.net/problem/2011 - 소스코드 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static int mod = 1000000; public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); int length = ..
[백준][DP][2193] 이친수 URL : https://www.acmicpc.net/problem/2193 - 소스코드 · 2차원 다이나믹 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[][] dp = new long[n+1][2]; // 이친수는 0으로 시작하지 않는다. dp[1][1] = 1; for(int i=2; i d[i-2] for (int i=3; i
[백준][DP][11057] 오르막 수 URL : https://www.acmicpc.net/problem/11057 - 소스코드 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] dp = new int[n+1][10]; for(int i=0; i