CSES - Datatähti 2020 alku - Results
Submission details
Task:Ruudukko
Sender:Aapeli
Submission time:2019-10-11 00:03:19 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int pieninmitaeiole(int*, int)':
input/code.cpp:50:22: error: invalid conversion from 'void*' to 'int*' [-fpermissive]
     int* list2=malloc(length*sizeof(int));
                ~~~~~~^~~~~~~~~~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:76:24: error: invalid conversion from 'void*' to 'int*' [-fpermissive]
     int* thisrow=malloc(number*sizeof(int));
                  ~~~~~~^~~~~~~~~~~~~~~~~~~~
input/code.cpp:77:24: error: invalid conversion from 'void*' to 'int**' [-fpermissive]
     int** sudoku=malloc(number*sizeof(int*));
                  ~~~~~~^~~~~~~~~~~~~~~~~~~~~
input/code.cpp:79:25: error: invalid conversion from 'void*' to 'int*' [-fpermissive]
         sudoku[i]=malloc(number*sizeof(int));
                   ~~~~~~^~~~~~~~~~~~~~~~~~~~
input/code.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&number);
     ~~~~~^~~...

Code

//
//  main.c
//  datatahti
//
//  Created by aapo haavisto on 10/9/19.
//  Copyright © 2019 Aapo Haavisto. All rights reserved.
//

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

int onkosarakkeessa(int** sudoku,int sarake, int rivi, int etsittava){
    int loytyi=0;
    for(int i=0;i<rivi;i++){
        if(etsittava==sudoku[i][sarake])
            loytyi=1;
    }
    return loytyi;
}

int cmpfunc (const void * a, const void * b) //what is it returning?
{
    // qsort() passes in `void*` types because it can't know the actual types being sorted
    // convert those pointers to pointers to int and deref them to get the actual int values
    
    int val1 = *(int*)a;
    int val2 = *(int*)b;
    
    // qsort() expects the comparison function to return:
    //
    //    a negative result if val1 < val2
    //    0 if val1 == val2
    //    a positive result if val1 > val2
    
    return ( val1 - val2 );
}

int onkolistassa(int* lista,int maara, int etsittava){
    for (int i=0; i<maara; i++) {
        if (etsittava==lista[i])
            return 1;
    }
    return 0;
}

int pieninmitaeiole(int* list, int length){
    if (length==0)
        return 1;
    int* list2=malloc(length*sizeof(int));
    memcpy(list2, list, length*sizeof(int));
    qsort(list2, length, sizeof(int), cmpfunc);
    //for(int x=0;x<length;x++)
      //  printf("%i ",list2[x]);
    int pienin=1;
    int i=1;
    while (i){
        for (int a=0;a<length;a++){
            if (list2[a]==pienin)
                pienin++;
            else
                i=0;
        }
        
    }
    free(list2);
    return pienin;
}


int main(void){
    
    int number;
    scanf("%i",&number);
    
    int* thisrow=malloc(number*sizeof(int));
    int** sudoku=malloc(number*sizeof(int*));
    for (int i=0;i<number;i++)
        sudoku[i]=malloc(number*sizeof(int));
    
    
    for (int rivi=0; rivi<number; rivi++) {
        for(int sarake=0;sarake<number; sarake++){
            int x=1;
            int katkaisu=0;
            while(1){
                if (!onkolistassa(thisrow, sarake, x)){
                    if (!onkosarakkeessa(sudoku, sarake, rivi, x)){
                        printf("%i ",x);
                        thisrow[sarake]=x;
                        sudoku[rivi][sarake]=x;
                        katkaisu=1;
                        break;
                    }
                }
                if (katkaisu)
                    break;
                x++;
            }
        }
        
            
      printf("\n");
    }
    
    for (int i=0;i<number;i++)
        free(sudoku[i]);
    free(sudoku);
}