@@ -4,127 +4,79 @@ import java.io.BufferedReader;
44import java.io.IOException ;
55import java.io.InputStreamReader ;
66import java.util.* ;
7+ import java.util.List ;
78
89public class Main {
9- static int [][] arr;
10- static boolean [][] visited;
11- static int [] dy = {- 1 ,0 ,1 ,0 };
12- static int [] dx = {0 ,1 ,0 ,- 1 };
13- static int size;
14- static final int LAND = 1 ;
15- static final int WATER = 0 ;
1610
11+ static StringBuilder sb = new StringBuilder ();
12+ static int target;
13+ static boolean [] num;
14+
15+ // 골드바흐의 강한 추측: 2보다 큰 짝수는 항상 두 소수의 합으로 표현할 수 있다.
16+ // 골드바흐의 약한 추측: 5보다 큰 홀수는 항상 세 소수의 합으로 표현할 수 있다.
17+ // 8 이상의 짝수는 항상 네 소수의 합으로 표현 가능하다-> 두 소수 + 2 + 2
18+ // 9 이상의 홀수는 항상 네 소수의 합으로 표현 가능하다.-> 세 소수 + 2
19+ // 즉 8이상은 항상 표현이 가능하다.
1720 public static void main (String [] args ) throws IOException {
1821 init();
19- int result = process();
20- System . out . println(result );
22+ process();
23+ print( );
2124 }
2225
2326 private static void init () throws IOException {
2427 BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
25- size = Integer . parseInt(br. readLine());
26- arr = new int [size][size];
27-
28- for (int i = 0 ; i < size; i++ ){
29- StringTokenizer st = new StringTokenizer (br. readLine());
30- for (int j = 0 ; j < size; j++ ){
31- arr[i][j] = Integer . parseInt(st. nextToken());
32- }
33- }
28+ target = Integer . parseInt(br. readLine());
29+ num = new boolean [target + 1 ];
30+ Arrays . fill(num, true );
31+ num[0 ] = false ;
32+ num[1 ] = false ;
3433 }
3534
36- private static int process () {
37- // 모든 섬들을 서로 다른 번호로 표시
38- visited = new boolean [size][size];
39- int islandNum = 2 ;
40-
41- for (int i = 0 ; i < size; i++ ){
42- for (int j = 0 ; j < size; j++ ){
43- if (arr[i][j] == LAND && ! visited[i][j]){
44- markIsland(i, j, islandNum);
45- islandNum++ ;
46- }
47- }
35+ private static void process () {
36+ if (target < 8 ) {
37+ sb. append(- 1 );
38+ return ;
4839 }
4940
50- // 각 섬에서 다른 섬으로 가는 최단 거리 찾기
51- int minDistance = Integer . MAX_VALUE ;
52-
53- for (int currentIsland = 2 ; currentIsland < islandNum; currentIsland++ ) {
54- int distance = bfsFromIsland(currentIsland);
55- minDistance = Math . min(minDistance, distance);
41+ getPrimes();
42+ int curNum = target;
43+ if ( target % 2 == 0 ){
44+ sb. append(" 2 2 " );
45+ curNum -= 4 ;
46+ } else {
47+ sb. append(" 2 3 " );
48+ curNum -= 5 ;
5649 }
5750
58- return minDistance;
59- }
60-
61- private static void markIsland (int y , int x , int islandNum ) {
62- Queue<Point > q = new LinkedList<> ();
63- arr[y][x] = islandNum;
64- q. add(new Point (x, y));
65- visited[y][x] = true ;
66-
67- while (! q. isEmpty()){
68- Point p = q. poll();
69- for (int i = 0 ; i < 4 ; i++ ) {
70- int nx = p. x + dx[i];
71- int ny = p. y + dy[i];
72- if (ny < 0 || ny >= size || nx < 0 || nx >= size) continue ;
73- if (visited[ny][nx] || arr[ny][nx] != LAND ) continue ;
74-
75- visited[ny][nx] = true ;
76- arr[ny][nx] = islandNum;
77- q. add(new Point (nx, ny));
51+ for (int i = 2 ; i <= curNum/ 2 ; i++ ) {
52+ if (num[i] && num[ curNum - i]) {
53+ sb. append(i). append(" " ). append(curNum- i);
54+ return ;
7855 }
7956 }
8057 }
8158
82- private static int bfsFromIsland (int startIsland ) {
83- Queue<Point > queue = new LinkedList<> ();
84- boolean [][] bfsVisited = new boolean [size][size];
85-
86- // 시작 섬의 모든 셀을 큐에 추가
87- for (int i = 0 ; i < size; i++ ){
88- for (int j = 0 ; j < size; j++ ){
89- if (arr[i][j] == startIsland){
90- queue. add(new Point (j, i));
91- bfsVisited[i][j] = true ;
59+ private static void getPrimes () {
60+ for (int i = 2 ; i * i <= target; i++ ) {
61+ if (num[i]) {
62+ int temp = i* 2 ;
63+ while ( temp <= target ) {
64+ num[temp] = false ;
65+ temp += i;
9266 }
9367 }
94- }
95-
96- int distance = 0 ;
9768
98- while ( ! queue . isEmpty()) {
99- int qSize = queue . size();
69+ }
70+ }
10071
101- for (int i = 0 ; i < qSize; i++ ) {
102- Point p = queue. poll();
10372
104- for ( int k = 0 ; k < 4 ; k ++ ) {
105- int nx = p . x + dx[k] ;
106- int ny = p . y + dy[k];
73+ private static void print () {
74+ System . out . println(sb . toString()) ;
75+ }
10776
108- if (nx < 0 || ny < 0 || nx >= size || ny >= size) continue ;
109- if (bfsVisited[ny][nx]) continue ;
11077
111- // 다른 섬을 찾았다면 거리 반환
112- if (arr[ny][nx] >= 2 && arr[ny][nx] != startIsland) {
113- return distance;
114- }
11578
116- // 물이면 다음 레벨로 확장
117- if (arr[ny][nx] == WATER ) {
118- bfsVisited[ny][nx] = true ;
119- queue. add(new Point (nx, ny));
120- }
121- }
122- }
123- distance++ ;
124- }
125-
126- return Integer . MAX_VALUE ;
127- }
12879}
12980
81+
13082```
0 commit comments