BOJ 4963번 섬의개수 문제

BFS로 붙어있는거 체크를 하는데, 2667번 단지번호붙이기와 거의 같은데, 대각선 방향이 추가된다.

2667.cpp

#include <bits/stdc++.h>
using namespace std;
int map_[26][26];
int countCheck[26][26];
int townNum;
int n;
int dirY[4] = {+1, -1, 0, 0};
int dirX[4] = {0, 0, -1, +1};

void BFS(int y, int x, int townNum) {
    queue<pair<int, int> > q;
    q.push({y, x});
    countCheck[y][x] = townNum;
    while (!q.empty()) {
        y = q.front().first;
        x = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++) {
            int nextY = y + dirY[i];
            int nextX = x + dirX[i];
            if (nextY >= 0 && nextY < n && nextX >= 0 && nextX < n) {
                if (map_[nextY][nextX] == 1 && countCheck[nextY][nextX] == 0) {
                    q.push({nextY, nextX});
                    countCheck[nextY][nextX] = townNum;
                }
            }
        }
    }
}
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%1d", &map_[i][j]);
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (map_[i][j] == 1 && countCheck[i][j] == 0) {
                townNum++;
                BFS(i, j, townNum);
            }
        }
    }
    vector<vector<int> > town(25 * 25);

    for (int y = 0; y < n; y++) {
        for (int x = 0; x < n; x++) {
            int townIndex = countCheck[y][x];
            if (townIndex == 0) continue;
            town[townIndex].push_back(1);
        }
    }
    sort(town.begin(), town.end());

    cout << townNum << '\n';
    for (int i = 0; i < town.size(); i++) {
        if (town[i].size() == 0) continue;
        cout << town[i].size() << '\n';
    }
}

BOJ 16397번 탈출 문제

2018 홍익대학교 컴퓨터공학과 코딩대회 D번 문제이다.

또 문제를 잘 못 봤다. 제일 높은 자리수 1을 빼는게 각 자리를 비교해서 가장 큰 수를 뺴는 걸로 생각했다.

왜 더 어렵게 생각해서 구현도 복잡하고 시간도 더 낭비했는지..

이런 것도 다 실력이니 이런 실수를 줄이는 노력을 해야겠다.

문제는 BFS로 풀면 된다.

16397.cpp

#include <bits/stdc++.h>
using namespace std;
bool visited[100000];
int po(int n) {
    int nn = 1;
    for (int i = 0; i < n; i++) {
        nn *= 10;
    }
    return nn;
}
int B(int i) {
    if (i == 0) {
        return i;
    }
    int originI = i * 2;
    i = originI;
    int t = 0;
    while (i > 0) {
        t++;
        i /= 10;
    }
    originI -= po(t - 1);
    return originI;
}

int main() {
    int n, t, g;
    cin >> n >> t >> g;
    queue<pair<int, int> > q;
    visited[n] = true;
    q.push({n, 0});

    while (!q.empty()) {
        int now = q.front().first;
        int cnt = q.front().second;
        q.pop();

        if (cnt > t) {
            cout << "ANG";
            return 0;
        }
        if (now == g) {
            cout << cnt;
            return 0;
        }

        if ((now + 1 <= 99999) && !visited[now + 1]) {
            q.push({now + 1, cnt + 1});
            visited[now + 1] = true;
        }
        int next = B(now);
        if ((now * 2 <= 99999) && !visited[next]) {
            q.push({next, cnt + 1});
            visited[next] = true;
        }
    }
    cout << "ANG";
}

BOJ 16396번 선 그리기 문제

2018 홍익대학교 컴퓨터공학과 코딩대회 C번 문제이다.

분명 간단하게 풀 수 있는데, 선분의 길이가 아니라 거치는 정점의 개수로 생각해서 2번이나 틀렸다.

왜 그렇게 생각했는지 모르겠는데, 1~9가 들어오면 9에도 true값을 했다.

16396.cpp

#include <bits/stdc++.h>
using namespace std;
bool l[100001];
int main() {
    int n;
    cin >> n;
    while (n--) {
        int a, b;
        cin >> a >> b;
        for (int i = a; i < b; i++) {
            l[i] = true;
        }
    }
    int c = 0;
    for (int i = 1; i <= 100000; i++) {
        if (l[i]) {
            c++;
        }
    }
    cout << c;
}

BOJ 16395번 파스칼의 삼각형 문제

