본문 바로가기
Programming/Algorithm

백준 14503 로봇 청소기

by OKOK 2018. 4. 14.

1. 설계 완벽하게 하기(예제 3개 돌리기)

2. 초기화 조건 확인하기

3. 가지치기

4.  경우의 수 나열하기


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
/*
1. 설계 완벽하게 하기(예제3개돌리기)
2. 초기화 조건 확인하기
3. 가지치기
4. 경우의 수 나열하기
16:36
17:08
32분
로봇 청소기
*/
 
#include <iostream>
#include <algorithm>
using namespace std;
 
#define SIZE 55 // 55
int N, M;
int map[SIZE][SIZE];
int startX, startY, startDir;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };
int x, y, nx, ny, dir;
int clean_cnt;
int turn_cnt;
 
void problemIn() {
    cin >> N >> M;
    cin >> startX >> startY >> startDir;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> map[i][j];
        }
    }
}
 
void init() {
 
}
 
void solve() {
 
    x = startX;
    y = startY;
    dir = startDir;
    clean_cnt++;
 
    while (1) {
 
        map[x][y] = 2// 청소는 2로 표시합니다. 1은 벽 0은 청소안된곳
 
        dir = (dir + 3) % 4;
        turn_cnt++;
        nx = x + dx[dir];
        ny = y + dy[dir];
        
        if (turn_cnt == 5) {
            dir = (dir - 3 + 4) % 4;
            dir = (dir + 2) % 4;
            nx = x + dx[dir];
            ny = y + dy[dir];
            if (nx < 0 || ny < 0 || nx >= N || ny >= M || map[nx][ny] == 1break;
            x = nx;
            y = ny;
            dir = (dir + 2) % 4;
            turn_cnt = 0;
            continue;
        }
 
        if (nx < 0 || ny < 0 || nx >= N || ny >= M) {
            continue;
        }
        else if (map[nx][ny] == 0) {
            turn_cnt = 0;
            clean_cnt++;
            x = nx;
            y = ny;
            continue;
        }
        else if (map[nx][ny] != 0) {
            continue;
        }
    }
}
 
int main() {
    problemIn();
    solve();
    cout << clean_cnt << endl;
    return 0;
}
 
cs