QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#537372#9224. Express Evictionttbchimbu999#TL 0ms3700kbC++203.4kb2024-08-30 11:14:462024-08-30 11:14:46

Judging History

你现在查看的是最新测评结果

  • [2024-08-30 11:14:46]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3700kb
  • [2024-08-30 11:14:46]
  • 提交

answer

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(), x.end()

using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
    if (a > b) return a = b, true;
    return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
    if (a < b) return a = b, true;
    return false;
}

const int N = 55;
const int M = 50 * 50;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
struct Node {
    int x, y;
    // int d;
    vector<int> v;
    Node() {}
    Node(int x, int y, vector<int> v) : 
        x(x), y(y), v(v) {}
};
int n, m;
char a[N][N];
int p[N][N];
vector<Node> q[N * N];
map<vector<int>, bool> flag[N][N];

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    } 
    int num = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (a[i][j] == '#') {
                p[i][j] = ++ num;
            }
        }
    }
    auto get = [&](int x, int y, int n_x, int n_y) {
        vector<int> res;
        if (n_x == x + 1) {
            if (p[n_x][n_y]) {
                res.push_back(p[n_x][n_y]);
            }
            if (n_y + 1 <= m && p[n_x][n_y + 1]) {
                res.push_back(p[n_x][n_y + 1]);
            }
        }
        if (n_x == x - 1) {
            if (p[x][y]) {
                res.push_back(p[x][y]);
            }
            if (y + 1 <= m && p[x][y + 1]) {
                res.push_back(p[x][y + 1]);
            }
        }
        if (n_y == y + 1) {
            if (p[n_x][n_y]) {
                res.push_back(p[n_x][n_y]);
            }
            if (n_x + 1 <= n && p[n_x + 1][n_y]) {
                res.push_back(p[n_x][n_y + 1]);
            }
        }
        if (n_y == y - 1) {
            if (p[x][y]) {
                res.push_back(p[x][y]);
            }
            if (x + 1 <= n && p[x + 1][y]) {
                res.push_back(p[x + 1][y]);
            }
        }
        return res;
    };
    q[0].emplace_back(0, 0, vector<int>());
    flag[0][0][vector<int>()] = true;
    for (int d = 0; d <= n + m; d++) {
        while (!q[d].empty()) {
            auto [x, y, v] = q[d].back();
            q[d].pop_back();
            if (mp(x, y) == mp(n, m)) {
                cout << d;
                // for (int x: v) {
                //     cerr << x << '\n';
                // }
                return 0;
            }
            // flag[x][y][v] = true;
            // cerr << x << ' ' << y << '\n';
            for (int i = 0; i < 4; i++) {
                auto [n_x, n_y] = mp(x + dx[i], y + dy[i]);
                if (n_x < 0 || n_y < 0 || n_x > n || n_y > m) {
                    continue;
                }
                // cerr << n_x << ' ' << n_y << '\n';
                vector<int> tmp(v);
                vector<int> nxt(get(x, y, n_x, n_y));
                for (int i: nxt) {
                    tmp.push_back(i);
                }
                sort(all(tmp));
                if (flag[n_x][n_y].count(tmp)) {
                    continue;
                }
                flag[n_x][n_y][tmp] = true;
                q[(int) tmp.size()].emplace_back(n_x, n_y, tmp);
            }
        }
    }
    cout << 0;
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3700kb

input:

4 6
.##...
.#....
##....
....#.

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: -100
Time Limit Exceeded

input:

20 30
...###########################
#..###########################
...###########################
...###########################
.#############################
...###########################
...###########################
#..###########################
...###########################
..#...............

output:


result: