반응형

www.acmicpc.net/problem/1722

 

1722번: 순열의 순서

첫째 줄에 N(1≤N≤20)이 주어진다. 둘째 줄의 첫 번째 수는 소문제 번호이다. 1인 경우 k(1≤k≤N!)를 입력받고, 2인 경우 임의의 순열을 나타내는 N개의 수를 입력받는다. N개의 수에는 1부터 N까지

www.acmicpc.net

 

1번 문제

2번 문제

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
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
ll arr[21], used[21];
ll n, m;
ll fact[21];
 
void init_fact() {
    fact[0= 1;
    for (ll i = 1; i <= n; i++) {
        fact[i] = i * fact[i - 1];
    }
}
void func1() {
    ll x;
    cin >> x;    
    for (ll i = 1; i <= n; i++) {            // 모든 자리 탐색
        ll cnt = 1;                            // 중간에 사용한 것 거르기 위해서 idx, cnt 따로 사용해야 함
        for (ll idx = 1; idx <= n; idx++) {    // 모든 숫자 탐색
            if (used[idx])                    // 사용했으면 다음 인덱스 탐색
                continue;
 
            if (cnt * fact[n - i] >= x) {    // 개수 충분한 지 확인
                cout << idx << ' ';
                used[idx] = true;
                x -= (cnt - 1* fact[n - i];
                break;
            }
            cnt++;
        }
    }
}
void func2() {
    for (ll i = 1; i <= n; i++)
        cin >> arr[i];
 
    ll ans = 1;
    ll idx = 1;
 
    while (idx <= n) {
        // 현재 idx의 숫자보다 앞에 있는 숫자의 개수 찾기
        ll cnt = arr[idx] - 1;
        for (ll i = 1; i < arr[idx]; i++) {
            // 앞에 있는 숫자가 이미 사용됐으면 cnt--
            if (used[i])
                cnt--;
        }
        // (내 앞에 올 수 있는 숫자 개수) * (내 뒤에 올 수 있는 경우의 수)
        ans += (cnt * fact[n - idx]);
        used[arr[idx]] = 1;
        idx++;
    }
    cout << ans;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> n >> m;
    init_fact();
 
    if (m == 1)
        func1();
    else 
        func2();
}
cs
반응형
반응형

www.acmicpc.net/problem/14502

 

14502번: 연구소

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크

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
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define pii pair<intint>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, m;
int arr[9][9];
int dx[4= { 10-10 };
int dy[4= { 010-1 };
int temp[9][9];
int ans = -1;
int chk_zero() {
    int ret = 0;
    rep(i, n) {
        rep(j, m) {
            if (temp[i][j] == 0)
                ret++;
        }
    }
    return ret;
}
bool visited[9][9];
void bfs() {
    queue<pii> q;
    // 1) 원본 배열 복사 (원본 배열로 하면 다음 bfs 실행 못함)
    rep(i, n) {        
        rep(j, m)    
            temp[i][j] = arr[i][j];
    }
    
    // 2) 모든 칸 탐색 (2 이고 방문 안했으면 q에 추가)
    rep(i, n) {        
        rep(j, m) {    
            if (temp[i][j] == 2 && !visited[i][j]) {
                q.push({ i,j });
                visited[i][j] = 1;
            }
        }
    }
 
    // 3) q에서 꺼내서 처리
    while (!q.empty()) {
        pii now = q.front();
        q.pop();
        int x = now.first;
        int y = now.second;
        rep(i, 4) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
                if (temp[nx][ny] == 0 && !visited[nx][ny]) {
                    temp[nx][ny] = 2;
                    visited[nx][ny] = 1;
                    q.push({ nx, ny });
                }
            }
        }
    }
}
 
void dfs(int x, int y, int cnt) {
    if (cnt == 3) {
        memset(visited, 0sizeof(visited));
        bfs();                        // 2. bfs로 2 퍼뜨리기
        ans = max(ans, chk_zero());    // 3. 0 개수 확인
        return;
    }
    
    for(int i = x; i<n; i++){        // 1. 벽 세우기(백트래킹)
        for(int j = 0; j<m; j++) {
            if (arr[i][j] == 0) {
                arr[i][j] = 1;    
                dfs(i, j, cnt + 1);
                arr[i][j] = 0;
            }
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    rep(i, n) {
        rep(j, m)
            cin >> arr[i][j];
    }
    dfs(000);
    cout << ans;
}
cs
반응형

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

백준 14500  (0) 2021.02.13
백준 15686  (0) 2021.02.05
백준 15684  (0) 2021.02.03
백준 14889  (0) 2021.01.29
백준 14888  (0) 2021.01.28
반응형

www.acmicpc.net/problem/15684

 

15684번: 사다리 조작

사다리 게임은 N개의 세로선과 M개의 가로선으로 이루어져 있다. 인접한 세로선 사이에는 가로선을 놓을 수 있는데, 각각의 세로선마다 가로선을 놓을 수 있는 위치의 개수는 H이고, 모든 세로선

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
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, h;
int arr[31][11];
int min_cnt = 987654321;
bool chk() {
    // 마지막 가로줄에 도착했을 때 출발번호와 도착번호 같은지 확인
    for (int i = 1; i <= n; i++) {        // 모든 세로줄 검사
        int current_col = i;            // 현재 세로줄 위치 저장, 가로줄 만나면 변함
        for (int j = 1; j <= h; j++) {    // 모든 가로줄 검사
            if (arr[j][current_col]==1)    // 현재 위치에 사다리가 있으면 그거 타고 오른쪽으로 이동
                current_col++;
            else if (arr[j][current_col - 1]==1)    // 현재 위치의 왼쪽에 사다리 있으면 그거 타고 왼쪽으로 이동
                current_col--;
        }
 
        // 출발번호 도착번호 다르면
        if (current_col != i)
            return false;
    }
    return true;
}
void dfs(int row, int col, int cnt) {
    // cnt가 3보다 크거나 현재 최솟값보다 크거나 같으면 return (더 이상 볼 필요 없음)
    if (cnt > 3 || cnt >= min_cnt) return;
    if (chk()) {
        min_cnt = min(cnt, min_cnt);
        return;
    }
    
    for(int i = row; i<=h; i++) {    // 모든 가로줄 검사
        col = 1;                    // 다음 가로줄 검사할 때 첫 번째 세로줄부터 검사
        for(int j=col; j<n; j++) {    // 모든 세로줄 검사 (마지막 세로줄은 오른쪽 검사 안함)
            if (arr[i][j]==0) {        // 현재 위치에 사다리 없으면
                if (arr[i][j - 1== 0 && arr[i][j + 1== 0) {   // 현재 위치 양 옆에 가로줄이 없어야 함
                    arr[i][j] = 1;
                    dfs(i, j, cnt + 1);
                    arr[i][j] = 0;
                }
            }
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> h;
    for (int i = 1;i <= m;i++) {
        int a, b;
        cin >> a >> b;
        arr[a][b] = 1;
    }
    dfs(110);
 
    cout << (min_cnt == 987654321 ? -1 : min_cnt);
}
cs
반응형

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

백준 14500  (0) 2021.02.13
백준 15686  (0) 2021.02.05
백준 14502  (0) 2021.02.03
백준 14889  (0) 2021.01.29
백준 14888  (0) 2021.01.28
반응형

www.acmicpc.net/problem/2583

 

2583번: 영역 구하기

첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 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
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <queue>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int m, n, k, cnt;
int dx[4= { 10-10 };
int dy[4= { 010-1 };
bool visited[101][101];
priority_queue<intvector<int>, greater<int> > q;
void dfs(int x, int y) {
    cnt++;
    visited[x][y] = 1;
    rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
            if (!visited[nx][ny]) 
                dfs(nx, ny);
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> m >> n >> k;
    rep(i,k){
        int lx, ly, rx, ry;
        cin >> lx >> ly >> rx >> ry;
        // 입력 받은 영역 채우기
        for (int i = lx; i < rx; i++) {
            for (int j = ly; j < ry; j++
                visited[i][j] = 1;
            
        }
    }
 
    int ans = 0;
    rep(i, n) {
        rep(j, m) {
            if (!visited[i][j]) {
                cnt = 0;
                dfs(i, j);
                q.push(cnt);
                ans++;
            }
        }
    }
    cout << ans << '\n';
    while (!q.empty()) {
        cout << q.top() << ' ';
        q.pop();
    }
}
cs
반응형

'백준 > DFS, BFS' 카테고리의 다른 글

백준 1707  (0) 2021.02.19
백준 13023  (0) 2021.02.19
백준 2468  (0) 2021.02.03
백준 1697  (0) 2021.02.03
백준 1012  (0) 2021.02.03
반응형

www.acmicpc.net/problem/2468

 

2468번: 안전 영역

재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는

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
#include <iostream>
#include <cstring>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, h, arr[101][101];
bool visited[101][101];
int dx[4= { 10-10 };
int dy[4= { 010-1 };
void dfs(int x, int y) {
    visited[x][y] = 1;
    rep(i, 4) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= 0 && nx < n && ny >= 0 && ny < n) {
            if (arr[nx][ny] > h && !visited[nx][ny])
                dfs(nx, ny);
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    int min_height = 101, max_height = -1;
    rep(i, n) {
        rep(j, n) {
            cin >> arr[i][j];
            if (arr[i][j] < min_height)
                min_height = arr[i][j];
            if (arr[i][j] > max_height)
                max_height = arr[i][j];
        }
    }
    int ans = 1;
    // 최소 높이 ~ 최대 높이 한 번씩 다 해봄
    for (int k = min_height; k <= max_height; k++) {
        memset(visited, 0sizeof(visited));
        int cnt = 0;
        h = k;
        rep(i, n) {
            rep(j, n) {
                // 도시가 강수량보다 높으면
                if (arr[i][j] > k && !visited[i][j]) {
                    dfs(i, j);
                    cnt++;
                }
            }
        }
        ans = max(ans, cnt);
    }
    cout << ans;
}
cs
반응형

'백준 > DFS, BFS' 카테고리의 다른 글

백준 1707  (0) 2021.02.19
백준 13023  (0) 2021.02.19
백준 2583  (0) 2021.02.03
백준 1697  (0) 2021.02.03
백준 1012  (0) 2021.02.03
반응형

www.acmicpc.net/problem/1697

 

1697번: 숨바꼭질

수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일

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
#include <iostream>
#include <queue>
#include <cstring>
#define rep(i,n) for(int i=0;i<n;i++)
#define pii pair<intint>
using namespace std;
int t, m, n, k, cnt;
int arr[51][51];
bool visited[51][51];
int dx[4= {10-10};
int dy[4= {010-1};
void bfs(int x, int y) {
    cnt++;
    queue<pii> q;
    q.push({ x, y });
    visited[x][y] = true;
    while (!q.empty()) {
        pii now = q.front();
        q.pop();
        int x = now.first;
        int y = now.second;
        rep(i, 4) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
                if (arr[nx][ny]==1 && !visited[nx][ny]) {
                    visited[nx][ny] = true;
                    q.push({ nx,ny });
                }
            }
        }
    }
 
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> t;
    while (t--) {
        cnt = 0;
        memset(arr, 0sizeof(arr));
        memset(visited, 0sizeof(visited));
        cin >> m >> n >> k;
        rep(i, k) {
            int a, b;
            cin >> a >> b;
            arr[b][a] = 1;
        }
 
        rep(i,n){
            rep(j, m) {
                if (arr[i][j] == 1 && !visited[i][j])
                    bfs(i, j);
            }
        }
        cout << cnt << '\n';
    }
}
 
 
 
cs
반응형

'백준 > DFS, BFS' 카테고리의 다른 글

백준 1707  (0) 2021.02.19
백준 13023  (0) 2021.02.19
백준 2583  (0) 2021.02.03
백준 2468  (0) 2021.02.03
백준 1012  (0) 2021.02.03
반응형

www.acmicpc.net/problem/1012

 

1012번: 유기농 배추

차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 

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
#include <iostream>
#include <queue>
#include <cstring>
#define rep(i,n) for(int i=0;i<n;i++)
#define pii pair<intint>
using namespace std;
int t, m, n, k, cnt;
int arr[51][51];
bool visited[51][51];
int dx[4= {10-10};
int dy[4= {010-1};
void bfs(int x, int y) {
// bfs 실행할 때마다 cnt++
    cnt++;
    queue<pii> q;
    q.push({ x, y });
    visited[x][y] = true;
    while (!q.empty()) {
        pii now = q.front();
        q.pop();
        int x = now.first;
        int y = now.second;
        rep(i, 4) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
                if (arr[nx][ny]==1 && !visited[nx][ny]) {
                    visited[nx][ny] = true;
                    q.push({ nx,ny });
                }
            }
        }
    }
 
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> t;
    while (t--) {
        cnt = 0;
        memset(arr, 0sizeof(arr));
        memset(visited, 0sizeof(visited));
        cin >> m >> n >> k;
        rep(i, k) {
            int a, b;
            cin >> a >> b;
            arr[b][a] = 1;
        }
 
        rep(i,n){
            rep(j, m) {
                // 현재 1이고 방문 안했으면 bfs 
                if (arr[i][j] == 1 && !visited[i][j])
                    bfs(i, j);
            }
        }
        cout << cnt << '\n';
    }
}
 
 
 
cs
반응형

'백준 > DFS, BFS' 카테고리의 다른 글

백준 1707  (0) 2021.02.19
백준 13023  (0) 2021.02.19
백준 2583  (0) 2021.02.03
백준 2468  (0) 2021.02.03
백준 1697  (0) 2021.02.03
반응형

www.acmicpc.net/problem/10971

 

10971번: 외판원 순회 2

첫째 줄에 도시의 수 N이 주어진다. (2 ≤ N ≤ 10) 다음 N개의 줄에는 비용 행렬이 주어진다. 각 행렬의 성분은 1,000,000 이하의 양의 정수이며, 갈 수 없는 경우는 0이 주어진다. W[i][j]는 도시 i에서 j

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
#include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, ans, arr[11][11];
int idx_arr[11];    // 탐색 순서 저장
 
int calc() {
    int ret = 0;
    for (int i = 0;i < n - 1;i++){
        // 0이면 못감
        if (arr[idx_arr[i]][idx_arr[i + 1]] == 0)
            return 987654321;
 
        // 다음 도시로 이동
        ret += arr[idx_arr[i]][idx_arr[i + 1]];
    }
    // 0이면 못감
    if (arr[idx_arr[n - 1]][idx_arr[0]] == 0)
        return 987654321;
 
    // 마지막 도시 -> 처음 도시
    ret += arr[idx_arr[n - 1]][idx_arr[0]];
    return ret;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    rep(i, n) {
        idx_arr[i] = i;
        rep(j, n)
            cin >> arr[i][j];
    }
    ans = calc();
    while (next_permutation(idx_arr, idx_arr + n))
        ans = min(ans, calc());
    cout << ans;
}
cs
반응형

'백준 > 백트래킹' 카테고리의 다른 글

백준 15661  (0) 2021.02.15
백준 18290  (0) 2021.02.15
백준 10819  (0) 2021.02.03
백준 1987  (0) 2021.02.03
백준 1759  (0) 2021.02.03

+ Recent posts