반응형

www.acmicpc.net/problem/3190

 

3190번: 뱀

 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임

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
#include <iostream>
#include <queue>
#define pii pair<intint>
#define rep(i,n) for(int i=1;i<=n;i++)
using namespace std;
int arr[101][101], n, k, a, b, l, x;
int dx[] = { 010-1 };
int dy[] = { 10-10 };
char c, turn[10001];
bool visited[101][101];
queue<pii> q;
void dfs(int x, int y, int direction, int sec) {
    // 종료 조건
    // 1. 벽에 부딪힘
    // 2. 자기 몸에 부딪힘
    if (x <= 0 || x > n || y <= 0 || y > n || visited[x][y]) {
        cout << sec;
        exit(0);
    }
 
    // 머리 방문
    visited[x][y] = 1;
    q.push({ x, y });
 
    // 사과 없으면 꼬리 줄이기
    if (!arr[x][y]) {
        int tx = q.front().first;
        int ty = q.front().second;
        q.pop();
        visited[tx][ty] = 0;
    }
 
    // 사과 한 번 먹으면 더 이상 못 먹음
    arr[x][y] = 0;    
 
    // 방향 전환
    int next_direction = direction;
    if (turn[sec] == 'L'
        next_direction = (direction + 3) % 4;
    else if(turn[sec] == 'D')
        next_direction = (direction + 1) % 4;
 
    // 머리 다음 위치
    int nx = x + dx[next_direction];
    int ny = y + dy[next_direction];
    dfs(nx, ny, next_direction, sec + 1);
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    rep(i, k) {
        cin >> a >> b;
        arr[a][b] = 1;
    }
    cin >> l;
    rep(i, l) {
        cin >> x >> c;
        turn[x] = c;
    }
    q.push({ 1,1 });
    visited[1][1= 1;
    dfs(1201);
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 15683  (0) 2021.04.30
백준 13458  (0) 2021.04.29
백준 13460  (0) 2021.03.07
백준 12100 [복습 필수]  (0) 2021.03.03
백준 20055  (0) 2021.02.23
반응형

www.acmicpc.net/problem/13460

 

13460번: 구슬 탈출 2

첫 번째 줄에는 보드의 세로, 가로 크기를 의미하는 두 정수 N, M (3 ≤ N, M ≤ 10)이 주어진다. 다음 N개의 줄에 보드의 모양을 나타내는 길이 M의 문자열이 주어진다. 이 문자열은 '.', '#', 'O', 'R', 'B'

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
#include <iostream>
#include <queue>
#define pii pair<intint>
#define ppii pair<pii, pii>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, m, cnt;
char arr[11][11];
int dx[] = { -1100 };
int dy[] = { 00-11 };
int visited[10][10][10][10];
queue<ppii> q;
pii r, b, hole;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    rep(i, n) {
        rep(j, m) {
            cin >> arr[i][j];
            if (arr[i][j] == 'R')
                r = { i,j };
            else if (arr[i][j] == 'B')
                b = { i,j };
            else if (arr[i][j] == 'O')
                hole = { i,j };
        }
    }
 
    q.push({ r, b });
    visited[r.first][r.second][b.first][b.second] = 1;
    while (!q.empty()) {
        if (cnt > 10) {
            cout << -1;
            exit(0);
        }
        int size = q.size(); 
        while (size--) {
            pii now_r = q.front().first;
            pii now_b = q.front().second;
            q.pop();
            
            // 빨간 공 O 도착했으면????
            if (arr[now_r.first][now_r.second] == 'O' && arr[now_b.first][now_b.second] != 'O') {
                cout << cnt;
                exit(0);
            }
 
            rep(i, 4) {
                int nrx = now_r.first;
                int nry = now_r.second;
                int nbx = now_b.first;
                int nby = now_b.second;
 
                // 다음 칸이 벽 아니면 이동
                while (arr[nrx + dx[i]][nry + dy[i]] != '#') {
                    if (arr[nrx][nry] == 'O'break;
                    nrx += dx[i];
                    nry += dy[i];
                }
 
                // 다음 칸이 벽 아니면 이동
                while (arr[nbx + dx[i]][nby + dy[i]] != '#') {
                    if (arr[nbx][nby] == 'O'break;
                    nbx += dx[i];
                    nby += dy[i];
                }
 
 
                // 빨간공, 파란공 겹치면????????    -> 이 부분 때문에 못 풂
                if (nrx == nbx && nry == nby) {
                    if (arr[nrx][nry] != 'O') {
                        // 더 많이 이동한 공을 한 칸 뒤로 이동
                        int r_dist = abs(nrx - now_r.first) + abs(nry - now_r.second);
                        int b_dist = abs(nbx - now_b.first) + abs(nby - now_b.second);
                        if (r_dist > b_dist) {
                            nrx -= dx[i];
                            nry -= dy[i];
                        }
                        else {
                            nbx -= dx[i];
                            nby -= dy[i];
                        }
                    }
                }
                
                // 이미 방문했으면 continue
                if (visited[nrx][nry][nbx][nby]) continue;
 
                // 방문 처리 + 큐에 삽입
                visited[nrx][nry][nbx][nby] = 1;
                q.push({ {nrx, nry}, {nbx, nby} });
            }
        }
        cnt++;
    }
    cout << -1;
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 13458  (0) 2021.04.29
백준 3190  (0) 2021.04.29
백준 12100 [복습 필수]  (0) 2021.03.03
백준 20055  (0) 2021.02.23
백준 15685 [복습 필수]  (0) 2021.02.23
반응형

www.acmicpc.net/problem/12100

 

12100번: 2048 (Easy)

첫째 줄에 보드의 크기 N (1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 게임판의 초기 상태가 주어진다. 0은 빈 칸을 나타내며, 이외의 값은 모두 블록을 나타낸다. 블록에 쓰여 있는 수는 2

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
// 한 쪽으로 옮기기
void move() {
    int temp[21][21];
    // 열 단위(세로줄)로 처리
    rep(j, n) {
        bool flag = 0;    // 합칠 수 있는가?
        int row = -1;    // temp[][]의 현재 row
        rep(i, n) {
            if (arr[i][j] == 0continue;
            if (flag && arr[i][j] == temp[row][j]) {    // 합칠 수 있음 + arr[i] == temp[row]
                temp[row][j] *= 2;                        // 현재 row에서 합치기
                flag = 0;                                // 합칠 수 없도록 변경
            }
            else {
                temp[++row][j] = arr[i][j];                // 현재 row에서 못 합치므로, 다음 row로 이동
                flag = 1;                                // 합칠 수 있도록 변경
            }
        }
 
        for (int k = row + 1; k < n; k++)                // 빈 칸 채우기
            temp[k][j] = 0;
            
    }
 
    rep(i, n) {
        rep(j, n)
            arr[i][j] = temp[i][j];
    }
}
cs

 

 

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
#include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, ans;
struct board {
    int arr[21][21];
    
    // 90도 회전
    void rotate() {
        int temp[21][21];
        rep(i, n) {
            rep(j, n)
                temp[i][j] = arr[n - j - 1][i];
        }
 
        rep(i, n) {
            rep(j, n)
                arr[i][j] = temp[i][j];
        }
    }
 
    // 한 쪽으로 옮기기
    void move() {
        int temp[21][21];
        // 열 단위(세로줄)로 처리
        rep(j, n) {
            bool flag = 0;    // 합칠 수 있는가?
            int row = -1;    // temp[][]의 현재 row
            rep(i, n) {
                if (arr[i][j] == 0continue;
                if (flag && arr[i][j] == temp[row][j]) {    // i: 계속 증가, row: 조건 맞으면 증가
                    temp[row][j] *= 2;                        // 현재 row에서 합치기
                    flag = 0;
                }
                else {
                    temp[++row][j] = arr[i][j];                // 현재 row에서 못 합치므로, 다음 row로 이동
                    flag = 1;
                }
            }
 
            for (int k = row + 1; k < n; k++)                // 빈 칸 채우기
                temp[k][j] = 0;
            
        }
 
        rep(i, n) {
            rep(j, n)
                arr[i][j] = temp[i][j];
        }
    }
 
    // 최댓값
    int getMax() {
        int ret = 0;
        rep(i, n) {
            rep(j, n)
                ret = max(ret, arr[i][j]);
        }
        return ret;
    }
};
 
void dfs(board b, int cnt) {
    if (cnt == 5) {
        ans = max(ans, b.getMax());
        return;
    }
 
    rep(i, 4) {
        // 원본을 이동시키면 안됨
        board next = b;
        next.move();
        dfs(next, cnt + 1);
        // 원본 회전시키기
        b.rotate();
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    board b;
    cin >> n;
    rep(i, n) {
        rep(j, n)
            cin >> b.arr[i][j];
    }
    dfs(b, 0);
    cout << ans;
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 3190  (0) 2021.04.29
백준 13460  (0) 2021.03.07
백준 20055  (0) 2021.02.23
백준 15685 [복습 필수]  (0) 2021.02.23
백준 14503  (0) 2021.02.22
반응형

www.acmicpc.net/problem/20055

 

20055번: 컨베이어 벨트 위의 로봇

길이가 N인 컨베이어 벨트가 있고, 길이가 2N인 벨트가 이 컨베이어 벨트를 위아래로 감싸며 돌고 있다. 벨트는 길이 1 간격으로 2N개의 칸으로 나뉘어져 있으며, 각 칸에는 아래 그림과 같이 1부

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
#include <iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, k, arr[201], cnt, ans;
int visited[201];
// 로봇이 움직이는 경우
// 1. 벨트가 움직일 때 같이 움직임
// 2. 벨트 회전한 후에 로봇이 스스로 움직임
void rotate() {
    int temp = arr[2 * n - 1];
    for (int i = 2 * n - 1; i > 0; i--
        arr[i] = arr[i-1];
    for (int i = n - 1; i > 0; i--)
        visited[i] = visited[i - 1];
    visited[0= 0;
    arr[0= temp;
 }
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    rep(i, 2 * n)
        cin >> arr[i];
    while (cnt < k) {
        ans++;
        // 1. 벨트 한 칸 회전
        rotate();
        // 2. 로봇 이동
        visited[n - 1= 0;    // 제일 끝 로봇은 바닥으로
        for (int i = n - 2; i >= 0; i--) {
            if (visited[i] && !visited[i + 1&& arr[i + 1> 0) {
                visited[i + 1= 1;
                visited[i] = 0;
                arr[i + 1]--;
                if (arr[i + 1== 0)
                    cnt++;
            }
        }
        // 3. 올라가는 위치에 로봇 올리기
        if (!visited[0&& arr[0> 0) {
            visited[0= 1;
            arr[0]--;
            if (arr[0== 0)
                cnt++;
        }
    }
    cout << ans;
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 13460  (0) 2021.03.07
백준 12100 [복습 필수]  (0) 2021.03.03
백준 15685 [복습 필수]  (0) 2021.02.23
백준 14503  (0) 2021.02.22
백준 14891  (0) 2021.02.22
반응형

www.acmicpc.net/problem/15685

 

15685번: 드래곤 커브

첫째 줄에 드래곤 커브의 개수 N(1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 드래곤 커브의 정보가 주어진다. 드래곤 커브의 정보는 네 정수 x, y, d, g로 이루어져 있다. x와 y는 드래곤 커

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
#include <iostream>
#include <stack>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
stack<int> s;
int n, x, y, d, g, arr[101][101];
int dx[] = { 0-101 };
int dy[] = { 10-10 };
int idx, direction[1025];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    rep(i, n) {
        idx = 0;
        cin >> y >> x >> d >> g;
 
        // 1. 방향 초기화
        direction[idx++= d;
        arr[x][y] = 1;
        rep(j, g) {        // 세대 별로 탐색
            for (int k = idx - 1; k >= 0; k--)
                direction[idx++= (direction[k] + 1) % 4;
        }
 
        // 2. 좌표 찍기
        rep(j, idx) {    // 모든 점 탐색
            x += dx[direction[j]];
            y += dy[direction[j]];
            if (x >= 0 && x <= 100 && y >= 0 && y <= 100)
                arr[x][y] = 1;
        }
    }
 
    // 3. 사각형 검사
    int ans = 0;
    rep(x, 100) {
        rep(y, 100) {
            if (arr[x][y] && arr[x][y + 1&& arr[x + 1][y] && arr[x + 1][y + 1])
                ans++;
        }
    }
    cout << ans;
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 12100 [복습 필수]  (0) 2021.03.03
백준 20055  (0) 2021.02.23
백준 14503  (0) 2021.02.22
백준 14891  (0) 2021.02.22
백준 14890  (0) 2021.02.21
반응형

www.acmicpc.net/problem/14503

 

14503번: 로봇 청소기

로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어

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
#include <iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int dx[] = { 0-101 };
int dy[] = { -1010 };
int n, m, r, c, d, ans, arr[51][51];    // 0: 청소 안됨, 1: 벽, 2: 청소 완료
void dfs(int x, int y, int dir) {
    if (arr[x][y] == 0) {                // 현재 칸 청소
        arr[x][y] = 2;
        ans++;
    }
    
    int i = 0;
    for (i = 0; i < 4; i++) {
        int nx = x + dx[dir];            // 왼쪽칸의 x좌표
        int ny = y + dy[dir];            // 왼쪽칸의 y좌표
        if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
            if (arr[nx][ny] == 0) {                // 왼쪽 청소 해야하면
                dir = (dir + 3) % 4;            // 왼쪽으로 회전
                dfs(nx, ny, dir);                // 왼쪽으로 이동
            }
        
            else                                // 왼쪽 청소 안해도 되면
                dir = (dir + 3) % 4;            // 회전시키기
        }
    }
    if (i == 4) {                                // 네 방향 확인했는데 청소할 곳 없으면
        dir = (dir + 3) % 4;                    // 처음 진입한 방향에서 후진
        int nx = x + dx[dir];                    // == 왼쪽으로 한 번 회전시킨 상태에서 왼쪽 방향으로 이동
        int ny = y + dy[dir];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
            if (arr[nx][ny] == 1) {                // 벽 만나면 즉시 종료
                cout << ans;
                exit(0);
            }
            dfs(nx, ny, dir);
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> r >> c >> d;
    rep(i, n) {
        rep(j, m)
            cin >> arr[i][j];
    }
    dfs(r, c, d);
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 20055  (0) 2021.02.23
백준 15685 [복습 필수]  (0) 2021.02.23
백준 14891  (0) 2021.02.22
백준 14890  (0) 2021.02.21
백준 14499  (0) 2021.02.21
반응형

www.acmicpc.net/problem/14891

 

14891번: 톱니바퀴

총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴

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
#include <iostream>
#include <cstring>
#define rep(i,n) for(int i=1;i<=n;i++)
using namespace std;
int arr[5][4][4], direction[5], k, n, dir, ans;
void rotate(int num, int dir) {
    int temp = arr[num][0][2];
    // 시계
    if (dir == 1) {
        arr[num][0][2= arr[num][0][1];
        arr[num][0][1= arr[num][0][0];
        arr[num][0][0= arr[num][1][0];
        arr[num][1][0= arr[num][2][0];
        arr[num][2][0= arr[num][2][1];
        arr[num][2][1= arr[num][2][2];
        arr[num][2][2= arr[num][1][2];
        arr[num][1][2= temp;
    }
    // 반시계
    else if (dir == -1) {
        arr[num][0][2= arr[num][1][2];
        arr[num][1][2= arr[num][2][2];
        arr[num][2][2= arr[num][2][1];
        arr[num][2][1= arr[num][2][0];
        arr[num][2][0= arr[num][1][0];
        arr[num][1][0= arr[num][0][0];
        arr[num][0][0= arr[num][0][1];
        arr[num][0][1= temp;
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    rep(i, 4) {
        char c;
        cin >> c;
        arr[i][0][1= c - '0';
        cin >> c;
        arr[i][0][2= c - '0';
        cin >> c;
        arr[i][1][2= c - '0';
        cin >> c;
        arr[i][2][2= c - '0';
        cin >> c;
        arr[i][2][1= c - '0';
        cin >> c;
        arr[i][2][0= c - '0';
        cin >> c;
        arr[i][1][0= c - '0';
        cin >> c;
        arr[i][0][0= c - '0';
    }
    cin >> k;
    while (k--) {
        memset(direction, 0sizeof(direction));
        cin >> n >> dir;
        direction[n] = dir;
        // 1. 왼쪽 확인
        for (int i = n; i > 1; i--) {
            if (arr[i][1][0== arr[i - 1][1][2]) {
                direction[i - 1= 0;
                break;
            }
            direction[i - 1= direction[i] * -1;
        }
 
        // 2. 오른쪽 확인
        for (int i = n; i < 4; i++) {
            if (arr[i][1][2== arr[i + 1][1][0]) {
                direction[i + 1= 0;
                break;
            }
            direction[i + 1= direction[i] * -1;
        }
        // 3. 회전시키기
        rep(i, 4)
            rotate(i, direction[i]);
    }
 
    rep(i, 4)
        ans += arr[i][0][1* (1 << (i-1));
    cout << ans;
}
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 15685 [복습 필수]  (0) 2021.02.23
백준 14503  (0) 2021.02.22
백준 14890  (0) 2021.02.21
백준 14499  (0) 2021.02.21
백준 14501  (0) 2021.02.15
반응형

www.acmicpc.net/problem/14890

 

14890번: 경사로

첫째 줄에 N (2 ≤ N ≤ 100)과 L (1 ≤ L ≤ N)이 주어진다. 둘째 줄부터 N개의 줄에 지도가 주어진다. 각 칸의 높이는 10보다 작거나 같은 자연수이다.

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
#include <iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, l, cnt = 0, arr[200][100];
bool flag;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> l;
    rep(i, n) {
        rep(j, n)
            cin >> arr[i][j];
    }
 
    // 세로를 가로로 채워넣기
    rep(i, n) {
        rep(j, n)
            arr[n + i][j] = arr[j][i];
    }
    rep(i, 2*n) {
        flag = true;
        int a = 0;                                        // 탐색을 위한 투포인터
        int b = 1;
        while (b < n) {
            if (abs(arr[i][a] - arr[i][b]) > 1) {        // 1. 차이가 1 이상 -> 즉시 종료
                flag = false;
                break;
            }
 
            if (arr[i][a] == arr[i][b]) {                // 2. 높이가 같음 -> 포인터 이동
                a++;
                b++;
            }
 
            else if (arr[i][a] > arr[i][b]) {            // 높은 곳 -> 낮은 곳 (3 -> 2) : b를 l만큼 오른쪽으로 이동
                int j;
                for (j = b; j < b + l; j++) {            // 1. b를 오른쪽으로 l만큼 이동시킬 수 있는지 확인 
                    if (j == n)                            //   1) 이동 중에 배열 끝에 도착 -> 불가
                        break;                            
                    if (arr[i][j] != arr[i][a] - 1)        //   2) 이동 중에 높이가 달라짐  -> 불가
                        break;
                }
                if (j != b + l) {                        // 2. b가 l만큼 이동했는지 확인 (경사로 놓을 공간 확보했는 지)
                    flag = false;                        //   1) 이동 못함: 현재 행 탐색 종료. 다음 행 탐색
                    break;
                }
 
                a = b + l - 1;                            //   2) 이동함: 포인터를 경사로 끝 지점으로 이동
 
                for (j = a + 1; j <= a + l; j++) {        // 3. (경사로 끝 지점 ~ 경사로 끝 지점 + l) 사이에 올라가는 구간이 있는지 검사
                    if (j == n)                                // 배열 끝에 도착 : break
                        break;                                
                    if (arr[i][j] > arr[i][a]) {            // 높이 올라감 : 불가
                        flag = false;                        
                        break;
                    }
                }
                if (!flag)                                    // 단순히 해당 구간에 올라가는 구간이 있는지만 검사하는 것이므로
                    break;                                    // 올라가는 구간이 나오기 전에 배열 끝에 도달해서 종료했다면 (j = a + l = n)
                                                            // break 안해도 됨
                b = a + 1;
            }
 
            else if (arr[i][a] < arr[i][b]) {            // 낮은 곳 -> 높은 곳 (2 -> 3) : a를 l만큼 왼쪽으로 이동
                int j;
                for (j = a; j > a - l; j--) {            // 1. a를 왼쪽으로 l만큼 이동시킬 수 있는지 확인
                    if (j < 0)                            //   1) 이동 중에 배열 끝에 도착 -> 불가
                        break;
                    if (arr[i][j] != arr[i][b] - 1)        //   2) 이동 중에 높이가 달라짐  -> 불가
                        break;
                }
                if (j != a - l) {                        // 2. a가 l만큼 이동했는지 확인 (경사로 놓을 공간 확보했는 지)
                    flag = false;                        //   1) 이동 못함: 현재 행 탐색 종료. 다음 행 탐색
                    break;                                
                }
                a = b;                                    //   2) 이동함: 포인터 이동 후 계속 탐색
                b = a + 1;
            }
        }
        if (flag) cnt++;
    }
    cout << cnt;
}
 
cs
반응형

'백준 > 삼성기출' 카테고리의 다른 글

백준 14503  (0) 2021.02.22
백준 14891  (0) 2021.02.22
백준 14499  (0) 2021.02.21
백준 14501  (0) 2021.02.15
백준 14500  (0) 2021.02.13

+ Recent posts