QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#662295#8236. Snake MovergrgtgrfWA 589ms329632kbC++233.4kb2024-10-20 22:38:082024-10-20 22:38:08

Judging History

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

  • [2024-10-20 22:38:08]
  • 评测
  • 测评结果:WA
  • 用时:589ms
  • 内存:329632kb
  • [2024-10-20 22:38:08]
  • 提交

answer

// #pragma GCC optimize(2, "Ofast", "inline")
#include <bits/stdc++.h>
// using pair<int,int>= pair<int,int>;
// using tuple<int,int,int>= tuple<int,int,int>;
using ll = long long;
using ull = unsigned long long;
using namespace std;
const int inf = 1e9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
void solve()
{
    int n, m, k;
    cin >> n >> m >> k;

    vector<pair<int,int>> snack(k);
    // map<pair<int,int>, int> mp;
    vector<int> mp(n * m + 1000, 0);
    for(int i = 0; i < k; i++) {
        cin >> snack[i].first >> snack[i].second;
        snack[i].first--;
        snack[i].second--;
        if(i)mp[snack[i].first * m + snack[i].second] = k - i;
    }

    vector<string> qizi(n);
    for(int i = 0; i < n; i++) {
        cin >> qizi[i];
    }

    int odir = -1;
    for(int i = 0; i < 4; i++) {
        if(snack[1].first+ dx[i] == snack[0].first && snack[1].second + dy[i] == snack[0].second) {
            odir = i;
        }
    }

    vector<int> f(n * m, inf);
    // f[snack[0].first][snack[0].second] = 0;
    deque<tuple<int,int,int>> pq;
    pq.push_back({0, odir, snack[0].first * m + snack[0].second});
    vector<vector<pair<int, int>>> v(n * m + 1);
    while(!pq.empty()) {
        auto [d, dir, pos] = pq.front();
        pq.pop_front();

        if(!v[d].empty()) {
            for(int i = 0; i < int(v[d].size()); i++) {
                auto [a, b] = v[d].back();
                v[d].pop_back();
                pq.push_front({d, a, b});
            }
        }

        int x = pos / m;
        int y = pos % m;
        if(f[pos] != inf) {
            continue;
        }
        // cerr << x + 1 << " " << y + 1 << " " << d << endl;
        f[pos] = d;
        for(int i = 0; i < 4; i++) {
            if(dir == (i + 2) % 4) continue;
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 0 || nx >= n || ny < 0 || ny >= m || qizi[nx][ny] == '#') continue;
                // cerr << mp[nx * m + ny] << endl;
            if(mp[nx * m + ny] > d + 1) {
                v[mp[nx * m + ny]].push_back({i, nx * m + ny});
            } else {
                pq.push_back({d + 1, i, nx * m + ny});
            }
            // cout << nx << " " << ny << endl;
        }
    }

    int tim = k - 2;
    tim = max(0, tim);
    queue<tuple<int,int,int>> q;
    q.push({snack[0].first, snack[0].second, tim});
    vector<int> dis(n * m, inf);
    dis[snack[0].first * m + snack[0].second] = tim;
    while(!q.empty()) {
        auto [x, y, d] = q.front();
        q.pop();

        for(int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 0 || nx >= n || ny < 0 || ny >= m || qizi[nx][ny] == '#') continue;
            if(dis[nx * m + ny] == inf){
                dis[nx * m + ny] = min(d + 1, dis[nx * m + ny]);
                q.push({nx, ny, d + 1});
            }
        }
    }

    ull res = 0;
    for(int i = 0; i < n * m; i++) {
        int ans = dis[i];
        for(int j = 0; j < 4; j++) {
            ans = min(ans, f[i]);
        }
        if(ans == inf) {
            ans = 0;
        }
        // cerr << ans << " \n"[(i + 1) % m == 0];
        res += 1ll * ans * ans;
    }

    cout << res << "\n";
}
signed main()
{
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    int tt = 1;
    // std::cin >> tt;
    while (tt--)
        solve();
}

详细

Test #1:

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

input:

4 5 5
3 5
3 4
3 3
3 2
4 2
.....
.....
.....
.....

output:

293

result:

ok single line: '293'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

2 2 4
1 1
1 2
2 2
2 1
..
..

output:

14

result:

ok single line: '14'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3836kb

input:

5 5 3
1 2
1 1
2 1
.....
.###.
.#.#.
.###.
.....

output:

407

result:

ok single line: '407'

Test #4:

score: 0
Accepted
time: 421ms
memory: 318432kb

input:

3000 2900 1
1882 526
........................................................................................................#................................................................................................................................................................#................

output:

35141960580077

result:

ok single line: '35141960580077'

Test #5:

score: 0
Accepted
time: 581ms
memory: 318500kb

input:

2900 3000 1
1333 1773
.....#....#......#.#..#...#.....#.#.#.#....#...###.#..#.....##....####..#......#.......######.#........#..#......#...###.#.#..#.....#.#........##..#..#.#..#.###.#.#...#..#.##..#...#....#..#.##..#......#.######............#.#...#......#......#..#.#.#.#...#...#..##........#.###.....

