90分求条
查看原帖
90分求条
766517
Loop1st楼主2024/9/24 12:27
#include <bits/stdc++.h>
using namespace std;
const int N = 2e2 + 10;
const int D[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
typedef long long ll;
typedef pair<int, int> pii;
struct Position {
    int x, y;
};
struct Snuke {
    int len, id;
    Position head;
    deque<Position>body;
    bool operator < (const Snuke &A) const {
        return len > A.len;
    }
} a[N];
map<char, Position>mp; 
int n, m, k, tot, bel[N][N], food[N][N], vis[N][N], cnt;
char s[N][N], op[N][N];
int main() {
    mp['W'] = {-1, 0}, mp['A'] = {0, -1}, mp['S'] = {1, 0}, mp['D'] = {0, 1};
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%s", s[i] + 1);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (s[i][j] == '&') {
                food[i][j] = 1;
                cnt++;
            } else if (s[i][j] == '@') {
                tot++;
                a[tot].id = tot;
                a[tot].len++;
                a[tot].head = {i, j};
                vis[i][j] = 1;
                bel[i][j] = i;
                a[tot].body.push_back({i, j});
                queue<Position>q;
                q.push({i, j});
                while (!q.empty()) {
                    auto [x, y] = q.front(); q.pop();
                    for (int k = 0; k < 4; k++) {
                        int xx = x + D[k][0], yy = y + D[k][1];
                        if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy]) continue;
                        if (s[xx][yy] == '#')  {
                            bel[xx][yy] = tot;
                            vis[xx][yy] = 1;
                            a[tot].len++;
                            q.push({xx, yy});
                            a[tot].body.push_back({xx, yy});
                        }
                    }
                }
                
            }
        }
    }
    for (int i = 1; i <= tot; i++) 
        scanf("%s", op[i] + 1);
    for (int j = 1; j <= k; j++) {
        for (int i = 1; i <= tot; i++) if (a[i].len != 0) {
            int x = a[i].head.x + mp[op[i][j]].x, y = a[i].head.y + mp[op[i][j]].y;
            if (x < 1 || x > n || y < 1 || y > m || bel[x][y]) {
                a[i].len = 0;
                a[i].head = {0, 0};
                for (auto [xx, yy] : a[i].body) {
                    food[xx][yy] = 1;
                    bel[xx][yy] = 0;
                    cnt++;
                } 
            } else {
                if (food[x][y]) {
                    cnt--;
                    food[x][y] = 0;
                    a[i].len++;
                    a[i].head = {x, y};
                    bel[x][y] = tot;
                    a[i].body.push_front({x, y});
                } else {
                    a[i].head = {x, y};
                    a[i].body.push_front({x, y});
                    bel[x][y] = tot;
                    auto [xx, yy] = a[i].body.back();
                    bel[xx][yy] = 0;
                    a[i].body.pop_back();
                }
            }
        }
    }
    sort(a + 1, a + tot + 1);
    for (int i = 1; i <= tot; i++) printf("%d %d\n", a[i].len, a[i].id);
    cnt = 0;
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (food[i][j]) cnt++;
    printf("%d\n", cnt);
    return 0;
}
2024/9/24 12:27
加载中...