문제 풀이

 

 bool 타입의 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
#include <iostream>
using namespace std;
int main() {
 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    int n, m;
    int ans = 0;
    bool com[201][201];
 
    cin >> n >> m;
 
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            com[i][j] = false;
        }
    }
 
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        com[x][y] = true;
        com[y][x] = true;
    }
 
    for (int i = 1; i <= n - 2; i++) {
        for (int j = i + 1; j <= n - 1; j++) {
            if (com[i][j]) continue;
            for (int k = j + 1; k <= n; k++) {
                if (com[i][k] || com[j][k]) continue;
                ans++;
            }
        }
    }
    cout << ans << '\n';
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

문제 출처

 

https://www.acmicpc.net/problem/2422

 

2422번: 한윤정이 이탈리아에 가서 아이스크림을 사먹는데

문제 한윤정과 친구들은 이탈리아로 방학 여행을 갔다. 이탈리아는 덥다. 윤정이와 친구들은 아이스크림을 사먹기로 했다. 아이스크림 가게에는 N종류의 아이스크림이 있다. 모든 아이스크림은 1부터 N까지 번호가 매겨져있다. 어떤 종류의 아이스크림을 함께먹으면, 맛이 아주 형편없어진다. 따라서 윤정이는 이러한 경우를 피하면서 아이스크림을 3가지 선택하려고 한다. 이때, 선택하는 방법이 몇 가지인지 구하려고 한다. 입력 첫째 줄에 정수 N과 M이 주어진다. N은

www.acmicpc.net

 

문제 풀이

 

 이 문제는 바이러스가 퍼지는 부분은 단순한 bfs 문제로 풀이가 가능했지만, 벽을 세우는 부분에서 코드가 길어지다 보니 헷갈리는 부분이 생겼던 문제입니다. 코드를 최대한 단순하게 짜는 것이 실수를 줄이는 방법이 될 것 같습니다..

 

 

코드

 

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
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <queue>
 
using namespace std;
 
int n, m;
int room[8][8];
int dx[] = { 00-11 };
int dy[] = { -1100 };
 
