본문 바로가기

알고리즘

디저트카페[swexpert academy]

https://swexpertacademy.com/main/solvingProblem/solvingProblem.do

 

9:07~36분 설계 / 9:37 ~ 10:41 구현

구현상에 있었던 이슈: 1] 범위 체크  2] arrayList 초기화 / 성공해도 초기화 했어야

설계때 초기화 하는것까지는 안적는데 내재화가 덜 됨.

 

 

 

 

 

 

 

 

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.ArrayList;
import java.util.Scanner;
 
public class Solution {
    public static int sum=0;
    public static int max=-1;
    public static int t,n;
    public static int[][] map,score;
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        t = scan.nextInt();
        
        for(int i=1;i<=t;i++
        {
            input(scan);
            dessertCafe();
            int x = findMaxScore();
            if(x==0)x=-1;
            System.out.println("#"+i+" "+x);
            max=-1;
        }
        
        scan.close();
    }
    private static int findMaxScore() {
        int t = 0;
        for(int i=1;i<=n;i++
        {
            for(int j=1;j<=n;j++
            {
                if(t<score[i][j])t=score[i][j];
//                System.out.print(score[i][j]+" ");
            }
//            System.out.println();
        }
        
        return t;
    }
    private static void dessertCafe() {
        //1.맵을 1,1 ~ n,n 까지 순회한다.
        //        기준점에서 (1,1)~(n,n)을 사각형 범위로 잡는다.
        //            3개점의 범위를 검사한다.
        //                검사에 걸리면 continue한다.
        //                안결리면 4방향 순회한다.arrList에 담고, 개수 검사 & 계산) 
        
        ArrayList<Integer> arrList = new ArrayList<Integer>();
        
        for(int i=1;i<=n;i++
        {
            for(int j=1;j<=n;j++
            {
                //한점에서 (1,1)~(n,n)
                for(int a=1;a<=n;a++
                {
                    for(int b=1;b<=n;b++
                    {
                        if(rangeCheck(i,j,a,b)) 
                        {
//                            System.out.println("i: "+i+" j: "+j+" a: "+a+" b: "+b);
                            directionCheck(i,j,a,b,arrList);//방향순회 & arrlist 갱신
                            if(arrList.size()!=2*a+2*b) {arrList.clear();continue;}
                            else 
                            {
                                sum=arrList.size();
                                arrList.clear();
                            }
                            if(sum>max)max=sum;
                        }
                        else 
                        {
                            arrList.clear();
                            //범위 걸림
                            continue;
                        }
                    }
                }
                arrList.clear();
                //한점의 최대값
                score[i][j]=max;
            }
        }
    }
    
    
    
    private static void directionCheck(int curx,int cury,int rangeX,int rangeY,ArrayList<Integer> arrList) {
        int tx = curx;int ty = cury;
        for(int i=0;i<rangeX;i++
        {
            tx = tx+1;ty=ty+1;
//            System.out.println("tx:"+tx+" "+ty);
            if(!arrList.contains(map[tx][ty])) 
            {
                arrList.add(map[tx][ty]);
            }
        }
        
        for(int i=0;i<rangeY;i++
        {
            tx = tx+1;ty=ty-1;
//            System.out.println("tx:"+tx+" "+ty);
            if(!arrList.contains(map[tx][ty])) 
            {
                arrList.add(map[tx][ty]);
            }
        }
        for(int i=0;i<rangeX;i++
        {
            tx = tx-1;ty=ty-1;
//            System.out.println("tx:"+tx+" "+ty);
            if(!arrList.contains(map[tx][ty])) 
            {
                arrList.add(map[tx][ty]);
            }
        }
        for(int i=0;i<rangeY;i++
        {
            tx = tx-1;ty=ty+1;
//            System.out.println("tx:"+tx+" "+ty);
            if(!arrList.contains(map[tx][ty])) 
            {
                arrList.add(map[tx][ty]);
            }
        }
        
    }
    private static boolean rangeCheck(int curx,int cury,int a, int b) {
        
        int x1 = curx+a;
        int y1 = cury+a;
        
        int x2 = curx+a+b;
        int y2 = cury+a-b;
        
        int x3 = curx+b;
        int y3 = cury-b;
        
        if(x1>=1 && y1>=1 && x2>=1 && y2>=1 && x3>=1 && y3>=1 && x1<=&& y1<=&& x2<=&& y2<=&& x3<=&& y3<=n)return true;
        
//        if(x1==8 && )
        
        return false;
    }
    private static void input(Scanner scan) {
        n = scan.nextInt();
        map = new int[n+1][n+1];
        score = new int[n+1][n+1];
        for(int j=1;j<=n;j++
        {
            for(int k=1;k<=n;k++
            {
                map[j][k]=scan.nextInt();
            }
        }
    }
}
 
cs

'알고리즘' 카테고리의 다른 글

활주로건설[swexpert academy]  (0) 2019.10.01
홈방범서비스[swexpertacademy]  (0) 2019.10.01
탈주범 검거[swexpertacademy]  (0) 2019.09.30
미생물 격리[swexpertacademy]  (0) 2019.09.30
무선 충전[swexpert academy]  (0) 2019.09.29