File tree Expand file tree Collapse file tree 1 file changed +69
-0
lines changed
Expand file tree Collapse file tree 1 file changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.BufferedReader ;
3+ import java.io.IOException ;
4+ import java.io.InputStreamReader ;
5+
6+ public class Main {
7+ private static BufferedReader br;
8+ private static int n;
9+ private static int [] ws;
10+ private static int [] ss;
11+ private static int answer;
12+
13+ public static void main (String [] args ) throws IOException {
14+ br = new BufferedReader (new InputStreamReader (System . in));
15+ n = Integer . parseInt(br. readLine());
16+ ss = new int [n];
17+ ws = new int [n];
18+ for (int i = 0 ; i < n; i++ ) {
19+ String [] temp = br. readLine(). split(" " );
20+ ss[i] = Integer . parseInt(temp[0 ]);
21+ ws[i] = Integer . parseInt(temp[1 ]);
22+ }
23+ answer = 0 ;
24+ dfs(0 );
25+ System . out. println(answer);
26+ br. close();
27+ }
28+
29+ private static void dfs (int now ) {
30+ if (now == n){
31+ // 완탐 리프 - 깨진 계란 개수 세기
32+ int count = 0 ;
33+ for (int i = 0 ; i < n; i++ ) {
34+ if (ss[i] <= 0 ) count++ ;
35+ }
36+ answer = Math . max(answer, count); // 반복문 밖으로 이동
37+ return ;
38+ }
39+
40+ // 현재 계란이 이미 깨져있으면 바로 다음으로
41+ if (ss[now] <= 0 ) {
42+ dfs(now + 1 );
43+ return ;
44+ }
45+
46+ boolean canHit = false ;
47+
48+ for (int target = 0 ; target < n; target++ ) {
49+ if (now == target || ss[target] <= 0 ) continue ;
50+
51+ canHit = true ;
52+
53+ // 서로 치기
54+ ss[now] -= ws[target];
55+ ss[target] -= ws[now];
56+
57+ dfs(now + 1 );
58+
59+ // 상태 복구
60+ ss[now] += ws[target];
61+ ss[target] += ws[now];
62+ }
63+
64+ if (! canHit) {
65+ dfs(now + 1 );
66+ }
67+ }
68+ }
69+ ```
You can’t perform that action at this time.
0 commit comments