SWEA_2805) 농작물 수확하기

2805) 농작물 수확하기 (19.10.20)


문제 링크 (SW Expert Academy)

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
import java.io.*;

public class Solution {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int test = Integer.parseInt(br.readLine());
        for(int i=1; i<=test; i++) {
            
            int n = Integer.parseInt(br.readLine());
            int[][] map = new int[n][n];
            
            for(int p=0; p<n; p++) {
                String s = br.readLine();
                for(int q=0; q<n; q++) {
                    map[p][q] = s.charAt(q)-'0';
                }
            }
            
            int ans = 0;
            int start = n/2;
            for(int p=0; p<n; p++) {
                if(p<=start) {
                    int q = p*2+1;
                    int s = start-p;
                    while(q>0) {
                        ans = ans+map[p][s];
                        q = q-1;
                        s = s+1;
                    }
                }
                else {
                    int s = p-start;
                    int q = (start-s)*2+1;
                    while(q>0) {
                        ans = ans+map[p][s];
                        q = q-1;
                        s = s+1;
                    }
                }
            }
            
            bw.write("#" + i + " " + ans + "\n");
        }
        
        bw.flush();
        bw.close();
    }
}
cs



* q를 각 줄마다 몇 개를 더하는지, s를 시작점의 인덱스로 설정.
* 근데 하다보니까 몇 개 더하는지 list에 저장해놓고 반대로 빼도 될 것 같아서 수정하니까
* 아래처럼 됐는데, 시간에 큰 차이는 없었다.
* 근데 list 추가해서 메모리 증가할 줄 알았는데 완전 살짝 감소해서 의외..

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
import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int test = Integer.parseInt(br.readLine());
        for(int i=1; i<=test; i++) {
            
            int n = Integer.parseInt(br.readLine());
            int[][] map = new int[n][n];
            
            for(int p=0; p<n; p++) {
                String s = br.readLine();
                for(int q=0; q<n; q++) {
                    map[p][q] = s.charAt(q)-'0';
                }
            }
            
            int ans = 0;
            int start = n/2;
            ArrayList<Integer> list = new ArrayList<>();
            for(int p=0; p<n; p++) {
                if(p<=start) {
                    int q = p*2+1;
                    int s = start-p;
                    list.add(q);
                    while(q>0) {
                        ans = ans+map[p][s];
                        q = q-1;
                        s = s+1;
                    }
                }
                else {
                    int s = p-start;
                    int q = list.remove(list.size()-2);
                    while(q>0) {
                        ans = ans+map[p][s];
                        q = q-1;
                        s = s+1;
                    }
                }
            }

            bw.write("#" + i + " " + ans + "\n");
        }
        
        bw.flush();
        bw.close();
    }
}
cs



댓글

이 블로그의 인기 게시물