Problem11

简介:

1.package com.yao.Algorithms;  
2.  
3.import java.util.ArrayList;  
4.import java.util.List;  
5.  
6./** 
7. *  
8. * @author shuimuqinghua77 @date 2011-11-6 
9. * 
10. */  
11./** 
12. * In the 2020 grid below, four numbers along a diagonal line have been marked 
13. * in red. 
14. *  
15. * 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 
16. * 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 
17. * 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 
18. * 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 
19. * 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 
20. * 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 
21. * 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 
22. * 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 
23. * 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 
24. * 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 
25. * 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 
26. * 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 
27. * 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 
28. * 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 
29. * 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 
30. * 33 48 61 43 52 01 89 19 67 48 The product of these numbers is 26 63 78 14 = 
31. * 1788696. 
32. *  
33. * What is the greatest product of four adjacent numbers in any direction (up, 
34. * down, left, right, or diagonally) in the 2020 grid? 
35. */  
36.public class Problem11 {  
37.    public static void main(String[] args) {  
38.        String text = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 0849 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 0081 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 6552 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 9122 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 8024 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 5032 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 7067 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 2124 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 7221 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 9578 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 9216 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 5786 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 5819 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 4004 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 6688 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 6904 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 3620 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 1620 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 5401 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";  
39.        int[][] seed = String2matrix(text);  
40.        int max = getGreatestProduct(4, seed);  
41.        System.out.println(max);  
42.  
43.    }  
44.  
45.    public static int[][] String2matrix(String text) {  
46.        text = text.replaceAll("[\\s]*", "");  
47.        int size = (int) Math.sqrt(text.length() / 2);  
48.        int[][] matrix = new int[size][size];  
49.        for (int i = 0; i < size; i++) {  
50.            for (int j = 0; j < size; j++) {  
51.                matrix[i][j] = Integer.valueOf(text.substring(size * i * 2 + j  
52.                        * 2, size * i * 2 + j * 2 + 2));  
53.            }  
54.        }  
55.        return matrix;  
56.    }  
57.  
58.    public static int getGreatestProduct(int size, int[][] seed) {  
59.        int max = 0;  
60.        max = lowGreatestProduct(size, seed, max);  
61.        max = columnGreatestProduct(size, seed, max);  
62.        max = diagonallylowGreatestProduct(size, seed, max);  
63.        max = diagonallycolumnGreatestProduct(size, seed, max);  
64.        max = disdiagonallylowGreatestProduct(size, seed, max);  
65.        max = disdiagonallycolumnGreatestProduct(size, seed, max);  
66.        return max;  
67.    }  
68.  
69.    private static int disdiagonallycolumnGreatestProduct(int size,  
70.            int[][] seed, int max) {  
71.        List<Integer> list = new ArrayList<Integer>();  
72.        for (int i = size - 1; i < seed.length; i++) {  
73.            list.clear();  
74.            for (int j = size - 1, x = i; j > -1 && x < seed.length; j--, x++) {  
75.                list.add(seed[x][j]);  
76.            }  
77.            max = getMax(size, max, list);  
78.        }  
79.        return max;  
80.    }  
81.  
82.    private static int disdiagonallylowGreatestProduct(int size, int[][] seed,  
83.            int max) {  
84.        List<Integer> list = new ArrayList<Integer>();  
85.        for (int j = size - 1; j < seed.length; j++) {  
86.            list.clear();  
87.            for (int i = 0, x = j; i < seed.length && x > -1; i++, x--)  
88.                list.add(seed[i][x]);  
89.            max = getMax(size, max, list);  
90.        }  
91.        return max;  
92.    }  
93.  
94.    private static int diagonallycolumnGreatestProduct(int size, int[][] seed,  
95.            int max) {  
96.        List<Integer> list = new ArrayList<Integer>();  
97.        for (int j = 1; j < seed.length - size + 1; j++) {  
98.            list.clear();  
99.            for (int i = 0, x = j; x < seed.length; i++, x++) {  
100.                list.add(seed[i][x]);  
101.            }  
102.            max = getMax(size, max, list);  
103.        }  
104.        return max;  
105.    }  
106.  
107.    private static int diagonallylowGreatestProduct(int size, int[][] seed,  
108.            int max) {  
109.        List<Integer> list = new ArrayList<Integer>();  
110.        for (int i = 0; i < seed.length - size + 1; i++) {  
111.            list.clear();  
112.            for (int j = 0, x = i; x < seed.length; j++, x++)  
113.                list.add(seed[x][j]);  
114.  
115.            max = getMax(size, max, list);  
116.  
117.        }  
118.  
119.        return max;  
120.    }  
121.  
122.    private static int getMax(int size, int max, List<Integer> list) {  
123.        if (list.size() < 4)  
124.            return max;  
125.  
126.        for (int c = 0; c < list.size() - size + 1; c++) {  
127.            int temp = 1;  
128.            for (int k = 0; k < size; k++) {  
129.                temp *= list.get(c + k);  
130.            }  
131.            if (temp > max)  
132.                max = temp;  
133.        }  
134.        return max;  
135.    }  
136.  
137.    private static int columnGreatestProduct(int size, int[][] seed, int max) {  
138.        for (int j = 0; j < seed.length; j++) {  
139.            for (int i = 0; i < seed.length - size + 1; i++) {  
140.                int temp = 1;  
141.                for (int k = i; k < size; k++) {  
142.                    temp *= seed[k][j];  
143.                }  
144.                if (temp > max)  
145.                    max = temp;  
146.            }  
147.        }  
148.        return max;  
149.    }  
150.  
151.    private static int lowGreatestProduct(int size, int[][] seed, int max) {  
152.        for (int i = 0; i < seed.length; i++) {  
153.            for (int j = 0; j < seed.length - size + 1; j++) {  
154.                int temp = 1;  
155.                for (int k = j; k < size; k++) {  
156.                    temp *= seed[i][k];  
157.                }  
158.                if (temp > max)  
159.                    max = temp;  
160.            }  
161.        }  
162.        return max;  
163.    }  
164.  
165.}  

相关文章
Divisibility Problem
Divisibility Problem
153 0
|
机器学习/深度学习
|
Java
|
数据库管理