bfs 문제다.


visit배열은 c[x][y][w][mem]으로 체크해주면 된다.


'?'를 만나면 현재의 mem을 가지고 4방향으로 진행했을 때의 좌표를 가지고 visit배열을 참조해서 안간쪽으로만 queue에 넣어주면 된다.


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
#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;
 
struct p {
    int x, y, w, mem;
};
 
int t, tc, n, m;
int dx[] = { -1,0,1,0 }, dy[] = { 0,1,0,-1 }, c[21][21][4][16];
char a[21][21];
queue<p> q;
 
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> t;
    for (tc = 1; tc <= t; ++tc) {
        cin >> n >> m;
        bool able = false;
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
            if (!able) {
                for (int j = 0; j < m; ++j) 
                    if (a[i][j] == '@') able = true;
            }
        }
        if (able) q.push({ 0,0,1,0 });
        c[0][0][1][0= tc;
        int f = 0;
        while (q.size()) {
            int x = q.front().x;
            int y = q.front().y;
            int w = q.front().w;
            int mem = q.front().mem;
            q.pop();
            if (a[x][y] == '<') w = 3;
            else if (a[x][y] == '>') w = 1;
            else if (a[x][y] == '^') w = 0;
            else if (a[x][y] == 'v') w = 2;
            else if (a[x][y] == '_') w = mem == 0 ? 1 : 3;
            else if (a[x][y] == '|') w = mem == 0 ? 2 : 0;
            else if (a[x][y] == '?') {
                for (int i = 0; i < 4++i) {
                    int nx = (x + dx[i] + n) % n;
                    int ny = (y + dy[i] + m) % m;
                    if (c[nx][ny][i][mem] == tc) continue;
                    c[nx][ny][i][mem] = tc;
                    q.push({ nx,ny,i,mem });
                }
                continue;
            }
            else if (a[x][y] == '@') {
                f = 1;
                while (q.size()) q.pop();
                break;
            }
            else if (a[x][y] >= '0' && a[x][y] <= '9') mem = a[x][y] - '0';
            else if (a[x][y] == '+') mem = (mem + 1) % 16;
            else if (a[x][y] == '-') mem = (mem + 15) % 16;
 
            x = (x + dx[w] + n) % n;
            y = (y + dy[w] + m) % m;
            if (c[x][y][w][mem] == tc) continue;
            c[x][y][w][mem] = tc;
            q.push({ x,y,w,mem });
        }
        
        cout << '#' << tc << ' ' << (f ? "YES" : "NO"<< '\n';
    }
    return 0;
}
cs


'SWEA' 카테고리의 다른 글

5785. 살아남은 투자가는 누구일까  (0) 2018.10.16
5789. 현주의 상자 바꾸기  (0) 2018.10.16
2112. 보호 필름  (0) 2018.10.08
4740. 밍이의 블록게임  (0) 2018.10.07
핀볼 런타임 에러나는 TC  (0) 2018.10.07

+ Recent posts