diff --git a/09/kang/1389.cpp b/09/kang/1389.cpp new file mode 100644 index 0000000..2a39c77 --- /dev/null +++ b/09/kang/1389.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +using namespace std; + +int N, M; +vector adj[103]; +int dist[103]; +vector> kb; + +int bfs(int s) { + queue q; + int x, sum = 0; + + fill(dist, dist + N + 1, 1e9); + q.push(s); + dist[s] = 1; + while(!q.empty()) { + x = q.front(); + q.pop(); + for(int nxt : adj[x]) { + if(dist[nxt] > dist[x] + 1) { + dist[nxt] = dist[x] + 1; + q.push(nxt); + } + } + } + + for(int i = 1; i <= N; i++) { + sum += dist[i]; + } + return sum; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int a, b; + + cin >> N >> M; + for(int i = 0; i < M; i++) { + cin >> a >> b; + adj[a].push_back(b); + adj[b].push_back(a); + } + + + for(int i = 1; i <= N; i++) { + kb.push_back({bfs(i), i}); + } + + sort(kb.begin(), kb.end()); + + cout << kb[0].second << "\n"; + + return 0; +} diff --git a/09/kang/16928.cpp b/09/kang/16928.cpp new file mode 100644 index 0000000..8fe965d --- /dev/null +++ b/09/kang/16928.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; + +int N, M; +int ladder[103]; +int snake[103]; +bool visit[103]; + +int bfs() { + queue> q; //좌표, 주사위 수 + int x, cnt, nx; + + q.push({1, 0}); + visit[1] = true; + while(!q.empty()) { + x = q.front().first; + cnt = q.front().second; + q.pop(); + if(x == 100) { + return cnt; + } + for(int i = 1; i <= 6; i++) { + nx = x + i; + if(nx > 100) + break; + if(visit[nx]) + continue; + if(ladder[nx] != 0) //사다리를 타고 이동한 좌표 + nx = ladder[nx]; + if(snake[nx] != 0) //뱀으로 이동한 좌표 + nx = snake[nx]; + q.push({nx, cnt + 1}); + visit[nx] = true; + } + } + +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int idx; + + cin >> N >> M; + for(int i = 0; i < N; i++) { + cin >> idx >> ladder[idx]; + } + for(int i = 0; i < M; i++) { + cin >> idx >> snake[idx]; + } + + cout << bfs() << "\n"; + + return 0; +} diff --git a/09/kang/17144.cpp b/09/kang/17144.cpp new file mode 100644 index 0000000..4bebc04 --- /dev/null +++ b/09/kang/17144.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +using namespace std; + +int R, C, T; +int map[51][51]; +int temp[51][51]; +int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; +int machine_up_r; +int machine_down_r; + +void spreadDust() { + memset(temp, 0, sizeof(temp)); + queue> q; + int x, y, nx, ny, dCnt; + + for(int i = 0; i < R; i++) { + for(int j = 0; j < C; j++) { + if(map[i][j] != 0) { + q.push({i, j}); + temp[i][j] = map[i][j]; + } + } + } + + while(!q.empty()) { + x = q.front().first; + y = q.front().second; + q.pop(); + dCnt = 0; + for(int i = 0; i < 4; i++) { + nx = x + d[i][0]; + ny = y + d[i][1]; + if(nx < 0 || nx >= R || ny < 0 || ny >= C) + continue; + if(map[nx][ny] == -1) + continue; + temp[nx][ny] += floor(map[x][y] / 5); + dCnt++; + } + temp[x][y] -= floor(map[x][y] / 5) * dCnt; + } + + for(int i = 0; i < R; i++) { + for(int j = 0; j < C; j++) { + map[i][j] = temp[i][j]; + } + } +} + +void runMachine() { + //위쪽 공기청정기 + for(int r = machine_up_r - 1; r > 0; r--) { + map[r][0] = map[r - 1][0]; + } + for(int c = 1; c < C; c++) { + map[0][c - 1] = map[0][c]; + } + for(int r = 1; r <= machine_up_r; r++) { + map[r - 1][C - 1] = map[r][C - 1]; + } + for(int c = C - 2; c >= 1; c--) { + map[machine_up_r][c + 1] = map[machine_up_r][c]; + } + map[machine_up_r][1] = 0; + + //아래쪽 공기청정기 + for(int r = machine_down_r + 2; r < R; r++) { + map[r - 1][0] = map[r][0]; + } + for(int c = 1; c < C; c++) { + map[R - 1][c - 1] = map[R - 1][c]; + } + for(int r = R - 2; r >= machine_down_r; r--) { + map[r + 1][C - 1] = map[r][C - 1]; + } + for(int c = C - 2; c >= 1; c--) { + map[machine_down_r][c + 1] = map[machine_down_r][c]; + } + map[machine_down_r][1] = 0; +} + +int calcDust() { + int cnt = 0; + for(int i = 0; i < R; i++) { + for(int j = 0; j < C; j++) { + if(map[i][j] > 0) { + cnt += map[i][j]; + } + } + } + return cnt; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> R >> C >> T; + for(int i = 0; i < R; i++) { + for(int j = 0; j < C; j++) { + cin >> map[i][j]; + if(map[i][j] == -1) { + if(machine_up_r == 0) + machine_up_r = i; + else + machine_down_r = i; + } + } + } + + while(T--) { + spreadDust(); + runMachine(); + } + cout << calcDust(); +} diff --git a/09/kang/1946.cpp b/09/kang/1946.cpp new file mode 100644 index 0000000..8de15fb --- /dev/null +++ b/09/kang/1946.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +using namespace std; + +int main() { + int t, n; + int ans = 0, temp_min; + + cin >> t; + while (t > 0) { + cin >> n; + + ans = 0; + temp_min = n; + vector > rank(n); + + for (int i = 0; i < n; i++) { + cin >> rank[i].first >> rank[i].second; + } + + sort(rank.begin(), rank.end()); + + for (int i = 0; i < n; i++) { + if (rank[i].second <= temp_min) { + ans++; + temp_min = rank[i].second; + } + } + + cout << ans << "\n"; + + t--; + } + +} diff --git a/09/kang/30804.cpp b/09/kang/30804.cpp new file mode 100644 index 0000000..62d94b2 --- /dev/null +++ b/09/kang/30804.cpp @@ -0,0 +1,50 @@ +#include +#include + +using namespace std; + +int N; +int s[200003]; +int f[2]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + cin >> N; + for(int i = 0; i < N; i++) { + cin >> s[i]; + } + + int maxCnt = 1; + int cnt = 0; + for(int i = 0; i < N; i++) { + bool isFind = false; + for(int j = 0; j < 2; j++) { //과일이 있는지 판단 + if(s[i] == f[j]) { + isFind = true; + cnt++; + break; + } + } + if(!isFind) { //새로운 과일이면 + maxCnt = max(maxCnt, cnt); + + int j = i - 1; + int bCnt = 0; + int bf = i - 1 >= 0 ? s[i - 1] : 0; + while(j >= 0 && s[j] == bf) { + bCnt++; + j--; + } + + cnt = bCnt + 1; //이전 과일 개수 + 새로운 과일 개수(1) + f[0] = bf; + f[1] = s[i]; + } + } + maxCnt = max(maxCnt, cnt); + cout << maxCnt << "\n"; + + return 0; +}