2018 홍익대학교 컴퓨터공학과 코딩대회 B번 문제이다.

간단한 DP 문제였다.

DP로 풀면 되는 걸 알고 있어서 어렵지 않게 풀 수 있었다.

16395.cpp

#include <bits/stdc++.h>
using namespace std;
int d[31][31];
int main() {
    for (int i = 0; i < 31; i++) {
        d[i][0] = 1;
    }
    for (int i = 1; i < 31; i++) {
        for (int j = 1; j <= i; j++) {
            d[i][j] = d[i - 1][j - 1] + d[i - 1][j];
        }
    }
    int a, b;
    cin >> a >> b;
    cout << d[a - 1][b - 1] << '\n';
}

BOJ 10101번 삼각형 외우기 문제

세 각이 같을 때 체크, 세 각의 합이 180일때 체크, 180을 넘을 떄 체크를 한다.

180일 때 세각이 모두 다른 경우와 그렇지 않은 경우를 체크한다.

10101.cpp

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 60 && a == b && b == c) {
        cout << "Equilateral";
    } else if (a + b + c == 180) {
        if (a != b && b != c && c != a) {
            cout << "Scalene";
        } else {
            cout << "Isosceles";
        }
    } else {
        cout << "Error";
    }
}

BOJ 2443번 별찍기 - 6 문제

백준에 별찍기 쉬운거는 다 푼지 알았는데 안 푼 문제가 있어서 오랜만에 별찍기 문제를 풀어봤다.

내가 기억하기로는 별 뒤에 있는 공백도 출력해야하는 걸로 알고 있었는데 출력하니까 출력 오류가 났다.

그 부분만 주의하면 될 것 같다.

2443.cpp

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            cout << " ";
        }
        for (int j = 2 * n - 2 * i - 1; j > 0; j--) {
            cout << "*";
        }
        cout << '\n';
    }
}

BOJ 2251번 물통 문제

BFS로 풀었다.

C에서 B, A로 담는 경우, B에서 A, C로 담는 경우, A에서 B, C로 주는 경우를 고려하면 된다.

물통이 용량을 모두 수용할 수 있는지 여부를 체크해줘야 한다.

항상 이런거 짤때 실수를 해서 고치느라 애먹었다.

답을 출력할 때 중복 값이 나와서 set에 담아서 출력했다.

2251.cpp

#include <bits/stdc++.h>
using namespace std;
bool visited[201][201][201];
struct Water {
    int a;
    int b;
    int c;
};
int main() {
    int A, B, C;
    cin >> A >> B >> C;
    set<int> ans;
    queue<Water> q;
    q.push({0, 0, C});

    while (!q.empty()) {
        int nowA = q.front().a;
        int nowB = q.front().b;
        int nowC = q.front().c;
        q.pop();
        if (visited[nowA][nowB][nowC])
            continue;
        visited[nowA][nowB][nowC] = true;
        // A가 0일때의 C의 용량을 담는다.
        if (nowA == 0) {
            //ans.push_back(nowC);
            ans.insert(nowC);
        }
        // case 1) C->B
        if (nowB + nowC > B) {
            q.push({nowA, B, nowB + nowC - B});
        } else {
            q.push({nowA, nowB + nowC, 0});
        }
        // case 2) C->A
        if (nowA + nowC > A) {
            q.push({A, nowB, nowA + nowC - A});
        } else {
            q.push({nowA + nowC, nowB, 0});
        }
        // case 3) B->A
        if (nowA + nowB > A) {
            q.push({A, nowA + nowB - A, nowC});
        } else {
            q.push({nowA + nowB, 0, nowC});
        }
        // case 4) B->C
        if (nowB + nowC > C) {
            q.push({nowA, nowB + nowC - C, C});
        } else {
            q.push({nowA, 0, nowB + nowC});
        }
        // case 5) A->B
        if (nowA + nowB > B) {
            q.push({nowA + nowB - B, B, nowC});
        } else {
            q.push({0, nowA + nowB, nowC});
        }
        // case 6) A->C
        if (nowA + nowC > C) {
            q.push({nowA + nowC - C, nowB, C});
        } else {
            q.push({0, nowB, nowA + nowC});
        }
    }
    set<int>::iterator iter;
    for (iter = ans.begin(); iter != ans.end(); iter++) {
        cout << *iter << " ";
    }
}

+ Recent posts