CSES - Datatähti 2020 alku - Results
Submission details
Task:Ruudukko
Sender:Aapeli
Submission time:2019-10-11 00:06:02 +0300
Language:C++ (C++17)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED100
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.01 sdetails
#5ACCEPTED0.04 sdetails
#6ACCEPTED0.04 sdetails

Compiler report

input/code.cpp: In function 'int main()':
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=(int*)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=(int*)malloc(number*sizeof(int));
    int** sudoku=(int**)malloc(number*sizeof(int*));
    for (int i=0;i<number;i++)
        sudoku[i]=(int*)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);
}

Test details

Test 1

Verdict: ACCEPTED

input
1

correct output

user output

Test 2

Verdict: ACCEPTED

input
2

correct output
1 2 
2 1 

user output
1 2 
2 1 

Test 3

Verdict: ACCEPTED

input
5

correct output
1 2 3 4 5 
2 1 4 3 6 
3 4 1 2 7 
4 3 2 1 8 
5 6 7 8 1 

user output
1 2 3 4 5 
2 1 4 3 6 
3 4 1 2 7 
4 3 2 1 8 
5 6 7 8 1 

Test 4

Verdict: ACCEPTED

input
42

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...
Truncated

Test 5

Verdict: ACCEPTED

input
99

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...
Truncated

Test 6

Verdict: ACCEPTED

input
100

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...
Truncated