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

 

BOJ 14503번 로봇 청소기 문제입니다.

 

처음에는 이동가능한 곳 중에서 청소할 수 있는 모든 곳을 해야된다고 생각했습니다.

그래서 방문했던 곳(청소 한곳) 이여도 이 곳을 다시 방문하면 다른 방향에 청소를 할 수 있는 부분이 있을거라고

생각하고 문제를 풀었습니다.

 

문제를 잘못이해해서 예제출력부터 다르길래 다시 읽어봤습니다.

청소를 한 곳은 후진을 하지 않는 이상 이동을 하지 않는다는 조건이 있습니다...

 

딱히 어렵지 않으니 문제에서 요구하는대로 푸시면 될 것 같습니다.

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits>
#include <cstring>
using namespace std;
 
struct direct {
    int y, x;
}direct[4];
 
int N, M;
int r, c, d;
bool arr[52][52];
int visit[52][52];
 
void dfs(int y, int x, int dt, int cnt) {
    visit[y][x] = 1;
    int go;
    bool check = false;
    for (int i = 1; i <= 4; i++) {
        go = (dt+i)%4;
        int yy = y + direct[go].y;
        int xx = x + direct[go].x;
        if (yy>=0 && yy<&& xx>=0 && xx<&& !arr[yy][xx] && !visit[yy][xx]) {
            check = true;
            dfs(yy, xx, go, cnt + 1);
            return;
        }
    }
 
    if (!check) {
        int yy = y;
        int xx = x;
        if (dt == 0) xx -= 1;
        if (dt == 1) yy++;
        if (dt == 2) xx++;
        if (dt == 3) yy--;
 
        if (yy >= 0 && yy < N && xx >= 0 && xx < M && !arr[yy][xx])
            dfs(yy, xx, dt, cnt);
        else {
            cout << cnt;
        }
    }
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    direct[0= { 0,1 };
    direct[1= { -1,0 };
    direct[2= { 0,-1 };
    direct[3= { 1,0 };
 
    cin >> N >> M;
    cin >> r >> c >> d;
 
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            cin >> arr[i][j];
 
    int dt;
    if (d == 0) dt = 1;
    if (d == 1) dt = 0;
    if (d == 2) dt = 3;
    if (d == 3) dt = 2;
    dfs(r, c, dt, 1);
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

'algorithm > BOJ' 카테고리의 다른 글

BOJ 1063번 킹  (0) 2020.04.07
BOJ 14499번 주사위 굴리기  (0) 2020.03.14
BOJ 3709번 레이저빔은 어디로  (0) 2020.03.13
BOJ 1043번 거짓말  (0) 2020.03.13
BOJ 1074번 Z  (0) 2020.03.05

+ Recent posts