From 8f55b536e95f9c11215547df541b2dad591da033 Mon Sep 17 00:00:00 2001 From: woJSwo <68185825+jaeseo222@users.noreply.github.com> Date: Tue, 7 Dec 2021 12:29:35 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Delete=2011=EC=9B=94=2030=EC=9D=BC=20-=20?= =?UTF-8?q?=EC=B5=9C=EC=86=8C=20=EC=8B=A0=EC=9E=A5=20=ED=8A=B8=EB=A6=AC=20?= =?UTF-8?q?directory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../16235.cpp" | 128 ------------------ .../1713.cpp" | 54 -------- 2 files changed, 182 deletions(-) delete mode 100644 "11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/16235.cpp" delete mode 100644 "11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/1713.cpp" diff --git "a/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/16235.cpp" "b/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/16235.cpp" deleted file mode 100644 index 3f1c0ef..0000000 --- "a/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/16235.cpp" +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace std; -typedef vector> matrix; -typedef tuple tp; - -queue spring(matrix& land, deque& tree, queue>& breeding_tree) { - //하나의 칸마다 나이가 어린 나무부터 자신의 나이만큼 양분을 먹고, 나이가 1 증가함 - //각 칸에 양분이 부족해 자신의 나이만큼 양분을 못 먹는 나무는 즉시 죽음 - - queue dead_tree;//죽은 나무 - int size = tree.size();//현재 남은 나무 개수 - while (size--) { //모든 나무 검사 - int age = get<0>(tree.front()); //나이 - int row = get<1>(tree.front()); //행 - int col = get<2>(tree.front()); //열 - tree.pop_front();//pop하여 삭제 - - if (land[row][col] < age) { //자신의 나이만큼 양분을 먹을 수 없다면 - dead_tree.push({ age, row, col });//죽은 나무로 저장 - continue; - } - - // 자신의 나이만큼 양분을 먹을 수 있다면 - land[row][col] -= age; // 땅에 있는 양분을 나이만큼 먹고 - tree.emplace_back(age + 1, row, col); // 나이 한 살 증가한 나무로 저장 - if ((age + 1) % 5 == 0) // 나이 한 살 증가한 후 나이가 5의 배수라면 - breeding_tree.push({ row, col });//번식하는 나무로 저장 - } - return dead_tree; -} - -void summer(queue& dead_tree, matrix& land) { - // 봄에 죽은 나무가 양분으로 변함. 죽은 나무마다 나이를 2로 나눈 값이 양분으로 추가 (소수점 버림) - - while (!dead_tree.empty()) { // 죽은 나무들 모두 검사 - int age = get<0>(dead_tree.front()); //죽은 나무의 나이 - int row = get<1>(dead_tree.front()); //죽은 나무의 행 위치 - int col = get<2>(dead_tree.front()); //죽은 나무의 열 위치 - dead_tree.pop();//pop하여 삭제 - land[row][col] += (age / 2); // 양분으로 변함 - } -} - -void fall(int n, deque& tree, queue>& breeding_tree) { - // 나이가 5의 배수인 나무가 번식. 인접한 8개 칸에 나이가 1인 나무가 생김 - - // (r-1, c), (r+1, c), (r, c-1), (r, c+1), (r-1, c-1), (r-1, c+1), (r+1, c-1), (r+1, c+1) - int dr[8] = { -1, 1, 0, 0, -1, -1, 1, 1 }; - int dc[8] = { 0, 0, -1, 1, -1, 1, -1, 1 }; - - while (!breeding_tree.empty()) { // 번식할 나무 모두 검사 - int row = breeding_tree.front().first; //번식할 나무의 행 - int col = breeding_tree.front().second; //번식할 나무의 열 - breeding_tree.pop();//pop하여 삭제 - - for (int j = 0; j < 8; j++) { // 인접한 8개 칸에 - int nr = row + dr[j]; - int nc = col + dc[j]; - if (nr < 0 || nr >= n || nc < 0 || nc >= n) //범위 벗어날 시 continue - continue; - tree.push_front({ 1, nr, nc }); // 새로 생긴 나이가 1인 나무 - } - } -} - -void winter(int n, matrix& a, matrix& land) { - // 로봇(S2D2)이 땅을 돌아다니면서 A[r][c]만큼 각 칸에 양분 추가 - - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - land[i][j] += a[i][j]; // 입력해놨던 A[r][c]만큼 땅에 양분 추가 -} - -/** - * [문제 설명] - 단순 구현 문제 - * 봄: 하나의 칸마다 나이가 어린 나무부터 자신의 나이만큼 양분을 먹고, 나이가 1 증가함 - * 각 칸에 양분이 부족해 자신의 나이만큼 양분을 못 먹는 나무는 즉시 죽음 - * 여름: 봄에 죽은 나무가 양분으로 변함. 죽은 나무마다 나이를 2로 나눈 값이 양분으로 추가 (소수점 버림) - * 가을: 나이가 5의 배수인 나무가 번식. 인접한 8개 칸에 나이가 1인 나무가 생김 - * 겨울: 로봇(S2D2)이 땅을 돌아다니면서 A[r][c]만큼 각 칸에 양분 추가 - * - * K년이 지난 후 상도의 땅에 살아있는 나무의 개수 - * - * [문제 풀이] - * a: 로봇(S2D2)가 겨울에 주는 양분의 양 - * land: 땅의 양분 - * breeding_tree: 나이가 5의 배수인 트리 (번식할 트리) - * tree: 땅에 심은 나무 나이, 행, 열 정보 - * -> deque 컨테이너를 활용해 번식한 나무를 앞에 넣어주면 입력 후에만 정렬해서 사용 가능 - * - * 문제의 설명대로 계절별 연산을 진행 - */ - -int main() { - int n, m, k, x, y, z;//n: 땅 크기, m: 나무개수, k: k년 - - //입력 - cin >> n >> m >> k; - matrix a(n, vector(n, 0)); - matrix land(n, vector(n, 5)); //처음 양분 모든 칸에 5 - queue> breeding_tree; //번식할 트리 - deque tree; - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - cin >> a[i][j];// 로봇이 겨울에 주는 양분의 양 저장 - while (m--) { - cin >> x >> y >> z;//x,y: 나무 위치, z : 나무의 나이 - tree.emplace_back(z, x - 1, y - 1); //(0, 0)부터 시작하도록 구현하기위해 1을 빼준 인덱스에 접근 - } - - //연산 - sort(tree.begin(), tree.end()); //어린 나이 순으로 정렬 - while (k--) { - queue dead_tree = spring(land, tree, breeding_tree); //봄이 지나고 죽은 나무 - summer(dead_tree, land);//여름 - fall(n, tree, breeding_tree);//가을 - winter(n, a, land);//겨울 - } - - //출력 - cout << tree.size();//k년이 지나고 살아남은 나무의 수 -} \ No newline at end of file diff --git "a/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/1713.cpp" "b/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/1713.cpp" deleted file mode 100644 index 682e3a3..0000000 --- "a/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/1713.cpp" +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include - -using namespace std; -typedef pair ci; - -map::iterator delCandidate(map& candidate) { - auto del = candidate.begin(); //처음 후보를 삭제한다 가정 - int cnt = candidate.begin()->second.first; //처음 후보의 추천 횟수 - int t = candidate.begin()->second.second; //처음 후보의 게시 시간 - for (auto iter = ++candidate.begin(); iter != candidate.end(); iter++) {// 남은 후보들 중에서 - int cur_cnt = iter->second.first;// 현재 후보의 추천 횟수 - int cur_t = iter->second.second;//현재 후보의 게시 시간 - if (cur_cnt < cnt) { //추천 횟수가 가장 작은 후보 찾기 - cnt = cur_cnt; // 추천 횟수 가장 작은 후보의 추천 횟수로 갱신 - t = cur_t; // 추천 횟수 가장 작은 후보의 게시 시간으로 갱신 - del = iter; // 추천 횟수 가장 작은 후보로 갱신 - } - else if (cur_cnt == cnt && cur_t < t) { //추천 횟수가 가장 작은 후보가 여러명이라면, 게시 시간이 오래된 후보 찾기 - t = cur_t; //위 조건에 맞는 후보의 게시 시간으로 갱신 - del = iter;//위 조건에 맞는 후보로 갱신 - } - } - return del;//가장 추천수가 작은 학생 중 게시 시간이 오래된 학생 리턴 -} - -/** - * 1. 비어있는 사진틀이 없는 경우, 가장 추천수가 작은 학생 중 게시 시간이 오래된 학생을 삭제 - * 2. 후보 학생을 바로 찾기 위해 본 풀이는 map 컨테이너를 사용해 구현 - * - * !주의! 게시 시간 정보 저장 시, 후보로 올라간 가장 첫 시간을 저장. 이미 후보에 있는데 게시 시간이 갱신되지 않도록 주의. - */ - -int main() { - int n, m, input;//n: 사진틀의 개수, m: 전체 학생의 총 추천 횟수 - - //입력 & 연산 - cin >> n >> m; - map candidate; //first: 후보 학생, second: {추천 횟수, 게시 시간} - for (int i = 0; i < m; i++) { - cin >> input; - if (candidate.size() == n && candidate.find(input) == candidate.end()) //비어있는 사진틀이 없는 경우 - candidate.erase(delCandidate(candidate)); // 가장 추천수가 작은 학생 중 게시 시간이 오래된 학생 - - if (candidate.find(input) == candidate.end()) //첫 게시라면(비어있는 사진틀이 있는 경우) - candidate[input].second = i;// 그 사진틀에 후보 사진 게시 - candidate[input].first++; //추천 횟수 증가 - } - - //출력 - for (auto iter = candidate.begin(); iter != candidate.end(); iter++) - cout << iter->first << ' ';// 최종 후보의 학생 번호 -} \ No newline at end of file From 1cfbfb3b342fd46ef09219fa9a72340692daef60 Mon Sep 17 00:00:00 2001 From: "jaeserrr@ewhain.net" Date: Tue, 7 Dec 2021 12:31:12 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[=EC=B5=9C=EC=86=8C=20=EC=8B=A0=EC=9E=A5=20?= =?UTF-8?q?=ED=8A=B8=EB=A6=AC]=2012=EC=9B=94=207=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 샘플 코드 보며 공부하였습니다! 항상 처음에 구현 문제를 맞닥뜨리면 뭐부터 해야 할 지 막막하게만 느껴지는데, 샘플 코드에서 차근차근히 설명해주셔서 너무 감사드리고 감을 잡게 되는 것 같아요! 항상 좋은 코드와 좋은 수업, 좋은 피드백 감사드립니다! --- .../16235.cpp" | 128 ++++++++++++++++++ .../1713.cpp" | 54 ++++++++ 2 files changed, 182 insertions(+) create mode 100644 "11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/16235.cpp" create mode 100644 "11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/1713.cpp" diff --git "a/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/16235.cpp" "b/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/16235.cpp" new file mode 100644 index 0000000..3f1c0ef --- /dev/null +++ "b/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/16235.cpp" @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +typedef vector> matrix; +typedef tuple tp; + +queue spring(matrix& land, deque& tree, queue>& breeding_tree) { + //하나의 칸마다 나이가 어린 나무부터 자신의 나이만큼 양분을 먹고, 나이가 1 증가함 + //각 칸에 양분이 부족해 자신의 나이만큼 양분을 못 먹는 나무는 즉시 죽음 + + queue dead_tree;//죽은 나무 + int size = tree.size();//현재 남은 나무 개수 + while (size--) { //모든 나무 검사 + int age = get<0>(tree.front()); //나이 + int row = get<1>(tree.front()); //행 + int col = get<2>(tree.front()); //열 + tree.pop_front();//pop하여 삭제 + + if (land[row][col] < age) { //자신의 나이만큼 양분을 먹을 수 없다면 + dead_tree.push({ age, row, col });//죽은 나무로 저장 + continue; + } + + // 자신의 나이만큼 양분을 먹을 수 있다면 + land[row][col] -= age; // 땅에 있는 양분을 나이만큼 먹고 + tree.emplace_back(age + 1, row, col); // 나이 한 살 증가한 나무로 저장 + if ((age + 1) % 5 == 0) // 나이 한 살 증가한 후 나이가 5의 배수라면 + breeding_tree.push({ row, col });//번식하는 나무로 저장 + } + return dead_tree; +} + +void summer(queue& dead_tree, matrix& land) { + // 봄에 죽은 나무가 양분으로 변함. 죽은 나무마다 나이를 2로 나눈 값이 양분으로 추가 (소수점 버림) + + while (!dead_tree.empty()) { // 죽은 나무들 모두 검사 + int age = get<0>(dead_tree.front()); //죽은 나무의 나이 + int row = get<1>(dead_tree.front()); //죽은 나무의 행 위치 + int col = get<2>(dead_tree.front()); //죽은 나무의 열 위치 + dead_tree.pop();//pop하여 삭제 + land[row][col] += (age / 2); // 양분으로 변함 + } +} + +void fall(int n, deque& tree, queue>& breeding_tree) { + // 나이가 5의 배수인 나무가 번식. 인접한 8개 칸에 나이가 1인 나무가 생김 + + // (r-1, c), (r+1, c), (r, c-1), (r, c+1), (r-1, c-1), (r-1, c+1), (r+1, c-1), (r+1, c+1) + int dr[8] = { -1, 1, 0, 0, -1, -1, 1, 1 }; + int dc[8] = { 0, 0, -1, 1, -1, 1, -1, 1 }; + + while (!breeding_tree.empty()) { // 번식할 나무 모두 검사 + int row = breeding_tree.front().first; //번식할 나무의 행 + int col = breeding_tree.front().second; //번식할 나무의 열 + breeding_tree.pop();//pop하여 삭제 + + for (int j = 0; j < 8; j++) { // 인접한 8개 칸에 + int nr = row + dr[j]; + int nc = col + dc[j]; + if (nr < 0 || nr >= n || nc < 0 || nc >= n) //범위 벗어날 시 continue + continue; + tree.push_front({ 1, nr, nc }); // 새로 생긴 나이가 1인 나무 + } + } +} + +void winter(int n, matrix& a, matrix& land) { + // 로봇(S2D2)이 땅을 돌아다니면서 A[r][c]만큼 각 칸에 양분 추가 + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + land[i][j] += a[i][j]; // 입력해놨던 A[r][c]만큼 땅에 양분 추가 +} + +/** + * [문제 설명] - 단순 구현 문제 + * 봄: 하나의 칸마다 나이가 어린 나무부터 자신의 나이만큼 양분을 먹고, 나이가 1 증가함 + * 각 칸에 양분이 부족해 자신의 나이만큼 양분을 못 먹는 나무는 즉시 죽음 + * 여름: 봄에 죽은 나무가 양분으로 변함. 죽은 나무마다 나이를 2로 나눈 값이 양분으로 추가 (소수점 버림) + * 가을: 나이가 5의 배수인 나무가 번식. 인접한 8개 칸에 나이가 1인 나무가 생김 + * 겨울: 로봇(S2D2)이 땅을 돌아다니면서 A[r][c]만큼 각 칸에 양분 추가 + * + * K년이 지난 후 상도의 땅에 살아있는 나무의 개수 + * + * [문제 풀이] + * a: 로봇(S2D2)가 겨울에 주는 양분의 양 + * land: 땅의 양분 + * breeding_tree: 나이가 5의 배수인 트리 (번식할 트리) + * tree: 땅에 심은 나무 나이, 행, 열 정보 + * -> deque 컨테이너를 활용해 번식한 나무를 앞에 넣어주면 입력 후에만 정렬해서 사용 가능 + * + * 문제의 설명대로 계절별 연산을 진행 + */ + +int main() { + int n, m, k, x, y, z;//n: 땅 크기, m: 나무개수, k: k년 + + //입력 + cin >> n >> m >> k; + matrix a(n, vector(n, 0)); + matrix land(n, vector(n, 5)); //처음 양분 모든 칸에 5 + queue> breeding_tree; //번식할 트리 + deque tree; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + cin >> a[i][j];// 로봇이 겨울에 주는 양분의 양 저장 + while (m--) { + cin >> x >> y >> z;//x,y: 나무 위치, z : 나무의 나이 + tree.emplace_back(z, x - 1, y - 1); //(0, 0)부터 시작하도록 구현하기위해 1을 빼준 인덱스에 접근 + } + + //연산 + sort(tree.begin(), tree.end()); //어린 나이 순으로 정렬 + while (k--) { + queue dead_tree = spring(land, tree, breeding_tree); //봄이 지나고 죽은 나무 + summer(dead_tree, land);//여름 + fall(n, tree, breeding_tree);//가을 + winter(n, a, land);//겨울 + } + + //출력 + cout << tree.size();//k년이 지나고 살아남은 나무의 수 +} \ No newline at end of file diff --git "a/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/1713.cpp" "b/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/1713.cpp" new file mode 100644 index 0000000..682e3a3 --- /dev/null +++ "b/11\354\233\224 30\354\235\274 - \354\265\234\354\206\214 \354\213\240\354\236\245 \355\212\270\353\246\254/1713.cpp" @@ -0,0 +1,54 @@ +#include +#include +#include + +using namespace std; +typedef pair ci; + +map::iterator delCandidate(map& candidate) { + auto del = candidate.begin(); //처음 후보를 삭제한다 가정 + int cnt = candidate.begin()->second.first; //처음 후보의 추천 횟수 + int t = candidate.begin()->second.second; //처음 후보의 게시 시간 + for (auto iter = ++candidate.begin(); iter != candidate.end(); iter++) {// 남은 후보들 중에서 + int cur_cnt = iter->second.first;// 현재 후보의 추천 횟수 + int cur_t = iter->second.second;//현재 후보의 게시 시간 + if (cur_cnt < cnt) { //추천 횟수가 가장 작은 후보 찾기 + cnt = cur_cnt; // 추천 횟수 가장 작은 후보의 추천 횟수로 갱신 + t = cur_t; // 추천 횟수 가장 작은 후보의 게시 시간으로 갱신 + del = iter; // 추천 횟수 가장 작은 후보로 갱신 + } + else if (cur_cnt == cnt && cur_t < t) { //추천 횟수가 가장 작은 후보가 여러명이라면, 게시 시간이 오래된 후보 찾기 + t = cur_t; //위 조건에 맞는 후보의 게시 시간으로 갱신 + del = iter;//위 조건에 맞는 후보로 갱신 + } + } + return del;//가장 추천수가 작은 학생 중 게시 시간이 오래된 학생 리턴 +} + +/** + * 1. 비어있는 사진틀이 없는 경우, 가장 추천수가 작은 학생 중 게시 시간이 오래된 학생을 삭제 + * 2. 후보 학생을 바로 찾기 위해 본 풀이는 map 컨테이너를 사용해 구현 + * + * !주의! 게시 시간 정보 저장 시, 후보로 올라간 가장 첫 시간을 저장. 이미 후보에 있는데 게시 시간이 갱신되지 않도록 주의. + */ + +int main() { + int n, m, input;//n: 사진틀의 개수, m: 전체 학생의 총 추천 횟수 + + //입력 & 연산 + cin >> n >> m; + map candidate; //first: 후보 학생, second: {추천 횟수, 게시 시간} + for (int i = 0; i < m; i++) { + cin >> input; + if (candidate.size() == n && candidate.find(input) == candidate.end()) //비어있는 사진틀이 없는 경우 + candidate.erase(delCandidate(candidate)); // 가장 추천수가 작은 학생 중 게시 시간이 오래된 학생 + + if (candidate.find(input) == candidate.end()) //첫 게시라면(비어있는 사진틀이 있는 경우) + candidate[input].second = i;// 그 사진틀에 후보 사진 게시 + candidate[input].first++; //추천 횟수 증가 + } + + //출력 + for (auto iter = candidate.begin(); iter != candidate.end(); iter++) + cout << iter->first << ' ';// 최종 후보의 학생 번호 +} \ No newline at end of file