ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] 삼성 1953. [모의 SW 역량테스트] 탈주범 검거
    코딩테스트/코딩테스트 연습 2020. 10. 27. 18:10
    728x90
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<string.h>
    using namespace std;
     
    int turnal[51][51] = { 0, };
    bool visited[51][51] = { false, };
    int dir[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
    int N, M;
     
    bool connected(int before_dir, int type) {
        if (before_dir == 0) {
            if (type == 1 || type == 3 || type == 6 || type == 7) {
                return true;
            }
        }
        else if (before_dir == 1) {
            if (type == 1 || type == 2 || type == 4 || type == 7) {
                return true;
            }
        }
        else if (before_dir == 2) {
            if (type == 1 || type == 3 || type == 4 || type == 5) {
                return true;
            }
        }
        else if (before_dir == 3) {
            if (type == 1 || type == 2 || type == 5 || type == 6) {
                return true;
            }
        }
        return false;
    }
     
    int solution(int r, int c, int time) {
        queue<pair<pair<int, int>, int>> q;
        q.push(make_pair(make_pair(r, c), 1));
     
        while (!q.empty()) {
            int x = q.front().first.first;
            int y = q.front().first.second;
            int count = q.front().second;
            q.pop();
            if (count > time) break;
            visited[x][y] = true;
            
            if (turnal[x][y] == 1) {
                for (int i = 0; i < 4; i++) {
                    int new_x = x + dir[i][0];
                    int new_y = y + dir[i][1];
                    if (new_x < 0 || new_y < 0 || new_x >= N || new_y >= M) continue;
                    if (!visited[new_x][new_y] && connected(i, turnal[new_x][new_y]))
                        q.push(make_pair(make_pair(new_x, new_y), count + 1));
                }
            }
            else if (turnal[x][y] == 2) {
                for (int i = 1; i < 4; i+=2) {
                    int new_x = x + dir[i][0];
                    int new_y = y + dir[i][1];
                    if (new_x < 0 || new_y < 0 || new_x >= N || new_y >= M) continue;
                    if (!visited[new_x][new_y] && connected(i, turnal[new_x][new_y]))
                        q.push(make_pair(make_pair(new_x, new_y), count + 1));
                }
            }
            else if (turnal[x][y] == 3) {
                for (int i = 0; i < 3; i+=2) {
                    int new_x = x + dir[i][0];
                    int new_y = y + dir[i][1];
                    if (new_x < 0 || new_y < 0 || new_x >= N || new_y >= M) continue;
                    if (!visited[new_x][new_y] && connected(i, turnal[new_x][new_y]))
                        q.push(make_pair(make_pair(new_x, new_y), count + 1));
                }
            }
            else if (turnal[x][y] == 4) {
                for (int i = 0; i < 4; i+=3) {
                    int new_x = x + dir[i][0];
                    int new_y = y + dir[i][1];
                    if (new_x < 0 || new_y < 0 || new_x >= N || new_y >= M) continue;
                    if (!visited[new_x][new_y] && connected(i, turnal[new_x][new_y]))
                        q.push(make_pair(make_pair(new_x, new_y), count + 1));
                }
            }
            else if (turnal[x][y] == 5) {
                for (int i = 0; i < 2; i++) {
                    int new_x = x + dir[i][0];
                    int new_y = y + dir[i][1];
                    if (new_x < 0 || new_y < 0 || new_x >= N || new_y >= M) continue;
                    if (!visited[new_x][new_y] && connected(i, turnal[new_x][new_y]))
                        q.push(make_pair(make_pair(new_x, new_y), count + 1));
                }
            }
            else if (turnal[x][y] == 6) {
                for (int i = 1; i < 3; i++) {
                    int new_x = x + dir[i][0];
                    int new_y = y + dir[i][1];
                    if (new_x < 0 || new_y < 0 || new_x >= N || new_y >= M) continue;
                    if (!visited[new_x][new_y] && connected(i, turnal[new_x][new_y]))
                        q.push(make_pair(make_pair(new_x, new_y), count + 1));
                }
            }
            else if (turnal[x][y] == 7) {
                for (int i = 2; i < 4; i++) {
                    int new_x = x + dir[i][0];
                    int new_y = y + dir[i][1];
                    if (new_x < 0 || new_y < 0 || new_x >= N || new_y >= M) continue;
                    if (!visited[new_x][new_y] && connected(i, turnal[new_x][new_y]))
                        q.push(make_pair(make_pair(new_x, new_y), count + 1));
                }
            }
        }
     
        int answer = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (visited[i][j] == true)
                    answer++;
            }
        }
        return answer;
    }
     
    int main() {
        int T;
        cin >> T;
        for (int t = 1; t <= T; t++) {
            memset(turnal, 0, sizeof(turnal));
            memset(visited, false, sizeof(visited));
            int R, C, L;
            cin >> N >> M >> R >> C >> L;
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    cin >> turnal[i][j];
                }
            }
            cout << "#" << t << " " << solution(R, C, L) << endl;;
        }
        return 0;
    }
    728x90

    댓글

Designed by Tistory.