output:

17464052497724

result:

ok single line: '17464052497724'

Test #6:

score: 0
Accepted
time: 60ms
memory: 329452kb

input:

3000 3000 1
2755 225
##..#.##.....####..#...###.#.##.#.##.#......###.#####..#..####....#.#.####..##..##.#...#...##..#.#.##..#....##.#...#.....##.#...##.##.##..##..#######.####.####......##.##.#....#..#.....#..##.#.#...#.####..##.#..#...###..###.#.#...##.#.....###.####......##...#...#....#.#...#.#.#....

output:

255915

result:

ok single line: '255915'

Test #7:

score: 0
Accepted
time: 47ms
memory: 318396kb

input:

3000 2900 1
878 738
#.##.##..##.#.#.###.#...###.####.#.###.####.##.#.#####.#.####..#.#.###.###..####.####...###..####.########..##..#####.#....#####.#.#########..#.###.##.##.#####.#####.#.##..###..##.#####.#.############..##.###.##.##..########.#.###..###...######.####...#######.###.###..####.######...

output:

1

result:

ok single line: '1'

Test #8:

score: 0
Accepted
time: 439ms
memory: 318672kb

input:

2900 3000 10
2883 1758
2883 1759
2883 1760
2883 1761
2883 1762
2884 1762
2884 1763
2883 1763
2882 1763
2882 1764
........................................................#............................#........................................................................................................

output:

49803365625286

result:

ok single line: '49803365625286'

Test #9:

score: 0
Accepted
time: 589ms
memory: 329500kb

input:

3000 3000 10
2015 1932
2015 1931
2015 1930
2015 1929
2016 1929
2017 1929
2018 1929
2018 1928
2018 1927
2017 1927
#...#...#..#.........#.......#####....#...###..#..###..###....##.....#..#..#...#.....##...##.#..#..##.###.........##.....#....#..##.##.#.#.##.#.#.#.....#....##.##.#..##....#....#...#.#......

output:

22509095749285

result:

ok single line: '22509095749285'

Test #10:

score: 0
Accepted
time: 55ms
memory: 318432kb

input:

3000 2900 10
326 1781
325 1781
325 1782
325 1783
325 1784
324 1784
324 1783
323 1783
323 1782
324 1782
##.#....#.###.######..#.#.....##.#.##..####.####.##..#..#.###.#####....##.#.##.#..###..##.###.##.#####.###..##.#..##..##.#..##.#.#.##...##..#.##.##........#..#..###.##.###.####.#..########.##.....#...

output:

40571

result:

ok single line: '40571'

Test #11:

score: 0
Accepted
time: 43ms
memory: 318492kb

input:

2900 3000 10
2447 135
2447 136
2447 137
2447 138
2447 139
2447 140
2448 140
2448 139
2449 139
2449 138
.#.##.##..#.###########.#####.###....#####.########..##..#.####.##.##.####.####..#.#####.##.#.#.###.##.#.##.####..##.#.####..###..###...##...##.#####.#####.#...#####.####..##.##.#.#..#..####.##..##...

output:

2705

result:

ok single line: '2705'

Test #12:

score: 0
Accepted
time: 477ms
memory: 329632kb

input:

3000 3000 100
2573 1917
2572 1917
2572 1916
2573 1916
2574 1916
2574 1915
2573 1915
2572 1915
2571 1915
2571 1914
2570 1914
2570 1915
2569 1915
2569 1916
2568 1916
2568 1917
2569 1917
2570 1917
2570 1916
2571 1916
2571 1917
2571 1918
2570 1918
2569 1918
2569 1919
2570 1919
2571 1919
2571 1920
2572 1...

output:

41693682087973

result:

ok single line: '41693682087973'

Test #13:

score: 0
Accepted
time: 575ms
memory: 318612kb

input:

3000 2900 56
923 228
922 228
921 228
920 228
919 228
919 227
919 226
920 226
920 227
921 227
922 227
922 226
922 225
921 225
921 224
921 223
920 223
920 222
920 221
921 221
921 222
922 222
923 222
924 222
925 222
925 223
924 223
923 223
922 223
922 224
923 224
923 225
924 225
924 224
925 224
925 225...

output:

36262204093655

result:

ok single line: '36262204093655'

Test #14:

score: -100
Wrong Answer
time: 51ms
memory: 318480kb

input:

2900 3000 100
2357 817
2357 818
2357 819
2358 819
2359 819
2360 819
2360 820
2361 820
2361 819
2361 818
2360 818
2360 817
2361 817
2362 817
2362 818
2363 818
2364 818
2365 818
2365 817
2364 817
2363 817
2363 816
2363 815
2362 815
2362 814
2361 814
2361 815
2360 815
2360 816
2359 816
2358 816
2358 81...

output:

9014912

result:

wrong answer 1st lines differ - expected: '4878972', found: '9014912'