CSES - Aalto Competitive Programming 2024 - wk1 - Mon - Results
Submission details
Task:Stone game
Sender:ZDHKLV
Submission time:2024-09-02 17:32:23 +0300
Language:C++11
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void play(int, int)':
input/code.cpp:51:25: error: invalid conversion from 'void*' to 'int***' [-fpermissive]
   51 |     int ***seen = malloc(sizeof (int **) * (A+B+1));
      |                   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                         |
      |                         void*
input/code.cpp:53:25: error: invalid conversion from 'void*' to 'int**' [-fpermissive]
   53 |         seen[i] = malloc(sizeof (int *) * (A+B+1));
      |                   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
      |                         |
      |                         void*
input/code.cpp:55:32: error: invalid conversion from 'void*' to 'int*' [-fpermissive]
   55 |             seen[i][j] = malloc(sizeof (int) * 2);
      |                          ~~~~~~^~~~~~~~~~~~~~~~~~
      |                                |
      |                                void*
input/code.cpp: In function 'int main(int, char**)':
input/code.cpp:71:10: warning: ignoring return v...

Code

#include <stdio.h>
#include <stdlib.h>

int UOLEVI = 0;
int MAIJA = 1;

int play_rec(int a, int b, int player, int ***seen) {

    if (seen[a][b][player] != -1) {
        return -1;
    }

    for (int x = 1; x <= a; x++) {
        int rec = play_rec(a - x, b, (player+1)%2, seen);
        if (rec == player) {
            seen[a][b][player] = player;
            return player;
        }
    }

    for (int y = 1; y <= b; y++) {
        int rec = play_rec(a, b - y, (player+1)%2, seen);
        if (rec == player) {
            seen[a][b][player] = player;
            return player;
        }
    }

    for (int x = 1; x <= a; x++) {
        int rec = play_rec(a - x, b + x, (player+1)%2, seen);
        if (rec == player) {
            seen[a][b][player] = player;
            return player;
        }
    }

    for (int y = 1; y <= b; y++) {
        int rec = play_rec(a + y, b - y, (player+1)%2, seen);
        if (rec == player) {
            seen[a][b][player] = player;
            return player;
        }
    }

    return (player+1)%2;

}

void play(int A, int B) {

    int ***seen = malloc(sizeof (int **) * (A+B+1));
    for (int i = 0; i <= A+B; i++) {
        seen[i] = malloc(sizeof (int *) * (A+B+1));
        for (int j = 0; j <= A+B; j++) {
            seen[i][j] = malloc(sizeof (int) * 2);
            seen[i][j][0] = -1;
            seen[i][j][1] = -1;
        }
    }

    int result = play_rec(A, B, UOLEVI, seen);

    if (result == UOLEVI) printf("Uolevi");
    else if (result == MAIJA) printf("Maija");
    else printf("Draw");

}

int main(int argc, char **argv) {
    int A, B;
    scanf("%d %d", &A, &B);
    play(A, B);
    return EXIT_SUCCESS;
}