-
Notifications
You must be signed in to change notification settings - Fork 0
/
17281.java
128 lines (105 loc) · 3.05 KB
/
17281.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.*;
import java.util.*;
public class Main {
static int N = 9, M = 4, ans = 0;
static boolean use[] = new boolean[N];
static int players[] = new int[N];
static int results[][];
static HashMap<Integer, HashMap<Integer, int[]>> field = new HashMap<>();
static{
field.put(0, new HashMap<>());
field.get(0).put(1, new int[]{1,0});
field.get(0).put(2, new int[]{2,0});
field.get(0).put(3, new int[]{3,0});
field.get(0).put(4, new int[]{0,1});
field.put(1, new HashMap<>());
field.get(1).put(1, new int[]{12,0});
field.get(1).put(2, new int[]{23,0});
field.get(1).put(3, new int[]{3,1});
field.get(1).put(4, new int[]{0,2});
field.put(2, new HashMap<>());
field.get(2).put(1, new int[]{13,0});
field.get(2).put(2, new int[]{2,1});
field.get(2).put(3, new int[]{3,1});
field.get(2).put(4, new int[]{0,2});
field.put(3, new HashMap<>());
field.get(3).put(1, new int[]{1,1});
field.get(3).put(2, new int[]{2,1});
field.get(3).put(3, new int[]{3,1});
field.get(3).put(4, new int[]{0,2});
field.put(12, new HashMap<>());
field.get(12).put(1, new int[]{123,0});
field.get(12).put(2, new int[]{23,1});
field.get(12).put(3, new int[]{3,2});
field.get(12).put(4, new int[]{0,3});
field.put(13, new HashMap<>());
field.get(13).put(1, new int[]{12,1});
field.get(13).put(2, new int[]{23,1});
field.get(13).put(3, new int[]{3,2});
field.get(13).put(4, new int[]{0,3});
field.put(23, new HashMap<>());
field.get(23).put(1, new int[]{13,1});
field.get(23).put(2, new int[]{2,2});
field.get(23).put(3, new int[]{3,2});
field.get(23).put(4, new int[]{0,3});
field.put(123, new HashMap<>());
field.get(123).put(1, new int[]{123,1});
field.get(123).put(2, new int[]{23,2});
field.get(123).put(3, new int[]{3,3});
field.get(123).put(4, new int[]{0,4});
}
public static void main(String args[]) throws Exception{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int inning = Integer.parseInt(input.readLine());
results = new int[inning][N];
for(int i=0; i<inning; i++){
String tc[] = input.readLine().split(" ");
for(int j=0; j<N; j++){
results[i][j] = Integer.parseInt(tc[j]);
}
}
getOrder(0, inning);
System.out.println(ans);
}
static int getScore(int[] order, int range) {
int score = 0;
int out = 0;
int cur = -1;
int state = 0;
int inning = 0;
while(inning < range){
cur++;
int result = results[inning][order[cur%N]];
if(result > 0){
int rs[] = field.get(state).get(result);
state = rs[0];
score+=rs[1];
continue;
}
out++;
if(out == 3){
inning++;
out = 0;
state = 0;
}
}
return score;
}
static void getOrder(int idx, int inning) {
if(players[3] != 0 && use[0]){
return;
}
if(idx == N){
ans = Math.max(ans, getScore(players, inning));
return;
}
for(int i=0; i<N; i++){
if(!use[i]){
use[i] = true;
players[idx] = i;
getOrder(idx+1, inning);
use[i] = false;
}
}
}
}