본문 바로가기

알고리즘/BOJ

14503번

14503번 - 로봇 청소기


내가 항상 뭔가 겁먹는 시뮬레이션 문제.. 다시 풀어보는 문제임에도 불구하고, 실수가 쪼금씩 발생했다.


방향에 따라 크게 한번 나누고,


1. 왼쪽방향으로 갈 수 있는지 탐색하고 왼쪽방향으로 이동


2. 내 주변 4방향 탐색해서 갈 수 있는 곳이 하나라도 있으면 , 방향만 바꿔주기


3. 내 주변 4방향 탐색해서 갈 수 없으면, 뒤에가 벽인지 탐색


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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
 
int n, m, r, c, d;
int map[51][51];
int turn[4= { 3,0,1,2 };
int dx[4= { 1,-1,0,0 };
int dy[4= { 0,0,1,-1 };
 
 
int main()
{
    //freopen("input.txt", "r", stdin);
 
    ios::sync_with_stdio(false);
    cin.tie(NULL);
 
    cin >> n >> m >> r >> c >> d;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> map[i][j];
        }
    }
 
    int x = r, y = c;
    int ans = 0;
    while (true)
    {
        map[x][y] = 2;
 
        if (d == 0)
        {
            if (y - 1 >= 0 && map[x][y - 1== 0)
            {
                d = turn[d];
                y = y - 1;
                continue;
            }
 
            bool chk = true;
            for (int i = 0; i < 4; i++)
            {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && ny >= 0 && nx < n && ny < m && map[nx][ny] == 0)
                {
                    chk = false;
                    break;
                }
            }
 
            if (!chk)
            {
                d = turn[d];
                continue;
            }
 
            if (x + 1 >= n || map[x + 1][y] == 1)
            {
                break;
            }
 
            x = x + 1;
 
        }
        else if (d == 1)
        {
            if (x - 1 >= 0 && map[x-1][y] == 0)
            {
                d = turn[d];
                x = x - 1;
                continue;
            }
 
            bool chk = true;
            for (int i = 0; i < 4; i++)
            {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && ny >= 0 && nx < n && ny < m && map[nx][ny] == 0)
                {
                    chk = false;
                    break;
                }
            }
 
            if (!chk)
            {
                d = turn[d];
                continue;
            }
 
            if (y - 1 < 0 || map[x][y-1== 1)
            {
                break;
            }
 
            y = y - 1;
        }
        else if (d == 2)
        {
            if (y+1 < m && map[x][y+1== 0)
            {
                d = turn[d];
                y = y + 1;
                continue;
            }
 
            bool chk = true;
            for (int i = 0; i < 4; i++)
            {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && ny >= 0 && nx < n && ny < m && map[nx][ny] == 0)
                {
                    chk = false;
                    break;
                }
            }
 
            if (!chk)
            {
                d = turn[d];
                continue;
            }
 
            if (x - 1 < 0 || map[x-1][y] == 1)
            {
                break;
            }
 
            x = x - 1;
        }
        else if (d == 3)
        {
            if (x + 1 < n && map[x + 1][y] == 0)
            {
                d = turn[d];
                x = x + 1;
                continue;
            }
 
            bool chk = true;
            for (int i = 0; i < 4; i++)
            {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && ny >= 0 && nx < n && ny < m && map[nx][ny] == 0)
                {
                    chk = false;
                    break;
                }
            }
 
            if (!chk)
            {
                d = turn[d];
                continue;
            }
 
            if (y + 1 >= m || map[x][y + 1== 1)
            {
                break;
            }
 
            y = y + 1;
            
        }
        
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (map[i][j] == 2)
            {
                ans++;
            }
        }
    }
 
    cout << ans << endl;
 
    return 0;
}
cs


반응형

'알고리즘 > BOJ' 카테고리의 다른 글

2839번  (2) 2018.03.16
1239번  (0) 2018.03.11
14502번  (2) 2018.03.11
14890번  (2) 2018.03.08
3967번  (0) 2018.03.07