Algorithm

[BOJ] 17281.⚾

프로그래민 2020. 5. 1. 01:41
반응형

구현이 중요한 문제다.

문제에 주어진 그대로 구현을 하는 문제이다. 구현은 까다롭지 않지만 다만 여기서 주의할점은 타자의 순서인데, 이 문제에선 1번선수가 4번타자를 맡게 되므로 나머지 8명의 선수를 이용하여 순열을 하고 그때 그때마다 게임을 진행하는 식으로 하였다.

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package boj;
 
 
public class Main_bj_17281_야구_2 {
    
    static int [] base;
    static int [][] game;
    static int score,n;
    
    
    static int[] order;
    static int[] realOrder;
    static int[] data;
    static boolean[] visit;
    
    static int result;
    public static void main(String[] args) throws Exception {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = null;
        
        n=Integer.parseInt(br.readLine());
    
        game=new int[n][9];
        for(int i=0;i<n;i++) {
            st=new StringTokenizer(br.readLine());
            for(int j=0;j<9;j++) {
                game[i][j]=Integer.parseInt(st.nextToken());
            }
        }
        
        base=new int[4];
        
        realOrder=new int[9];
        order=new int[8];
        data=new int[8];
        for(int i=0;i<8;i++) {
            data[i]=i+1;
        }
        visit=new boolean[8];
        result=0;
        perm(0);
        System.out.println(result);
        
    }
    
    static void perm(int depth) {
        if(depth==8) {
            for(int i=0;i<9;i++) {
                if(i<3)
                    realOrder[i]=order[i];
                else if(i==3)
                    realOrder[i]=0;
                else
                    realOrder[i]=order[i-1];
            }
            score=0;
            func();
            result=Math.max(result, score);
            return;
        }
        
        for(int i=0;i<8;i++) {
            if(visit[i]==false) {
                visit[i]=true;
                order[depth]=data[i];
                perm(depth+1);
                visit[i]=false;
            }
        }
        
        
    }
    
    static void func() {
        int cur=0;
        for(int i=0;i<n;i++) {
            int out=0;
            clearBase();
            while(true) {
                int option=game[i][realOrder[cur]];
                
                if(option==0out+=1;
                else if(option==1) hit(1);
                else if(option==2) hit(2);
                else if(option==3) hit(3);
                else if(option==4) hit(4);
                
                cur+=1;
                if(cur==9) cur=0;
                
                if(out==3)
                    break;
            }
        }
        
        
    }
    
    static void hit(int opt) {
        if(opt==1) {
            if(base[3]==1) score+=1;
            base[3]=base[2];
            base[2]=base[1];
            base[1]=1;
        }else if(opt==2) {
            if(base[3]==1)    score+=1;
            if(base[2]==1)    score+=1;
            base[3]=base[1];
            base[2]=1;
            base[1]=0;
        }else if(opt==3) {
            int count=0;
            for(int i=1;i<=3;i++) {
                if(base[i]==1) {
                    count+=1;
                    base[i]=0;
                }
            }
            score+=count;
            base[3]=1;
        }else if(opt==4) {
            int count=0;
            for(int i=1;i<=3;i++) {
                if(base[i]==1) {
                    count+=1;
                    base[i]=0;
                }
            }
            score+=count+1;
        }
    }
    
    static void clearBase() {
        for(int i=1;i<=3;i++)
            base[i]=0;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
                                           
반응형

'Algorithm' 카테고리의 다른 글

[SWEA] 4366. 정식이의 은행업무  (0) 2020.05.02
[SWEA] 1949. 등산로 조성  (0) 2020.05.02
[BOJ] 1339. 단어수학  (0) 2020.04.30
[BOJ] 12100. 2048(Easy)  (0) 2020.04.30
[SWEA] 6019. 추억의 2048게임  (0) 2020.04.30