From 722f69b38e3138293dd5239f8c5c52e617e00fe3 Mon Sep 17 00:00:00 2001 From: wonkydd Date: Sun, 14 Apr 2024 19:02:17 +0900 Subject: [PATCH] [week-12] 3190, 16235 --- week12/wonkyDD/.gitkeep | 0 week12/wonkyDD/16235.cpp | 133 +++++++++++++++++++++++++++++++++++++++ week12/wonkyDD/3190.cpp | 117 ++++++++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+) delete mode 100644 week12/wonkyDD/.gitkeep create mode 100644 week12/wonkyDD/16235.cpp create mode 100644 week12/wonkyDD/3190.cpp diff --git a/week12/wonkyDD/.gitkeep b/week12/wonkyDD/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/week12/wonkyDD/16235.cpp b/week12/wonkyDD/16235.cpp new file mode 100644 index 0000000..f3d8c54 --- /dev/null +++ b/week12/wonkyDD/16235.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#define PLANE_MAX 11 +using namespace std; +int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1}; +int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1}; + + +int N,M,K; + +// NOTE : 매 겨울 채워지는 양분의 양 +int plane[PLANE_MAX][PLANE_MAX]; + +// NOTE : 각 칸의 양분 상태 +int nutrient[PLANE_MAX][PLANE_MAX]; + +/** + * NOTE + * - 각 칸에는 여러 개의 나무가 담길 수 있다 + * - 나무의 나이가 담기게 된다 +*/ +vector tree[PLANE_MAX][PLANE_MAX]; + +bool age_cmp(int a1, int a2) { + return a1 < a2; +} + +bool check_bound(int y, int x) { + return y>=1 && y<=N && x>=1 && x<=N; +} + +void spring_and_summer() { + // spring + for (int y=1; y<=N; ++y) { + for (int x=1; x<=N; ++x) { + if (tree[y][x].empty()) continue; + + // NOTE : 가장 어린 나무가 먼저 양분을 먹는다 + sort(tree[y][x].begin(), tree[y][x].end(), age_cmp); + + // NOTE : 양분을 섭취하고 나이가 먹은 나무들의 나이가 담긴다 + vector tmp; + + int die = 0; + + for (int i=0; i> N >> M >> K; + for (int i=1; i<=N; ++i) { + for (int j=1; j<=N; ++j) { + cin >> plane[i][j]; + nutrient[i][j] = 5; + } + } + + int y,x,age; + for (int i=0; i> y >> x >> age; + tree[y][x].push_back(age); + } + + while (K--) { + spring_and_summer(); + fall(); + winter(); + } + cout << answer(); + return 0; +} \ No newline at end of file diff --git a/week12/wonkyDD/3190.cpp b/week12/wonkyDD/3190.cpp new file mode 100644 index 0000000..235b66b --- /dev/null +++ b/week12/wonkyDD/3190.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +using namespace std; +#define BOARD_MAX 101 + +struct snake { + int y; int x; +}; + +// NOTE : Up, Down, Left, Right 순서 +int dy[] = {-1, 1, 0, 0}; +int dx[] = {0, 0, -1, 1}; + +int N,K,L; +unordered_map time_to_dir; +bool apple[BOARD_MAX][BOARD_MAX]; + +// NOTE : front: 꼬리, back: 머리 +deque dq; +// NOTE : 뱀은 처음에 오른쪽을 향한다. +int dir=3; +bool game_over; + +int dir_converter(char c) { + if (c == 'L') { + if (dir == 0) return 2; + if (dir == 1) return 3; + if (dir == 2) return 1; + if (dir == 3) return 0; + } + + if (c == 'D') { + if (dir == 0) return 3; + if (dir == 1) return 2; + if (dir == 2) return 0; + if (dir == 3) return 1; + } + + return -1; +} + +bool check_bound(int y, int x) { + return y>=1 && y<=N && x>=1 && x<=N; +} + +bool check_itself(int y, int x) { + for (const auto& p : dq) { + if (p.y == y && p.x == x) return false; + } + return true; +} + +void rotate(int time) { + char l_or_d = time_to_dir[time]; + dir = dir_converter(l_or_d); +} + +void move() { + // NOTE : dq.back == 머리, dq.front == 꼬리 + int ny = dq.back().y+dy[dir], nx = dq.back().x+dx[dir]; + + // NOTE : 만약 벽이나 자기자신의 몸과 부딪히면 게임이 끝난다 + if (!check_bound(ny, nx) || !check_itself(ny, nx)) { + game_over = true; + return; + } + + dq.push_back({ny, nx}); + + // NOTE : 만약 이동한 칸에 사과가 있다면, 그 칸에 있던 사과가 없어지고 꼬리는 움직이지 않는다 + if (apple[ny][nx]) { + apple[ny][nx] = false; + } + // NOTE : 만약 이동한 칸에 사과가 없다면, 몸길이를 줄여서 꼬리가 위치한 칸을 비워준다. 즉, 몸길이는 변하지 않는다. + else { + for (int i=0; i> N >> K; + int r,c; + for (int i=0; i> r >> c; + apple[r][c] = true; + } + cin >> L; + int t; char d; + for (int i=0; i> t >> d; + time_to_dir.insert({t, d}); + } + + int time = 0; + dq.push_back({1, 1}); + while (1) { + game_over = false; + + // NOTE : 먼저 뱀은 몸길이를 늘려 머리를 다음칸에 위치시킨다 + move(); + ++time; + + if (game_over) break; + + // NOTE : 방향을 전환해야 하는 순간이면 전환한다 + if (time_to_dir.find(time) != time_to_dir.end()) rotate(time); + } + cout << time; + return 0; +} \ No newline at end of file