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

삼성 SW 역량 테스트 기출 문제 : 로봇 청소기

Jedy_Kim 2021. 11. 5. 12:02
728x90

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

 

14503번: 로봇 청소기

로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어

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
import java.util.*;
import java.io.*
 
 
public class Main { 
  
  // 북, 동, 남, 서
  static final int[][] DELTAS = { {-10}, {01}, {10}, {0-1} };
  static int N, M, R, C, D;
  static int[][] map;
  
  
  // 로봇 청소기
  static class Robot {
    
    /* 멤버변수 */
    // row, col 좌표, dir 방향, cleaned 청소구역 개수
    int row, col, dir, cleaned;
    
    /* 생성자 */
    public Robot( int row, int col, int dir ) {
      this.row     = row;
      this.col     = col;
      this.dir     = dir;
      this.cleaned = 0;
    }
    
    /* 기능 메서드 */ 
    // 청소
    public void clean(int r, int c) {
      this.row = r;
      this.col = c;
      // 청소를 한다.
      map[r][c] = 2;
      // 청소구역 수를 +1 해준다.
      ++this.cleaned;
    }
    
    // 회전 - 반시계
    public void rotate() {
      --dir;
      if( dir < 0 ) dir = 3;
    }
    
    // 후진이 가능한지 판단 - 벽인지만 판단해준다.
    public boolean back() {
      this.row -= DELTAS[this.dir][0];
      this.col -= DELTAS[this.dir][1];
      if( map[this.row][this.col] == 1 ) return false;
      return true;
    }
    
  }
  
  
  // main
  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)); 
    StringTokenizer st = new StringTokenizer(br.readLine());
    N   = Integer.parseInt(st.nextToken());
    M   = Integer.parseInt(st.nextToken());
    st  = new StringTokenizer(br.readLine());
    R   = Integer.parseInt(st.nextToken());
    C   = Integer.parseInt(st.nextToken());
    D   = Integer.parseInt(st.nextToken());
    map = new int[N][];
    for(int i=0; i<N; ++i) map[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    /* 입력 완료 */
    
    // 로봇 청소기 생성
    Robot cleaner = new Robot(R, C, D);
    // 현재 위치를 청소
    cleaner.clean(R, C);
    // 가능한 모든 부분을 청소 시킨다.
    outer : while(true) {
      // 4방 탐색
      for(int d=0; d<4++d) {
        // 왼쪽으로 회전해보기
        cleaner.rotate();
        // 해당 방향으로 갈 수 있는지 판다.
        int ny = cleaner.row + DELTAS[cleaner.dir][0];
        int nx = cleaner.col + DELTAS[cleaner.dir][1];
        // 없으면 필터링
        if( map[ny][nx] != 0 ) continue;
        // 있다면 : 한칸 앞으로 진행
        cleaner.clean(ny, nx);
        // 나머지 방향은 볼 필요가 없다
        continue outer;
      }
      // continue outer로 못갔을 경우 (4방 모두 청소를 했거나 벽인 경우) -> 후진가능여부를 판단한다.
      // 후진이 불가능하면 종료시킨다.
      if!cleaner.back() ) break outer;
    }
    
    bw.write(String.valueOf(cleaner.cleaned));
    bw.flush();
    bw.close();
    br.close();
  } 
cs
반응형