11822번 Document
바로 한글변환...
https://www.acmicpc.net/problem/11822
11822번: Document
In the first example, the document must be signed by two officials — the first one accept documents on Mondays, Wednesdays and Fridays, and the second one — on Mondays and Thursdays. The fastest way to sign the document is one of the following: you s
www.acmicpc.net
이 문제를 처음에 이해하기로는 그냥 어느 공무원이든 모두 일처리를 한번씩만 하면 되는 것으로 착각하고 풀었는데, 접근방식이 틀렸었다.
-----
hint
3명의 공무원이 있다면 첫번째 공무원부터 부터 계단식으로 서류통과를 하는 방식이 맞는 방식이다.
입력케이스 하나를 예로 들었을 때
2
0 1 0 0 1
1 1 0 0 0
각각 배열로 선언을 했다 쳤을 때 두번째 줄 a배열, 세번째 줄 b배열로 치면
a[1] 인덱스에서 1을 발견하고 카운트, 이후 b배열로 이동해서 b[2] -> b[3] -> b[4] ->b[0] 으로 이동을 해서 1을 발견한 b[1]
자리에서 멈춘다 그렇다면 이미 한 주의 사이클이 돌았으니 5일 +2일 + 두번째 주의 1일 해서 8이 나오게 된다
-----
solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import java.util.*; import java.io.*; public class Main { static int count = 0; static int length = 0; //static boolean bool[] = new boolean[5]; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); int[][] al = new int[n][5]; for (int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); for (int j = 0; j < 5; j++) { al[i][j] = Integer.parseInt(st.nextToken()); } } length = n; goRepeat(al, 0); } static void goRepeat(int[][] a, int n) { if (n == length) { if (count % 5 == 0 && count / 5 > 0) { System.out.println(count + ((count / 5 - 1) * 2)); return; } else { System.out.println(count + (count / 5 * 2));//5일 지날때마다 +2 return; } } go(a[n], count % 5); n++; goRepeat(a, n); return; } static void go(int a[], int index) { for (int i = 0; i < 5; i++) { count++; if (a[index] == 1) { return; } index = (index + 1) % 5; } } } | cs |
실행시간 76ms
처음엔 배열로 풀려고 하다가 재귀함수를 학습하고싶은 마음에 재귀함수를 통해서 풀어봤다.
진짜 별거아닌 코드같은데 놀랍게도 이틀중 5시간은 써가면서 겨우 구현한 것 같다....
재귀 구현 너무 어렵...
'공부 > Algorithm' 카테고리의 다른 글
백준 2583번 영역 구하기 자바로 풀어 본 짧은 글 (0) | 2023.07.23 |
---|---|
알고리즘 근황 (0) | 2023.07.23 |
백준 21771 가희야 거기서 자는 거 아니야 자바로 풀어 본 짧은 글 (0) | 2023.05.31 |
백준 13717 포켓몬Go 자바로 풀어 본 짧은 글 (0) | 2023.05.25 |
백준 1418 K-세준수 자바로 풀어 본 짧은 글 (0) | 2023.05.24 |