QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#699093#7781. Sheep Eat WolvesimtxTL 928ms6368kbC++173.0kb2024-11-02 01:06:412024-11-02 01:06:41

Judging History

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

  • [2024-11-02 01:06:41]
  • 评测
  • 测评结果:TL
  • 用时:928ms
  • 内存:6368kb
  • [2024-11-02 01:06:41]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr int N = 105;
constexpr int inf = 1e9;

int f[N][N][2];

void solve() {
    int x, y, p, q;
    cin >> x >> y >> p >> q;

    for (int i = 0; i <= x; i++) {
        for (int j = 0; j <= y; j++) {
            f[i][j][0] = f[i][j][1] = -1;
        }
    }

    //f[i][j][0/1]表示i只羊 j只狼 现在在岸的某一边的
    f[x][y][0] = 0;
    queue<array<int, 3>> que;
    que.push({x, y, 0});

    while (!que.empty()) {
        auto [nx, ny, k] = que.front();
        que.pop();

        // cerr << nx << ' ' << ny << ' ' << k << '\n';

        //还有nx个羊 ny只狼 0/1代表左右侧

        if (k == 0) {
            //枚举运走i只羊 j只狼
            for (int i = 0; i <= p; i++) {
                for (int j = 0; i + j <= p; j++) {
                    if (i != 0 && j > i + q) {
                        continue;
                    }
                    if (i > nx || j > ny) {
                        continue;
                    }

                    int nnx = nx - i, nny = ny - j; //留下nnx只羊 nny只狼
                    if (nnx != 0 && nny > nnx + q) {
                        continue;
                    }

                    if (f[nnx][nny][1] == -1) {
                        f[nnx][nny][1] = f[nx][ny][0] + 1;
                        que.push({nnx, nny, 1});
                    }
                }
            }
        } else {
            for (int i = 0; i <= p; i++) {
                for (int j = 0; i + j <= p; j++) {
                    if (i != 0 && j > i + q) {
                        continue;
                    }

                    if (i > x - nx || j > y - ny) {
                        continue;
                    }


                    int nnx = x - nx - i, nny = y - ny - j;
                    // if (nx == 2 && ny == 2) {
                    //     cerr << i << ' ' << j << ' ' << f[nx + i][ny + j][0] << '\n';
                    // }
                    if (nnx != 0 && nny > nnx + q) {
                        continue;
                    }


                    // if (f[nx + i][ny + j][0] == -1) {
                        f[nx + i][ny + j][0] = f[nx][ny][1] + 1;
                        que.push({nx + i, ny + j, 0});
                    // }
                }
            }

            
            int nnx = x - nx, nny = y - ny;
            if (nnx == 0 || nny <= nnx + q) {
                f[nx][ny][0] = f[nx][ny][1] + 1;
                que.push({nx, ny, 0});
            }
        }

    }

    constexpr int inf = 1e9;
    int ans = inf;
    for (int j = 0; j <= y; j++) {
        if (f[0][j][0] != -1) {
            ans = min(ans, f[0][j][1]);
        }
    }

    if (ans == inf) {
        ans = -1;
    }

    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int T = 1;
    // cin >> T;
    
    while (T--) {
        solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3880kb

input:

4 4 3 1

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 5 2 0

output:

5

result:

ok 1 number(s): "5"

Test #3:

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

input:

2 5 1 1

output:

-1

result:

ok 1 number(s): "-1"

Test #4:

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

input:

1 1 1 0

output:

1

result:

ok 1 number(s): "1"

Test #5:

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

input:

3 3 1 1

output:

7

result:

ok 1 number(s): "7"

Test #6:

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

input:

3 3 2 1

output:

3

result:

ok 1 number(s): "3"

Test #7:

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

input:

10 9 1 10

output:

19

result:

ok 1 number(s): "19"

Test #8:

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

input:

15 20 2 5

output:

27

result:

ok 1 number(s): "27"

Test #9:

score: 0
Accepted
time: 2ms
memory: 3592kb

input:

18 40 16 7

output:

5

result:

ok 1 number(s): "5"

Test #10:

score: 0
Accepted
time: 2ms
memory: 3704kb

input:

60 60 8 1

output:

27

result:

ok 1 number(s): "27"

Test #11:

score: 0
Accepted
time: 3ms
memory: 3720kb

input:

60 60 8 4

output:

27

result:

ok 1 number(s): "27"

Test #12:

score: 0
Accepted
time: 5ms
memory: 3968kb

input:

60 60 8 8

output:

25

result:

ok 1 number(s): "25"

Test #13:

score: 0
Accepted
time: 6ms
memory: 3720kb

input:

60 60 16 1

output:

13

result:

ok 1 number(s): "13"

Test #14:

score: 0
Accepted
time: 33ms
memory: 3904kb

input:

60 60 16 8

output:

11

result:

ok 1 number(s): "11"

Test #15:

score: 0
Accepted
time: 64ms
memory: 4232kb

input:

60 60 16 16

output:

11

result:

ok 1 number(s): "11"

Test #16:

score: 0
Accepted
time: 87ms
memory: 4176kb

input:

60 60 16 24

output:

9

result:

ok 1 number(s): "9"

Test #17:

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

input:

75 15 1 1

output:

175

result:

ok 1 number(s): "175"

Test #18:

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

input:

50 100 1 0

output:

-1

result:

ok 1 number(s): "-1"

Test #19:

score: 0
Accepted
time: 19ms
memory: 3832kb

input:

100 100 10 10

output:

35

result:

ok 1 number(s): "35"

Test #20:

score: 0
Accepted
time: 3ms
memory: 3688kb

input:

100 100 10 1

output:

37

result:

ok 1 number(s): "37"

Test #21:

score: 0
Accepted
time: 28ms
memory: 3792kb

input:

100 100 10 20

output:

33

result:

ok 1 number(s): "33"

Test #22:

score: 0
Accepted
time: 44ms
memory: 3956kb

input:

100 100 10 30

output:

31

result:

ok 1 number(s): "31"

Test #23:

score: 0
Accepted
time: 79ms
memory: 4328kb

input:

100 100 10 80

output:

21

result:

ok 1 number(s): "21"

Test #24:

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

input:

100 100 1 100

output:

199

result:

ok 1 number(s): "199"

Test #25:

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

input:

100 100 5 0

output:

95

result:

ok 1 number(s): "95"

Test #26:

score: 0
Accepted
time: 57ms
memory: 4008kb

input:

100 100 25 3

output:

13

result:

ok 1 number(s): "13"

Test #27:

score: 0
Accepted
time: 141ms
memory: 4256kb

input:

100 100 30 4

output:

11

result:

ok 1 number(s): "11"

Test #28:

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

input:

95 100 3 3

output:

125

result:

ok 1 number(s): "125"

Test #29:

score: 0
Accepted
time: 928ms
memory: 6368kb

input:

98 99 50 6

output:

5

result:

ok 1 number(s): "5"

Test #30:

score: 0
Accepted
time: 469ms
memory: 4980kb

input:

100 100 45 4

output:

7

result:

ok 1 number(s): "7"

Test #31:

score: -100
Time Limit Exceeded

input:

100 100 40 39

output:


result: