ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [모의 SW 역량테스트] 탈주범 검거
    SWexpertAcademy 2019. 9. 29. 21:49

    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
    #include<iostream>
    #include<queue>
    #include<utility>
    #include<cstring>
     
    using namespace std;
     
    int tc,n,m,R,C,L;
    int map[51][51];
    int d[51][51];
    bool dist[51][51];
    int dx[8][4= {{0,0,0,0}, {0,0,1,-1},{1,-1,0,0},{0,0,0,0},{-1,0,0,0},{1,0,0,0},
    {1,0,0,0},{-1,0,0,0 }};
    int dy[8][4= { {0,0,0,0}, {1,-1,0,0},{0,0,0,0},{1,-1,0,0},{0,1,0,0},{0,1,0,0},
    {0,-1,0,0},{0,-1,0,0}};
    int ans;
     
    bool canbe(int nx, int ny,int x, int y) {
        for (int i = 0; i < 4; i++) {
            if (map[nx][ny] == 0continue;
            int sx = nx + dx[map[nx][ny]][i]; int sy = ny + dy[map[nx][ny]][i];
            if (sx >= 0 && sx < n && sy >= 0 && sy < m) {
                if (sx == x && sy == y) {
                    return true;
                }
            }
     
        }
        return false;
    }
     
    void bfs(int x, int y) {
        queue<pair<intint> > q;
        q.push(make_pair(x, y));
        int s = 1;
        dist[x][y] = true;
        d[x][y] = 1;
        if (L > 1) {
            while (!q.empty()) {
                x = q.front().first; y = q.front().second;
                int val = map[x][y];
                q.pop();
                for (int i = 0; i < 4; i++) {
                    int nx = x + dx[val][i]; int ny = y + dy[val][i];
                    if (nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] != 0) {
                        if (d[nx][ny] == 0 && canbe(nx, ny, x, y) && dist[nx][ny] == false) {
                            d[nx][ny] = d[x][y] + 1;
                            dist[nx][ny] = true;
                            s++;
                            if (d[nx][ny] < L) {
                                q.push(make_pair(nx, ny));
                            }
                        }
                    }
                }
     
            }
        }
        ans = s;
    }
     
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cin >> tc;
        for (int t = 1; t <= tc; t++) {
            cin >> n >> m >> R >> C >> L;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    cin >> map[i][j];
                }
            }
            bfs(R, C);
     
            cout << '#' <<t<< ' ' << ans << '\n';
            memset(d, 0sizeof(d));
            memset(dist, falsesizeof(dist));
            memset(map, 0sizeof(map));
        }
        return 0;
    }
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
     

    알고리즘에서 BFS,DFS 개념 자체는 어렵지않다.

    어려운것은 조건을 잘보고 그에 따라 구현할줄알아야한다

    이번에는 시간 L 이 1일때를 고려하지않아서 한번 틀린문제

    댓글

Designed by Tistory.