5-10

#include <iostream>
using namespace std;


int n, m;
int map[1001][1001];
bool visit[1001][1001];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

void dfs(int i, int j) {
    visit[i][j] = true;

    for (int z = 0; z < 4; z++) {
        int nx = i + dx[z];
        int ny = j + dy[z];

        if (nx < 0 || nx >= n || ny < 0 || ny >= m)
            continue;
        if (map[nx][ny] == 0 && !visit[nx][ny])
            dfs(nx, ny);
    }
}

int main() {
    int result = 0;
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char c;
            cin >> c;
            map[i][j] = c - '0';
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (map[i][j] == 0 && !visit[i][j]) {
                dfs(i, j);
                result++;
            }
        }
    }

    cout << result;

    return 0;
}

 

5-11

#include <iostream>
#include <queue>
using namespace std;

int n, m;
int map[1001][1001];
bool visit[1001][1001];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};


int main() {
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char c;
            cin >> c;
            map[i][j] = c - '0';
        }
    }
    queue<pair<int, int>> q;
    visit[0][0] = true;
    q.push({0, 0});

    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 >= n || ny >= m)
                continue;
            if (map[nx][ny] == 1 && !visit[nx][ny]) {
                visit[nx][ny] = true;
                map[nx][ny] = map[x][y] + 1;
                q.push({nx, ny});
            }
        }
    }
    cout << map[n-1][m-1];


    return 0;
}

 

#include <iostream>
#include <queue>
using namespace std;

bool cmp(pair<string,int> s, pair<string,int>s1){
    return s.second<s1.second;
}
int main() {
    vector<pair<string,int>> v ={{"이순신",77}, {"홍길동",95}};
    sort(v.begin(), v.end(), cmp);
    cout << v[0].first << endl;
    cout << v[1].first;
    return 0;
}

'⚖️Algorithm > 📝 이취코' 카테고리의 다른 글

[이취코] 10. 그래프 이론 - C++  (0) 2022.10.22
[이취코] 9. 최단경로 - C++  (0) 2022.10.22
[이취코] 8. DP - C++  (0) 2022.10.21
[이취코] 2. 구현 - C++  (0) 2022.10.04
[이취코] 1. 그리디(탐욕법) - C++  (0) 2022.10.04

+ Recent posts