본문 바로가기

알고리즘

무선 충전[swexpert academy]

링크: https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRDL1aeugDFAUo

 

 

시뮬레이션 2차 풀이 방식 : arrayList[][] 활용 (훨씬 깔끔)   / 원래 3차원 배열로 다시 풀어보려고 했는데, 굳이 어제의 비효율적인 방식을 반복할 필요는 없다고 판단. -> 설계를 코드로 옮기다가 다시 바꿈. 

7:38 ~ 8:01 설계/ 구현 8:01 ~ 8:51 천천히 함(확실히 실수는 줌)

 

 

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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
 
public class Solution {
    public static int[] dx = {0,-1,0,1,0};
    public static int[] dy = {0,0,1,0,-1};
    public static int t,m,a;
    public static int[] moveA,moveB;
    public static int[] power;
    public static int[][] score;
    public static ArrayList<Integer>[][] arrList = new ArrayList[11][11];
    public static void main(String[] args) {
        
        
        Scanner scan = new Scanner(System.in);
        t  = scan.nextInt();
        for(int i=1;i<=t;i++
        {
            input(scan);
//            printArea();
            int x = simulation();
            System.out.println("#"+i+" "+x);
        }
        scan.close();
    }
    private static int simulation() {
        int ax=1;int ay=0;int bx=10;int by=11;
        int totalSum=0;
        for(int i=0;i<=m;i++
        {
            int adir = moveA[i];
            int bdir = moveB[i];
            
            if(adir==1) {ax = ax+dx[1];ay = ay+dy[1];}
            else if(adir==2) {ax = ax+dx[2];ay = ay+dy[2];}
            else if(adir==3) {ax = ax+dx[3];ay = ay+dy[3];}
            else if(adir==4) {ax = ax+dx[4];ay = ay+dy[4];}
            
            if(bdir==1) {bx = bx+dx[1];by = by+dy[1];}
            else if(bdir==2) {bx = bx+dx[2];by = by+dy[2];}
            else if(bdir==3) {bx = bx+dx[3];by = by+dy[3];}
            else if(bdir==4) {bx = bx+dx[4];by = by+dy[4];}
            
            ArrayList<Integer> groupA = arrList[ax][ay];
            ArrayList<Integer> groupB = arrList[bx][by];
            
            
            int t = 0;int max=0;
//            System.out.println("p:"+Arrays.toString(power));
//            System.out.println("ga:"+groupA.toString());
//            System.out.println("gb:"+groupB.toString());
            for(int j=0;j<groupA.size();j++
            {
                for(int k=0;k<groupB.size();k++
                {
                    if(groupA.get(j)==groupB.get(k)) 
                    {
                        t = power[groupA.get(j)];
                    }
                    else 
                    {
                        t = power[groupA.get(j)]+power[groupB.get(k)];
                    }
                    if(t>max)max=t;
                }
            }
//            System.out.println("i:"+i+" 일 때 합:"+max);
            totalSum+=max;
        }
//        System.out.println("total:"+totalSum);
        return totalSum;
    }
    private static void input(Scanner scan) {
        m = scan.nextInt();
        a = scan.nextInt();
        moveA = new int[m+1];
        moveB = new int[m+1];
        power = new int[a+1];
        score = new int[2][m+1];
        moveA[0]=2;moveB[0]=4;
        for(int j=1;j<=m;j++)moveA[j] = scan.nextInt();
        for(int j=1;j<=m;j++)moveB[j] = scan.nextInt();
        
        init();
        
        for(int j=1;j<=a;j++
        {
            int w = scan.nextInt();
            int x = scan.nextInt();
            int y = scan.nextInt();
            int z = scan.nextInt();
            markArea(x,w,y,j);
            power[j]=z;
        }
    }
    private static void printArea() 
    {
        for(int i=0;i<11;i++
        {
            for(int k=0;k<11;k++
            {
                System.out.print("{"+i+","+k+"}:"+arrList[i][k].toString());
            }System.out.println();
        }
    }
    
    private static void markArea(int x, int y, int range,int idx) {
        // TODO Auto-generated method stub
        for(int i=1;i<11;i++
        {
            for(int j=1;j<11;j++
            {
                if(abs(x-i)+abs(y-j)<=range) 
                {
//                    System.out.println(i+" "+j);
                    arrList[i][j].add(idx);
                }
            }
        }
    }
 
    public static int abs(int x) {if(x>0)return x;else return -x;}
    
    public static void init() 
    {
        for(int i=0;i<11;i++
        {
            for(int j=0;j<11;j++
            {
                arrList[i][j] = new ArrayList<Integer>();
                arrList[i][j].add(0);
            }
        }
    }
}
 
cs

 

 

 

시뮬레이션 문제 1차 풀이 방식: 3차원 배열 사용

 

 

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
 
public class Solution {
 
    public static int totalSum = 0;
    public static int[] powerTable;
    public static int[][][] map;
    public static int t, m, a;
    public static int[] aMove, bMove;
    public static int[] dx = new int[] { 0-10+10 };
    public static int[] dy = new int[] { 0010-1 };
    public static int[][] score;
 
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
 
        t = scan.nextInt();
 
        for (int i = 1; i <= t; i++) {
            m = scan.nextInt();
            a = scan.nextInt();
 
            aMove = new int[m + 1];
            bMove = new int[m + 1];
            map = new int[11][11][a + 1];
            for (int j = 1; j <= m; j++)
                aMove[j] = scan.nextInt();
            for (int j = 1; j <= m; j++)
                bMove[j] = scan.nextInt();
            aMove[0= 2;
            bMove[0= 4;
 
            powerTable = new int[a + 1];
            score = new int[2][m + 1];
            for (int layer = 1; layer <= a; layer++) {
 
                int x = scan.nextInt();
                int y = scan.nextInt();
                int range = scan.nextInt();
                int power = scan.nextInt();
                powerTable[layer] = power;
 
                makeMap(y, x, range, power, layer);
 
            }
//            printMap();
            traverse();
//            printScore();
 
            System.out.println("#" + i + " " + totalSum);
            totalSum = 0;
        }
    }
 
    private static void printScore() {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(score[i][j] + " ");
            }
            System.out.println();
        }
    }
 
    private static void traverse() {
 
        int aNewX = 1, aNewY = 0;
        int bNewX = 10, bNewY = 11;
 
        for (int i = 0; i <= m; i++) {
//            System.out.println("T : "+(i+1));
 
            aNewX = aNewX + dx[aMove[i]];
            aNewY = aNewY + dy[aMove[i]];
//            System.out.println("aNewX:"+aNewX+" aNewY:"+aNewY);
 
            bNewX = bNewX + dx[bMove[i]];
            bNewY = bNewY + dy[bMove[i]];
//            System.out.println("bNewX:"+bNewX+" bNewY:"+bNewY);
 
            ArrayList<Integer> aLay = new ArrayList<Integer>();
            ArrayList<Integer> bLay = new ArrayList<Integer>();
            for (int j = 1; j <= a; j++) {
                if (map[aNewX][aNewY][j] != 0) {
                    aLay.add(j);
                }
                if (map[bNewX][bNewY][j] != 0) {
                    bLay.add(j);
                }
            }
 
            Collections.sort(aLay);
            Collections.sort(bLay);
 
            if (aLay.isEmpty())
                aLay.add(0);
            if (bLay.isEmpty())
                bLay.add(0);
 
//            System.out.println("a의 좌표 ["+aNewX+","+aNewY+"] 값:"+aLay.toString());
//            System.out.println("b의 좌표 ["+bNewX+","+bNewY+"] 값:"+bLay.toString());
 
            calculate(i, aLay, bLay);
        }
 
    }
 
    private static void calculate(int idx, ArrayList<Integer> aLay, ArrayList<Integer> bLay) {
 
        int max = 0;
        int maxA = 0;
        int maxB = 0;
        int t = 0;
        for (int i = 0; i < aLay.size(); i++) {
 
            for (int j = 0; j < bLay.size(); j++) {
                if (aLay.get(i) == bLay.get(j)) {
                    t = powerTable[aLay.get(i)];
                    if (t >= max) {
                        max = t;
                        maxA = aLay.get(i);
                        maxB = bLay.get(j);
                        score[0][idx] = powerTable[maxA] / 2;
                        score[1][idx] = powerTable[maxB] / 2;
                    }
                } else {
                    t = powerTable[aLay.get(i)] + powerTable[bLay.get(j)];
//                    System.out.println("t:"+t);
                    if (t >= max) {
                        max = t;
                        maxA = aLay.get(i);
                        maxB = bLay.get(j);
                        score[0][idx] = powerTable[maxA];
                        score[1][idx] = powerTable[maxB];
                    }
                }
            }
        }
//        System.out.println(">t:"+t+" "+maxA+" "+);
        int sum = 0;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j <= m; j++) {
                sum += score[i][j];
            }
        }
        totalSum = sum;
 
    }
 
    private static void makeMap(int x, int y, int range, int pw, int layer) {
        for (int i = 1; i <= 10; i++) {
            for (int j = 1; j <= 10; j++) {
                if (abs(i - x) + abs(j - y) <= range) {
                    map[i][j][layer] = layer;
                    map[i][j][0= -1;
                }
            }
        }
 
    }
 
    public static void printMap() {
        for (int layer = 0; layer <= 3; layer++) {
            for (int x = 1; x <= 10; x++) {
                for (int y = 1; y <= 10; y++) {
                    if (map[x][y][layer] != 0)
                        System.out.print(map[x][y][layer] + " ");
                    else
                        System.out.print("x ");
                }
                System.out.println();
            }
            System.out.println("------");
        }
    }
 
    private static int abs(int x) {
        if (x > 0)
            return x;
        else
            return -x;
    }
}
 
//1
//20 3
//2 2 3 2 2 2 2 3 3 4 4 3 2 2 3 3 3 2 2 3
//4 4 1 4 4 1 4 4 1 1 1 4 1 4 3 3 3 3 3 3
//4 4 1 100
//7 10 3 40
//6 3 2 70
 
cs

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

탈주범 검거[swexpertacademy]  (0) 2019.09.30
미생물 격리[swexpertacademy]  (0) 2019.09.30
DAM  (0) 2019.09.26
SAFETYZONE  (0) 2019.09.26
ICEBERG  (0) 2019.09.26