Skip to content

Instantly share code, notes, and snippets.

@Macesuted
Created March 8, 2022 09:49
Show Gist options
  • Save Macesuted/2a90253b9ea5821745598ffccb0f4ee4 to your computer and use it in GitHub Desktop.
Save Macesuted/2a90253b9ea5821745598ffccb0f4ee4 to your computer and use it in GitHub Desktop.
LG4944 Code
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
const int maxn = 205;
const int maxk = 105;
const int way[2][4] = {{1, 0, -1, 0}, {0, 1, 0, -1}};
char map[maxn][maxn], cmd[maxn][maxk];
bool vis[maxn][maxn];
int n, m, restFood;
struct node {
int x, y;
};
class Snake {
private:
std::deque<node> body;
void die(void) {
live = false;
len = 0;
while (!body.empty()) {
node cac = body.front();
body.pop_front();
map[cac.x][cac.y] = '&', restFood++;
}
return;
}
public:
Snake(int x, int y, int id) {
this->id = id;
len = 0;
live = true;
static std::queue<node> que;
que.push((node){x, y});
while (!que.empty()) {
node cac = que.front();
vis[cac.x][cac.y] = true;
body.push_back(cac), len++;
que.pop();
for (int i = 0; i < 4; i++) {
int tx = cac.x + way[0][i], ty = cac.y + way[1][i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m &&
!vis[tx][ty] && map[tx][ty] == '#') que.push((node){tx, ty});
}
}
return;
}
~Snake(void) {
body.clear();
return;
}
bool live;
int id, len;
void move(char way) {
node to = body.front();
map[to.x][to.y] = '#';
if (way == 'W') to.x--;
if (way == 'S') to.x++;
if (way == 'A') to.y--;
if (way == 'D') to.y++;
if (to.x < 1 || to.x > n || to.y < 1 || to.y > m ||
map[to.x][to.y] == '#' || map[to.x][to.y] == '@') return die();
body.push_front(to), len++;
if (map[to.x][to.y] == '&') return restFood--, void(map[to.x][to.y] = '@');
map[to.x][to.y] = '@';
map[body.back().x][body.back().y] = '.';
return len--, body.pop_back();
}
inline bool operator<(const Snake& oth) const {
return this->len > oth.len || this->len == oth.len && this->id < oth.id;
}
};
std::vector<Snake> snakes;
int main() {
std::ios::sync_with_stdio(false);
int k, c;
std::cin >> n >> m >> k;
restFood = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
std::cin >> map[i][j];
if (map[i][j] == '&') restFood++;
}
c = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (map[i][j] == '@') snakes.push_back((Snake){i, j, ++c});
for (int i = 0; i < c; i++)
for (int j = 1; j <= k; j++)
std::cin >> cmd[i][j];
for (int tim = 1; tim <= k; tim++)
for (int p = 0; p < c; p++) {
if (!snakes[p].live) continue;
snakes[p].move(cmd[p][tim]);
}
std::sort(snakes.begin(), snakes.end());
for (std::vector<Snake>::iterator i = snakes.begin(); i != snakes.end(); i++)
std::cout << i->len << ' ' << i->id << std::endl;
std::cout << restFood << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment