목록Data Structure & Algorithm/PS - JAVA (270)
Dev.baelanche

캠핑장 이용일 수를 최대로 하기 위해, 전체 휴가 수에서 캠핑 이용일 수를 빼고 캠핑장 이용 불가능 한 수를 번갈아 빼며 진행한다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int c = 1; while(true) { int l = sc.nextInt(); int p = sc.nextInt(); int v = sc.nextInt(); if(l == 0 && p == 0 && v == 0) break; int use = 0; while(true) { if(v >= l) { v -= l; use += l; } else { use += v; v = 0; } v -= p - l;..

일반적인 BFS 탐색에서 조건을 잘 주면 된다. A 버튼을 눌렀을때는 99999를 넘지않게한다. B 버튼을 눌러 2배가 되었을때 99999가 넘는다면 그 경우는 통과한다. public class Main { static int t; static int g; static int max = 100000; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); t = sc.nextInt(); g = sc.nextInt(); int ans = bfs(n); System.out.println(ans == -1 ? "ANG" : ans); } public static int bfs(int n)..

주어진 수에 6이 포함되면 5로 바꿔 최소값을 합치고, 5가 포함되면 6으로 바꿔 최대값끼리 합치면 된다. A, B 를 어떤 형태로 받던 상관없이 수만 잘 바꾸어주면 된다. 아래 코드에서는 정수로 받아 10으로 나누면서 각 자리수에 접근했다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println((makeMin(a) + makeMin(b)) + " " + (makeMax(a) + makeMax(b))); } public static int makeMin(int a) { St..

1차원 배열에서 BFS 순회한다. 수빈이는 뒤로도 이동할 수 있으므로 동생과 못 만날 경우는 없다. 이동 조건이 {-1, 1, 2*x} 인 것만 유의하자. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int a[] = new int[100001]; boolean visited[] = new boolean[100001]; Queue queue = new LinkedList(); queue.offer(n); visited[n] = true; a[n] = 0; while(!queue.isEmpty()) ..

1차원 배열에서 방문 이력을 기록하면서 이동하는 문제이다. 변수 D 를 음수로 쓰는것만 까먹지 않으면 된다. public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int f = sc.nextInt(); int s = sc.nextInt(); int g = sc.nextInt(); int u = sc.nextInt(); int d = sc.nextInt(); Queue queue = new LinkedList(); int a[] = new int[f+1]; boolean visited[] = new boolean[f+1]; int mx[] = {u, -d}; boolean success..

여타 BFS 문제처럼 1에서 확장하고 -1은 벽으로 취급하여 푼다. 안익은 토마토(0)이 없을때의 카운트를 출력해준다. public class Main { static int m; static int n; static int a[][]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); m = sc.nextInt(); n = sc.nextInt(); a = new int[n][m]; for(int i=0; i

2차원 배열에서 BFS 를 순회하는데 벽을 한 번 부술 수 있다. 방문 정보 배열을 2층으로 구성하여 벽을 부수고 지나갔을 때의 정보와 벽을 안부쉈을때의 정보를 따로 기록한다. 큐에도 벽 부수기에 대한 정보를 담아 한번 부수면 더이상 부술수 없게 한다. public class Main { static int n; static int m; static int a[][]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); a = new int[n][m]; for(int i=0; i

이 문제와 로직이 같은 문제이다. public class Main { static int r; static int c; static char a[][]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); r = sc.nextInt(); c = sc.nextInt(); a = new char[r][c]; for(int i=0; i