int bfs(queue<pair<intint>> q) {
    
    int space = 0;
    int d[8][8= { 0, };
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            d[i][j] = room[i][j];
 
    while (!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
            if (d[nx][ny] == 0)
            {
                d[nx][ny] = 2;
                q.push(make_pair(nx, ny));
            }
        }
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (d[i][j] == 0)
                space++;
        }
    }
    return space;
}
 
 
int main() {
 
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    int ans = 0;
    queue<pair<intint>> q;
    cin >> n >> m;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> room[i][j];
            if (room[i][j] == 2)
                q.push(make_pair(i, j));
        }
    }
    for (int i1 = 0; i1 < n; i1++)
    {
        for (int j1 = 0; j1 < m; j1++)
        {
            if (room[i1][j1] != 0continue;
            for (int i2 = 0; i2 < n; i2++)
            {
                for (int j2 = 0; j2 < m; j2++)
                {
                    if (room[i2][j2] != 0continue;
                    for (int i3 = 0; i3 < n; i3++)
                    {
                        for (int j3 = 0; j3 < m; j3++)
                        {
                            if (room[i3][j3] != 0continue;
                            if (i1 == i2 && j1 == j2)
                                continue;
                            if (i2 == i3 && j2 == j3)
                                continue;
                            if (i1 == i3 && j1 == j3)
                                continue;
 
                            room[i1][j1] = 1;
                            room[i2][j2] = 1;
                            room[i3][j3] = 1;
 
                            int x = bfs(q);
                            if (x > ans)
                                ans = x;
 
                            room[i1][j1] = 0;
                            room[i2][j2] = 0;
                            room[i3][j3] = 0;
                        }
                    }
                }
            }
        }
    }
    cout << ans << '\n';
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/14502

 

14502번: 연구소

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크기가 N×M인 직사각형으로 나타낼 수 있으며, 직사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다.  일부 칸은 바이러스가 존재하며, 이 바이러스는 상하좌우로 인접한 빈 칸으로 모두 퍼져나갈 수 있다.

www.acmicpc.net

 

문제 풀이

 

 여기서 중요한 부분은 'ㅗ, ㅓ, ㅏ, ㅜ' 모양의 테트로미노를 해결하는 것입니다. 따라서 이를 for문으로 예외 처리 해주었습니다. 이외의 테트로미노는 한 번에 처리가 가능하기 때문에 함수로 선언하여 문제를 풀었습니다.

 

 

코드

 

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
#include <iostream>
 
using namespace std;
 
int n, m;
int ans;
int board[500][500];
bool isVisit[500][500];
int dx[] = { 00-11 };
int dy[] = { -1100 };
 
void ttr(int x, int y, int sum, int cnt) {
 
    if (cnt == 4)
    {
        if (sum > ans)
            ans = sum;
        return;
    }
 
    if (isVisit[x][y] == truereturn;
    
    if (x < 0 || x >= n || y < 0 || y >= m) return;
 
    isVisit[x][y] = true;
    
    for (int i = 0; i < 4; i++)
        ttr(x + dx[i], y + dy[i], sum + board[x][y], cnt + 1);
    
    isVisit[x][y] = false;
}
 
int main() {
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    ans = 0;
    cin >> n >> m;
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> board[i][j];
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            ttr(i, j, 00);
        
    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < m - 2; j++)
        {
            int sum = board[i][j] + board[i][j + 1+ board[i][j + 2+ board[i - 1][j + 1];
            if (sum > ans)
                ans = sum;
        }
    }
    for (int i = 1; i < n - 1; i++)
    {
        for (int j = 0; j < m - 1; j++)
        {
            int sum = board[i][j] + board[i][j + 1+ board[i + 1][j + 1+ board[i - 1][j + 1];
            if (sum > ans)
                ans = sum;
        }
    }
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < m - 2; j++)
        {
            int sum = board[i][j] + board[i][j + 1+ board[i][j + 2+ board[i + 1][j + 1];
            if (sum > ans)
                ans = sum;
        }
    }
    for (int i = 0; i < n - 2; i++)
    {
        for (int j = 0; j < m - 1; j++)
        {
            int sum = board[i][j] + board[i + 1][j] + board[i + 2][j] + board[i + 1][j + 1];
            if (sum > ans)
                ans = sum;
        }
    }
    cout << ans;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/14500

 

14500번: 테트로미노

폴리오미노란 크기가 1×1인 정사각형을 여러 개 이어서 붙인 도형이며, 다음과 같은 조건을 만족해야 한다. 정사각형은 서로 겹치면 안된다. 도형은 모두 연결되어 있어야 한다. 정사각형의 꼭짓점끼리 연결되어 있어야 한다. 즉, 변과 꼭짓점이 맞닿아있으면 안된다. 정사각형 4개를 이어 붙인 폴리오미노는 테트로미노라고 하며, 다음과 같은 5가지가 있다. 아름이는 크기가 N×M인 종이 위에 테트로미노 하나를 놓으려고 한다. 종이는 1×1 크기의 칸으로 나누어져

www.acmicpc.net

 

문제 해설

 

 현재 높이의 2배의 삼각형이 다음에 만들어진다는 것에서 착안하여 문제를 풀었습니다.

 

 

 즉, 위의 그림과 같은 규칙으로 삼각형이 생성됩니다. 따라서 이를 코드로 구현하면 아래와 같습니다.

(+ null 문자열이 출력되지 않도록 유의합니다.)

 

 

코드

 

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
#include <iostream>
 
using namespace std;
 
char d[3072][6144];
 
void star(int n) {
 
    for (int i = n / 2; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            d[i][j] = d[i - n / 2][j];
    }
 
    for (int i = n / 2; i < n; i++)
    {
        for (int j = n; j < 2 * n; j++)
            d[i][j] = d[i][j - n];
    }
 
    for (int i = 0; i < n / 2; i++)
    {
        for (int j = (2 * n) - (n / 2- 1; j >= n / 2; j--)
        {
            d[i][j] = d[i][j - (n / 2)];
            d[i][j - (n / 2)] = ' ';
        }
    }
    return;
}
 
int main() {
 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    int n;
    cin >> n;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 2 * n; j++)
            d[i][j] = ' ';
    }
 
    //initial triangle
    d[0][2= '*';
    d[1][1= '*';
    d[1][3= '*';
    for (int i = 0; i < 5; i++)
        d[2][i] = '*';
 
    for (int i = 3; i <= n; i *= 2)
    {
        if (i == 3)
            continue;
        else
            star(i);
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 2 * n; j++)
            cout << d[i][j];
        cout << '\n';
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/2448

 

2448번: 별 찍기 - 11

첫째 줄에 N이 주어진다. N은 항상 3×2k 수이다. (3, 6, 12, 24, 48, ...) (k ≤ 10)

www.acmicpc.net

 

문제 풀이

 

 에라토스테네스의 체를 활용하여 소수를 구했습니다.

(https://ko.wikipedia.org/wiki/%EC%97%90%EB%9D%BC%ED%86%A0%EC%8A%A4%ED%85%8C%EB%84%A4%EC%8A%A4%EC%9D%98_%EC%B2%B4)

 

 

코드

 

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
#include <iostream>
#include <vector>
 
using namespace std;
 
int d[1001= { 0, };
 
int main() {
 
    int n;
    int m = 0;
    cin >> n;
 
    vector <int> v;    
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        if(x > m)
            m = x;
        v.push_back(x);
    }
    
    d[1= 1;
    for (int i = 2; i <= m; i++)
    {
        if (d[i] == 0)
        {
            for (int j = i; j*<= m; j++)
            {
                if (d[j*i] == 0)
                    d[j*i] = 1;
            }
        }
    }
 
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        if (d[v[i]] == 0)
            ans++;
    }
 
    cout << ans;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/1978

 

1978번: 소수 찾기

첫 줄에 수의 개수 N이 주어진다. N은 100이하이다. 다음으로 N개의 수가 주어지는데 수는 1,000 이하의 자연수이다.

www.acmicpc.net

 

문제 해설

 

 자료형을 연습하기 좋은 문제라고 생각이 들어 가져오게 되었습니다. 유의해야 할 점은 총 두 가지로 반올림과 최빈값 구하기입니다.

 반올림 같은 경우, 양수와 음수일 때를 고려하여 양수일 때는 0.5를 더해준 값을 int형으로 변환시켜 저장해 주었고 음수일 때는 0.5를 빼준 값을 int형으로 변환시켜 저장하였습니다. 최빈값의 경우, 우선 가장 빈도수가 높을 때 빈도가 몇인지 구하여 같은 빈도가 있는지 확인해 주었습니다.

 

 

코드

 

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 <cstdio>
#include <algorithm>
#include <vector>
 
using namespace std;
 
int d[500000];
 
int main() {
 
    int n;
    int bi[8001= { 0, };
    int sum = 0, bm = 0, cm = 0, dm = 0;
 
    scanf("%d"&n);
 
    for (int i = 0; i < n; i++)
    {
        scanf("%d"&d[i]);
        sum += d[i];
 
        int x = d[i] + 4000;
        bi[x]++;
    }
 
    sort(d, d + n);
 
    int maximum = 0;
    for (int i = 0; i <= d[n - 1+ 4000; i++)
    {
        if (bi[i] > maximum)
            maximum = bi[i];
    }
 
    vector <int> v;
    for (int i = 0; i <= d[n - 1+ 4000; i++)
    {
        if (bi[i] == maximum)
            v.push_back(i);
    }
 
    float am = float(sum) / float(n);
 
    if (am >= 0)
        am = int(am + 0.5);
    else
        am = int(am - 0.5);
 
    bm = d[n / 2];
 
    if (v.size() > 1)
        cm = v[1- 4000;
    else
        cm = v[0- 4000;
 
    dm = d[n - 1- d[0];
 
    printf("%d\n"int(am));
    printf("%d\n", bm);
    printf("%d\n", cm);
    printf("%d\n", dm);
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/2108

 

2108번: 통계학

첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.

www.acmicpc.net

 

문제 풀이

 

 게임의 규칙을 찾아야 풀이가 가능한 문제입니다. 게임의 규칙은 다음과 같습니다.

 

1. 같은 곳을 두 번 뒤집으면 결과가 같으므로 이러한 경우는 고려하지 않아도 됩니다.

2. N은 짝수이므로,

2-1) (x, y)의 돌 (=노란색 별) 을 뒤집으면 자기 자신과 가로줄, 세로줄의 돌 색이 바뀌게 되므로 노란색 별 기준 주변의 검은색 돌 갯수는 홀수개가 됩니다.

2-2) 초록색 별 기준 바뀌는 돌의 색은 자기 자신과 자기 자신이 속한 한 줄이 되므로 검은 돌의 갯수는 짝수개가 됩니다.

2-3) 자기 자신의 색이 바뀌지 않는 빨간색 별 기준 바뀌는 돌의 색은 가로 줄 한 개, 세로 줄 한 개이므로 검은 돌의 갯수는 홀수개가 됩니다.

(+ 흰 색 돌을 기준으로 보는 경우 뒤집은 돌의 주변 흰색 돌만 짝수개가 됩니다.)

3. 즉, 가로줄의 검은 돌 갯수와 세로 줄의 검은 돌 갯수를 합한 것이 홀수라면, 뒤집은 돌이 됩니다. (중복 제외)

(+ cin, cout으로 받으면 시간 초과납니다ㅠㅠ)

 

 

코드

 

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
#include <iostream>
 
using namespace std;
 
int main() {
 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int t;
    cin >> t;
 
    for (int tc = 1; tc <= t; tc++)
    {
        int ans = 0;
        char board[1000][1000];
        int h[1000= { 0, };
        int v[1000= { 0, };
 
        int n;
        cin >> n;
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cin >> board[i][j];
                if (board[i][j] == 'B')
                {
                    h[i]++;
                    v[j]++;
                }
            }
        }
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int x = h[i] + v[j];
                if (board[i][j] == 'B')
                    x -= 1;
 
                if (x % 2 != 0)
                    ans++;
            }
        }
        cout << '#' << tc << ' ' << ans << '\n';
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWefzFeq5P8DFAUh&categoryId=AWefzFeq5P8DFAUh&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

문제 풀이

 

 bfs 알고리즘을 활용하여 방문하지 않은 길을 따라 도착점이 나올 때까지 탐색하였습니다.

 

 

코드

 

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 <cstdio>
#include <queue>
 
using namespace std;
 
int ans;
int map[16][16];
bool isVisit[16][16];
int dx[] = { 00-11 };
int dy[] = { -1100 };
 
void bfs(int a, int b)
{
    queue <pair<intint>> q;
    q.push(make_pair(a, b));
    isVisit[a][b] = true;
 
    while (!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && ny >= 0 && nx < 16 && ny < 16)
            {
                //도착점이라면 ans = 1로 하고, 리턴
                if (map[nx][ny] == 3)
                {
                    ans = 1;
                    return;
                }
                //방문한 적 없는 길인 경우
                else if (map[nx][ny] == 0 && isVisit[nx][ny] == false)
                {
                    isVisit[nx][ny] = true;
                    q.push(make_pair(nx, ny));
                }
            }
        }
    }
}
 
int main() {
 
    while(1)
    {
        int tc;
        scanf("%d"&tc);
        ans = 0;
        int x = 0, y = 0//starting point;
        for (int i = 0; i < 16; i++)
        {
            for (int j = 0; j < 16; j++)
            {
                scanf("%1d"&map[i][j]);
 
                if (map[i][j] == 1)
                    isVisit[i][j] = true;
 
                else if (map[i][j] == 2)
                {
                    x = i;
                    y = j;
                    isVisit[i][j] = false;
                }
 
                else
                    isVisit[i][j] = false;
            }
        }
 
        bfs(x, y);
 
        printf("#%d %d\n", tc, ans);
        
        if (tc == 10)
            break;
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14vXUqAGMCFAYD&categoryId=AV14vXUqAGMCFAYD&categoryType=CODE&&&

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

+ Recent posts