Skip to content

09 #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

09 #38

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions 09/kang/1389.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

int N, M;
vector<int> adj[103];
int dist[103];
vector<pair<int, int>> kb;

int bfs(int s) {
queue<int> 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;
}
56 changes: 56 additions & 0 deletions 09/kang/16928.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <queue>
using namespace std;

int N, M;
int ladder[103];
int snake[103];
bool visit[103];

int bfs() {
queue<pair<int , int>> 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;
}
119 changes: 119 additions & 0 deletions 09/kang/17144.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
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<pair<int, int>> 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();
}
37 changes: 37 additions & 0 deletions 09/kang/1946.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <vector>
#include <algorithm>

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 <pair<int, int>> 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--;
}

}
50 changes: 50 additions & 0 deletions 09/kang/30804.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <map>

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;
}