[DIAMOND] 다이아몬드
 개발이야기/알고스팟2022. 1. 6. 21:43
          
          
          
          
          
        
      
    Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.
문제 : https://www.algospot.com/judge/problem/read/DIAMOND
algospot.com :: DIAMOND
다이아몬드 문제 정보 문제 ......#............................ ..#.######......................... .##########..........###........... ..#.#########......#######......... ......#............#######......... ....................####.#......... ...
www.algospot.com
풀이입니다. 🤔
| 
 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 
 | 
 import java.util.*; 
public class Main { 
    private static final char SHARP = '#'; 
    private static final char[][] board = new char[50][50]; 
    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in); 
        int cases = scanner.nextInt(); 
        while (cases-- > 0) { 
            int h = scanner.nextInt(), w = 0; 
            for (int i = 0; i < h; ++i) { 
                String line = scanner.next(); 
                w = line.length(); 
                for (int j = 0; j < w; ++j) { 
                    board[i][j] = line.charAt(j); 
                } 
            } 
            int ret = 0; 
            for (int i = 0; i < h; ++i) { 
                for (int j = 0; j < w; ++j) { 
                    ret = Math.max(ret, getMaxDiamondLength(i, j, h, w)); 
                } 
            } 
            System.out.println(ret); 
        } 
    } 
    private static int getMaxDiamondLength(int y, int x, int h, int w) { 
        int ret = 0, safety = 0; 
        for (int goal = 1; goal <= h; goal += 2) { 
            int center = goal / 2; 
            for (int distance = safety; distance < goal; ++distance) { 
                if (y + distance >= h 
                        || !hasLine(y + distance, x, w, (distance > center) ? goal - distance - 1 : distance)) { 
                    return ret; 
                } 
            } 
            ret = goal; 
            safety = center; // 다이아몬드가 있는 것이 증명되었다면, 다음 회차에서 Center 까지는 정확한 # 이 존재함을 보장한다. 
        } 
        return ret; 
    } 
    private static boolean hasLine(int y, int x, int w, int distance) { 
        int startX = x - distance, endX = x + distance; 
        if (startX < 0) return false; 
        if (endX >= w) return false; 
        for (int i = startX; i <= endX; ++i) if (board[y][i] != SHARP) return false; 
        return true; 
    } 
} 
 | 
cs이 포스트를 읽어주셔서 감사합니다. 🙇🏻♂️ | 
이 포스트를 읽어주셔서 감사합니다. 🙇🏻♂️
반응형
    
    
    
  '개발이야기 > 알고스팟' 카테고리의 다른 글
| [MVP] Most Valuable Programmer (0) | 2022.01.07 | 
|---|---|
| [MISPELL] Mispelling (0) | 2022.01.06 | 
| [ENCODING] Encoding (0) | 2022.01.06 | 
| [CANDLESTICK] Candlestick Charts (0) | 2022.01.06 | 
| [KAKURO1] Kakuro I (0) | 2022.01.06 |