Skip to content

Commit

Permalink
Merge pull request #65 from HYU-PS-STUDY/wimmings
Browse files Browse the repository at this point in the history
[week-10] 13335, 16234
  • Loading branch information
clean2001 authored Mar 30, 2024
2 parents e47df62 + 2dbb4c2 commit cc87df5
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
57 changes: 57 additions & 0 deletions week10/wimmings/13335.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include <vector>
#include <string>
#include <memory.h>
#include <queue>
#include <algorithm>
#include <deque>

using namespace std;
int n, w, L;

struct Truck {
int weight, dist;
};
deque<Truck> deq;
queue<int> trucks;

int main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> w >> L;
for (int i = 0; i < n; ++i) {
int x; cin >> x;
trucks.push(x);
}

// 초기 설정 : 1번 트럭을 덱에 넣어놓자.
int ans = 1;
int sum = trucks.front(); // 총 하중은 1번 트럭 무게
deq.push_back({trucks.front(), 0}); // { 1번 트럭 무게, 이동한 거리 } 덱에 삽입
trucks.pop(); // 1번 트럭 pop

while (!deq.empty() || !trucks.empty()) { // deq과 trucks가 모두 비어있을 때 끝
for (int i = 0; i < deq.size(); ++i) {
deq[i].dist++; // 덱에 있는 모든 트럭 한 칸씩 이동
}
if (deq.front().dist == w) { // 다리 길이만큼 다 이동했으면 다리 통과 -> sum에서 빼주고 덱에서도 빼주기
sum -= deq.front().weight;
deq.pop_front();
}

/*
덱에 넣는 상황 : trucks가 비어있어도 trucks.front()가 0으로 잡힌다.
그래서 비어있는지 체크 그리고 하중 남아있으면 덱에 넣기,
근데 위에서 다리 끝까지 간 애는 덱에서 빼주기 때문에 새로 트럭을 넣을 땐 항상 자리가 있긴 함.
*/
if (!trucks.empty() && sum + trucks.front() <= L) {
sum += trucks.front();
deq.push_back({trucks.front(), 0});
trucks.pop();
}
ans++;
}

cout << ans << '\n';

return 0;
}
95 changes: 95 additions & 0 deletions week10/wimmings/16234.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <iostream>
#include <vector>
#include <string>
#include <memory.h>
#include <queue>
#include <algorithm>
#include <cmath>

using namespace std;
typedef pair<int, int> pii;

int n, L, R;
int map[52][52];
bool visited[52][52];


int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

bool open(int x, int y, int nx, int ny) {
int diff = abs(map[x][y] - map[nx][ny]);
return diff >= L && diff <= R;
}

int bfs(int r, int c) {
queue<pii> q;
vector<pii> v;

q.push({r, c});
v.push_back({r, c});

visited[r][c] = 1;
int sum = 0;
int flag = false;

while (!q.empty()) {
pii now = q.front();
sum += map[now.first][now.second];
q.pop();

for (int i = 0; i < 4; ++i) {
int nx = now.first + dx[i];
int ny = now.second + dy[i];
if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if (visited[nx][ny] || !open(now.first, now.second, nx, ny)) continue;

flag = true;
visited[nx][ny] = 1;
q.push({nx, ny});
v.push_back({nx, ny});
}
}

if (flag) { // 인구 이동 있다
int avg = sum / v.size();
for (int i = 0; i < v.size(); ++i) {
map[v[i].first][v[i].second] = avg;
}
return 1;
}
else { // 없다
return -1;
}

}
int ans;
int main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> L >> R;

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> map[i][j];
}
}
bool move = false;

for (; ; ) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (visited[i][j]) continue;
int res = bfs(i, j);
if (res == -1) continue; // 한 번이라도 인구이동 발생해서 bfs 값이 -1이 아니라면 move가 true.
move = true;
}
}
if (move) ans++;
else break;
memset(visited, 0, sizeof(visited));
move = false;
}
cout << ans << '\n';

return 0;
}

0 comments on commit cc87df5

Please sign in to comment.