CS/알고리즘_삼성 SW 역량 테스트 기출 문제

삼성 SW 역량 테스트 기출 문제 : 2048 (Easy)

Jedy_Kim 2021. 10. 12. 19:20
728x90

https://www.acmicpc.net/problem/12100

 

12100번: 2048 (Easy)

첫째 줄에 보드의 크기 N (1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 게임판의 초기 상태가 주어진다. 0은 빈 칸을 나타내며, 이외의 값은 모두 블록을 나타낸다. 블록에 쓰여 있는 수는 2

www.acmicpc.net

 

// 코드 -> 틀렸다고 한다..ㅠ (주말에 다시 도전해야 겠다.)

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
// 실패... 원인이..뭘까..
 
import java.util.*;
import java.io.*;
 
/*
  각 방향으로 이동시 보드의 숫자 변화를 어떻게 처리할 것인가?
  -> 상, 하, 좌, 우 4개의 함수를 구현하여 따로 처리해도 되지만,
  ***
  -> 코딩의 실수와 코딩 시간을 줄이기 위해서는 한가지 방향만 구현하고,
  -> 보드를 90도 돌리는 함수를 구현하면 코딩 시간을 줄일 수 있다.
*/
 
public class Main{
  
  static int resCnt = 0;
  static int n;
  static int map[][]; 
  
  static void up() { 
    int[][] temp = new int[n][n]; 
    
    for(int col=0; col<n; ++col) {
      int flag = 0, target = -1//  target : 이전 요소를 뜻함.
      for(int row=0; row<n; ++row) {
        
        if(map[row][col] == 0continue;
        
        if(flag == 1 && map[row][col] == temp[target][col]) { 
          temp[target][col] *= 2;
          flag = 0
        } else {
          temp[++target][col] = map[row][col];
          flag = 1;
        }
        
      }
      
      for(++target; target < n; ++target) {
        temp[target][col] = 0;
      }
      
    }  
    
    for(int row=0; row<n; ++row) {
      for(int col=0; col<n; ++col) {
        map[row][col] = temp[row][col];
      }
    }
    
  }
  
  static void rotate() {
    int[][] temp = new int[n][n];
    
    for(int col=0; col<n; ++col) {
      for(int row=0; row<n; ++row) {
        temp[row][col] = map[n - col - 1][row];
      }
    } 
    
    for(int row=0; row<n; ++row) {
      for(int col=0; col<n; ++col) {
        map[row][col] = temp[row][col];
      }
    }
  }
  
  static int getMax() {
    int ret = 0
    
    for(int row=0; row<n; ++row) {
      for(int col=0; col<n; ++col) {
        if(ret < map[row][col]) ret = map[row][col]; 
      } 
    }
    
    return ret;
  }
  
  static void dfs(int count) {   
    
    if(count == 5) { 
      int candi = getMax(); 
      resCnt = Math.max(resCnt, candi);
      return;
    }
    
    for(int dir=0; dir<4++dir) {
      up();
      dfs(count+1);
      rotate();
    }
    
  }
  
  // main
  public static void main(String[] args) throws Exception {
    
    BufferedWriter bw  = new BufferedWriter(new OutputStreamWriter(System.out));
    BufferedReader br  = new BufferedReader(new InputStreamReader(System.in)); 
    
    n = Integer.parseInt(br.readLine());
    map = new int[n][n];
    for(int y=0; y<n; ++y) {
      StringTokenizer st = new StringTokenizer(br.readLine());
      for(int x=0; x<n; ++x) {
        map[y][x] = Integer.parseInt(st.nextToken());
      }
    } 
    
    dfs(0);
    bw.write(String.valueOf(resCnt));
    
    br.close();
    bw.flush();
    bw.close();
  } 
}
cs

// 위의 코드 방식의 원인은 찾았는데 해결을 못했다.. 향후 또 도전해야겠다..

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
import java.util.*;
import java.io.*;
public class Main{
  
  static int maxVal = Integer.MIN_VALUE;
  static int N;
  static int[][] map;
  
  static void rotate() {
    
    int[][] temp = new int[N][N];
    
    for(int col=0; col<N; ++col) {
      for(int row=0; row<N; ++row) {
        temp[row][col] = map[N - col -1] [row];
      }
    }
    
    for(int row=0; row<N; ++row) for(int col=0; col<N; ++col) map[row][col] = temp[row][col];  
  }
  
  static void up() {
    
    int[][] temp = new int[N][N];
    
    for(int col=0; col<N; ++col) {
      int flag=0, target=-1;
      for(int row=0; row<N; ++row) {
        
        if(map[row][col] == 0continue;
        
        if(flag == 1 && map[row][col] == temp[target][col]) {
          temp[target][col]*=2;
          flag = 0;
        } else {
          temp[++target][col] = map[row][col];
          flag = 1;
        }
        
      }
      // for(++target; target < N; ++target) temp[target][col] = 0; 
    }
    for(int row=0; row<N; ++row) for(int col=0; col<N; ++col) map[row][col] = temp[row][col]; 
  }
  
  static void getMax() { 
    for(int[] ar1 : map) for(int ar2 : ar1) maxVal = Math.max(maxVal, ar2); 
  }
  
  static void dfs(int count) { 
    
    if(count >= 5) {
      getMax();  
      return;
    } 
    
    int[][] arrCpy = new int[N][];
    for(int i=0; i<N; ++i) {
      arrCpy[i] = map[i].clone();
    }
    
    for(int dir=0; dir<4++dir) {
      up(); // 복사본 객체로 돌아가야한다.
      dfs(count + 1);
      rotate(); // 원본 객체로 돌아가야한다.
    } 
    for(int i=0; i<N; ++i) map[i] = arrCpy[i].clone();
    
  } 
  
  public static void main(String[] args) throws Exception {
    // Please Enter Your Code Here
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    N = Integer.parseInt(br.readLine());
    map = new int[N][];
    
    for(int i=0; i<N; ++i) {
      map[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    dfs(0);
    bw.write(String.valueOf(maxVal));
    
    br.close();
    bw.flush();
    bw.close();
  }
}
cs

 

// 다른사람 풀이 참조.. 우선 이방식을 익혀야겠다.

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
157
158
159
160
161
162
163
164
import java.util.*;
import java.io.*;
/*
10
8 8 4 16 32 0 0 8 8 8
8 8 4 0 0 8 0 0 0 0
16 0 0 16 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 16
0 0 0 0 0 0 0 0 0 2
*/
class Main{ 
  
  static int maxVal = Integer.MIN_VALUE;
  static int N;  
  static int map[][];
  
  static void playGame(int cnt) {
    
    if(cnt == 5) {findMax(); return;}
    
    int[][] cpy = new int[N][];
    for(int i=0; i<N; ++i) cpy[i] = map[i].clone();
    for(int i=0; i<4++i) {
      move(i); 
      playGame(cnt + 1);
      for(int k=0; k<N; ++k) map[k] = cpy[k].clone(); 
    }
  }
  
  static void move(int dir) {
    switch(dir) {
      // 위로 몰기
      case 0:
        // 열을 선택하고 행을 돌린다.
        for(int col=0; col<N; ++col) {
          int idx = 0, block = 0;
          for(int row=0; row<N; ++row) {
            
            if(map[row][col] != 0) {
              // 일치하는 경우가 발생한 경우
              if(block == map[row][col]) {
                map[idx - 1][col] = block * 2;
                block = 0;
                map[row][col] = 0;
              }
              // 일치하는 경우가 없는 경우
              else {
                block = map[row][col]; 
                map[row][col] = 0;
                map[idx++][col] = block;
              } 
            } 
          }
        }
        
      break;
      
      // 아래쪽으로 몰기
      case 1:
        for(int col=0; col<N; ++col) {
          int idx = N - 1, block = 0;
          for(int row=- 1; row > -1--row) {
            
            if(map[row][col] != 0) { 
              if(block == map[row][col]) {
                map[idx + 1][col] = block * 2;
                block = 0;
                map[row][col] = 0;
              }
              else {
                block = map[row][col];
                map[row][col] = 0;
                map[idx--][col] = block;
              }
              
            }
            
          }
        }
      break;
      
      // 왼쪽으로 몰기
      case 2:
        for(int row=0; row<N; ++row) {
          int idx=0, block=0;
          for(int col=0; col<N; ++col) {
            
            if(map[row][col] != 0) {
              if(block == map[row][col]) {
                map[row][idx - 1= block * 2;
                block = 0;
                map[row][col] = 0;
              }
              else {
                block = map[row][col];
                map[row][col] = 0;
                map[row][idx++= block;
              }
            }
            
          }
        }
      break;
      
      // 오른쪽으로 몰기
      case 3:
        for(int row=0; row<N; ++row) {
          int idx=N-1, block=0;
          for(int col=N-1; col>=0--col) {
            
            if(map[row][col] != 0) {
              
              if(block == map[row][col]) {
                map[row][idx + 1= block * 2;
                block = 0;
                map[row][col] = 0;
              }
              else {
                block = map[row][col];
                map[row][col] = 0;
                map[row][idx--= block;
              }
              
            }
            
          }
        }
      break;
      
    }
  }
  
  static void findMax() {
    for(int[] ar1 : map) 
      for(int ar2 : ar1)
        maxVal = Math.max(maxVal, ar2);
    
  }
  
  public static void main(String[] args) throws Exception {
    // Please Enter Your Code Here
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    N = Integer.parseInt(br.readLine());
    map = new int[N][];
    
    for(int i=0; i<N; ++i) {
      map[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    } 
    
    playGame(0);
    
    bw.write(String.valueOf(maxVal));
    br.close();
    bw.flush();
    bw.close();
  }
}
cs

 

반응형