먼지의삶 2019. 10. 9. 03:29

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

 

1726번: 로봇

많은 공장에서 로봇이 이용되고 있다. 우리 월드 공장의 로봇은 바라보는 방향으로 궤도를 따라 움직이며, 움직이는 방향은 동, 서, 남, 북 가운데 하나이다. 로봇의 이동을 제어하는 명령어는 다음과 같이 두 가지이다. 명령 1. Go k   - k는 1, 2 또는 3일 수 있다. 현재 향하고 있는 방향으로 k칸 만큼 움직인다. 명령 2. Turn dir   - dir은 left 또는 right 이며, 각각 왼쪽 또는 오른쪽으로 90° 회전한다. 공장 내 궤

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
#include<iostream>
#include<queue>
#include<utility>
 
using namespace std;
 
struct robot {
    int x, y, dir, cnt;
    robot(int x, int y, int dir, int cnt) {
        this->= x;
        this->= y;
        this->dir = dir;
        this->cnt = cnt;
    }
};
int m, n, ex, ey, ed;
int map[101][101];
int dist[100][100];
bool dos[100][100][2];
int ans = 987654321;
robot rb = robot(0000);
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
 
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> m >> n;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> map[i][j];
        }
    }
    int a, b, c;
    cin >> a >> b >> c;
    rb = robot(a, b, c - 10);
    cin >> ex >> ey >> ed;
    ed--;
 
    queue<robot > q;
    q.push(rb);
    while (!q.empty()) {
        int x = q.front().x;
        int y = q.front().y;
        int d = q.front().dir;
        int cnt = q.front().cnt;
        //cout << x << ' ' << y << ' ' << d << ' ' << cnt << '\n';
        q.pop();
        if (ex == x && ey == y && ed == d) {
            ans = cnt; break;
        }
        if (ex == x && ey == y) {
            for (int i = 0; i < 4; i++) {
                if (d == 0 && i == 1 && i == ed) {
                    ans = ans > cnt + 2 ? cnt + 2 : ans;
                    break;
                }
                if (d == 1 && i == 0 && i == ed) {
                    ans = ans > cnt + 2 ? cnt + 2 : ans;
                    break;
                }
                if (d == 2 && i == 3 && i == ed) {
                    ans = ans > cnt + 2 ? cnt + 2 : ans;
                    break;
                }
                if (d == 3 && i == 2 && i == ed) {
                    ans = ans > cnt + 2 ? cnt + 2 : ans;
                    break;
                }
                if (i == ed) {
                    ans = ans > cnt + 1 ? cnt + 1 : ans;
                    break;
                }
 
            }
        }
        int sx = x; int sy = y;
        for (int i = 0; i < 3; i++) {
            sx += dx[d]; sy += dy[d];
            int nx = sx; int ny = sy;
            if (nx <= 0 || nx > m || ny <= 0 || ny > n)continue;
            if (map[nx][ny] == 1break;
            if (dist[nx][ny] == 0) {
                dist[nx][ny] = cnt + 1;
                q.push(robot(nx, ny, d, cnt + 1));
            }
            else if (dist[nx][ny] > cnt + 1) {
                dist[nx][ny] = cnt + 1;
                q.push(robot(nx, ny, d, cnt + 1));
            }
        }
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i]; int ny = y + dy[i];
            if (nx <= 0 || nx > m || ny <= 0 || ny > n)continue;
            if (i == d) continue;
            if (dos[x][y][1== false) {
                if (d == 0 && i == 1) q.push(robot(x, y, i, cnt + 2));
                else if (d == 1 && i == 0)q.push(robot(x, y, i, cnt + 2));
                else if (d == 2 && i == 3)q.push(robot(x, y, i, cnt + 2));
                else if (d == 3 && i == 2)q.push(robot(x, y, i, cnt + 2));
                else q.push(robot(x, y, i, cnt + 1));
            }
        }dos[x][y][1= true;
 
 
    }
 
    /*for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            cout << dist[i][j] << ' ';
        }cout << '\n';
    }*/
    cout << ans << '\n';
    return 0;
 
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

BFS문제, 근데 함정이 좀 많다.

직진할때, 갈수없는곳으로가면 멈춰야하고, 회전하는 움직임일때는, 뒤로가는방향도 포함이될순있는데, 이때 가중치가 2가된다는것, 이것을 확인하기위해서 그냥 bool배열을 처음에 3차원으로만들었다.

2차원으로도 충분하며, 돌때만 확인해주면된다.