사탕게임
풀이
(1) 우선적으로 있는 그대로 행 그리고 열을 비교한다.
(2) 사탕을 상하좌우로 움직이고 비교한다.
(2-1) 사탕을 상하로 움직인 경우, 좌우를 비교한다.
(2-2) 사탕을 좌우로 움직인 경우, 상하를 비교한다.
중요한 것은 결국 전체를 비교해야하는 것이고, (2-1) 과 (2-2) 부분에서 조금 최적화를 시킬 수 있다. 하지만 나는 하지 않았다..
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 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class Main { private static final File file = new File("src\\input.txt"); private static FileInputStream fis = null; static InputStreamReader isr; static BufferedReader br; static OutputStreamWriter osw; static BufferedWriter bw; static StringTokenizer st; static int N = 0; public static void main(String[] args) throws IOException { // fis = new FileInputStream(file); // isr = new InputStreamReader(fis); isr = new InputStreamReader(System.in); br = new BufferedReader(isr); osw = new OutputStreamWriter(System.out); bw = new BufferedWriter(osw); N = Integer.parseInt(br.readLine()); char[][]array = new char[N][N]; String line = null; for(int i = 0; i < N; i++) { line = br.readLine(); for(int j = 0; j < N; j++) { array[i][j] = line.charAt(j); } } int resultMax = 0; resultMax = Math.max(getMaxCandy(true, array), getMaxCandy(false, array)); resultMax = Math.max(Math.max(moveLeftAndRight(array), moveUpAndDown(array)), resultMax); bw.write(resultMax + "\n"); bw.flush(); bw.close(); br.close(); } /** * 행과 열 한 라인씩 비교 <br> * @param flag * @return */ private static int getMaxCandy(boolean flag, char[][]array) { int resultMax = 1; if(flag) { // 행 비교 for(int i = 0; i < N; i++) { int count = 1; for(int j = 1; j < N; j++) { if(array[i][j] == array[i][j-1]) { count += 1; } else { resultMax = Math.max(count, resultMax); count = 1; } } resultMax = Math.max(count, resultMax); } } else { // 열 비교 for(int j = 0; j < N; j++) { int count = 1; for(int i = 1; i < N; i++) { if(array[i][j] == array[i-1][j]) { count += 1; } else { resultMax = Math.max(count, resultMax); count = 1; } } resultMax = Math.max(count, resultMax); } } return resultMax; } /** * 캔디를 좌우로 움직이고, 상하로 비교 */ private static int moveLeftAndRight(char[][]array) { int resultMax = 0; for(int i = 0; i < N; i++) { for(int j = 1; j < N; j++) { char temp = array[i][j]; array[i][j] = array[i][j-1]; array[i][j-1] = temp; // 상하비교 resultMax = Math.max(getMaxCandy(false, array), resultMax); array[i][j-1] = array[i][j]; array[i][j] = temp; } } return resultMax; } /** * 캔디를 상하로 움직이고, 좌우로 비교 */ private static int moveUpAndDown(char[][]array) { int resultMax = 0; for(int j = 0; j < N; j++) { for(int i = 1; i < N; i++) { char temp = array[i-1][j]; array[i-1][j] = array[i][j]; array[i][j] = temp; // 상하비교 resultMax = Math.max(getMaxCandy(true, array), resultMax); array[i][j] = array[i-1][j]; array[i-1][j] = temp; } } return resultMax; } } | cs |
'Problem Solving & Algorithm > BOJ ' 카테고리의 다른 글
20181225 1182번 : 부분집합의 합 (0) | 2018.12.25 |
---|---|
20181225 2503번 : 숫자 야구 (0) | 2018.12.25 |
20180826 8월 BOJ 풀이 : 트리의 순회 (0) | 2018.08.26 |
20180825 8월 BOJ 풀이 : 트리의 높이와 너비 (0) | 2018.08.25 |
20180822 8월 BOJ 풀이 : 트리의 지름 (0) | 2018.08.22 |