Cod sursa(job #457898)

Utilizator write2cristi1989Dumitru Andrei write2cristi1989 Data 21 mai 2010 22:50:13
Problema Distante Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 8.72 kb
#include<stdlib.h>
#include<stdio.h>

int** alocMat(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}




int** alocMat1(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}



int** alocMat2(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}





int** alocMat3(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}


int min3(int x1, int x2, int x3){
    if (x1<=x2 && x1<=x3)
        return x1;
    if (x2<=x3)
        return x2;
    return x3;
}

int min4(int x1, int x2, int x3, int x4){
    if (x1<=x2 && x1<=x3 && x1<=x4)
        return x1;
    if (x2<=x3 && x2<=x4)
        return x2;
    if (x3<=x4)
        return x3;
    return x4;
}

void printf_max_nrmax(int **a, int n, FILE *f){   
    int max=0, nrmax=0,i,j;
    for (i=0;i<n;i++)
        for (j=0;j<n;j++)
            if (max<a[i][j]){
                max=a[i][j];
                nrmax=1;
            }
            else if (max==a[i][j])
                nrmax++;
    fprintf(f,"%i %i\n",max,nrmax);       
}

int main(){
        
    int n, **mat, **mat_aux_patrat, **mat_aux_romb, i, j;
    char c;
    FILE *f,*f2;
    f=fopen("figuri2.in","rt");
    f2=fopen("figuri2.out","wt");
    fscanf(f,"%i",&n);
    
    mat=alocMat(n);
    mat_aux_patrat=alocMat(n);
    mat_aux_romb=alocMat(n);
    
    for (i=0;i<n;i++)
        for (j=0;j<n;j++){
            fscanf(f,"%c",&c);
            if (c=='0')
                mat[i][j]=0;
            else if (c=='1')
                mat[i][j]=1;
            else
                j--;
        }          
    for (i=0;i<n;i++){
        mat_aux_patrat[0][i]=mat[0][i];
        mat_aux_patrat[i][0]=mat[i][0];
        mat_aux_romb[i][0]=mat[i][0];
        mat_aux_romb[i][n-1]=mat[i][n-1];
        mat_aux_romb[0][i]=mat[0][i];
        mat_aux_romb[1][i]=mat[1][i];
    }
    
    for (i=1;i<n;i++)
        for (j=1;j<n;j++)
            if (mat[i][j])
                mat_aux_patrat[i][j]=1+min3(mat_aux_patrat[i-1][j-1],mat_aux_patrat[i][j-1],mat_aux_patrat[i-1][j]);
            else
                mat_aux_patrat[i][j]=0;
    
    for (i=2;i<n;i++)
        for (j=1;j<n-1;j++)
            if (mat[i][j])
                mat_aux_romb[i][j]=1+min4(mat_aux_romb[i-1][j-1],mat_aux_romb[i-1][j],mat_aux_romb[i-1][j+1],mat_aux_romb[i-2][j]);
            else
                mat_aux_romb[i][j]=0;
                
    printf_max_nrmax(mat_aux_patrat,n,f2);
    printf_max_nrmax(mat_aux_romb,n,f2);
    
    return 0;
}

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

int** alocMat(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}




int** alocMat1(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}



int** alocMat2(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}





int** alocMat3(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}


int min3(int x1, int x2, int x3){
    if (x1<=x2 && x1<=x3)
        return x1;
    if (x2<=x3)
        return x2;
    return x3;
}

int min4(int x1, int x2, int x3, int x4){
    if (x1<=x2 && x1<=x3 && x1<=x4)
        return x1;
    if (x2<=x3 && x2<=x4)
        return x2;
    if (x3<=x4)
        return x3;
    return x4;
}

void printf_max_nrmax(int **a, int n, FILE *f){   
    int max=0, nrmax=0,i,j;
    for (i=0;i<n;i++)
        for (j=0;j<n;j++)
            if (max<a[i][j]){
                max=a[i][j];
                nrmax=1;
            }
            else if (max==a[i][j])
                nrmax++;
    fprintf(f,"%i %i\n",max,nrmax);       
}

int main(){
        
    int n, **mat, **mat_aux_patrat, **mat_aux_romb, i, j;
    char c;
    FILE *f,*f2;
    f=fopen("figuri2.in","rt");
    f2=fopen("figuri2.out","wt");
    fscanf(f,"%i",&n);
    
    mat=alocMat(n);
    mat_aux_patrat=alocMat(n);
    mat_aux_romb=alocMat(n);
    
    for (i=0;i<n;i++)
        for (j=0;j<n;j++){
            fscanf(f,"%c",&c);
            if (c=='0')
                mat[i][j]=0;
            else if (c=='1')
                mat[i][j]=1;
            else
                j--;
        }          
    for (i=0;i<n;i++){
        mat_aux_patrat[0][i]=mat[0][i];
        mat_aux_patrat[i][0]=mat[i][0];
        mat_aux_romb[i][0]=mat[i][0];
        mat_aux_romb[i][n-1]=mat[i][n-1];
        mat_aux_romb[0][i]=mat[0][i];
        mat_aux_romb[1][i]=mat[1][i];
    }
    
    for (i=1;i<n;i++)
        for (j=1;j<n;j++)
            if (mat[i][j])
                mat_aux_patrat[i][j]=1+min3(mat_aux_patrat[i-1][j-1],mat_aux_patrat[i][j-1],mat_aux_patrat[i-1][j]);
            else
                mat_aux_patrat[i][j]=0;
    
    for (i=2;i<n;i++)
        for (j=1;j<n-1;j++)
            if (mat[i][j])
                mat_aux_romb[i][j]=1+min4(mat_aux_romb[i-1][j-1],mat_aux_romb[i-1][j],mat_aux_romb[i-1][j+1],mat_aux_romb[i-2][j]);
            else
                mat_aux_romb[i][j]=0;
                
    printf_max_nrmax(mat_aux_patrat,n,f2);
    printf_max_nrmax(mat_aux_romb,n,f2);
    
    return 0;
}

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

int** alocMat(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}




int** alocMat1(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}



int** alocMat2(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}





int** alocMat3(int n){
    int i=0;
    int **mat=(int**)malloc(n * sizeof(int*));
    for (i=0;i<n;i++)  
        (mat)[i] = (int*)malloc(n*sizeof(int));
    return mat;
}


int min3(int x1, int x2, int x3){
    if (x1<=x2 && x1<=x3)
        return x1;
    if (x2<=x3)
        return x2;
    return x3;
}

int min4(int x1, int x2, int x3, int x4){
    if (x1<=x2 && x1<=x3 && x1<=x4)
        return x1;
    if (x2<=x3 && x2<=x4)
        return x2;
    if (x3<=x4)
        return x3;
    return x4;
}

void printf_max_nrmax(int **a, int n, FILE *f){   
    int max=0, nrmax=0,i,j;
    for (i=0;i<n;i++)
        for (j=0;j<n;j++)
            if (max<a[i][j]){
                max=a[i][j];
                nrmax=1;
            }
            else if (max==a[i][j])
                nrmax++;
    fprintf(f,"%i %i\n",max,nrmax);       
}

int main(){
        
    int n, **mat, **mat_aux_patrat, **mat_aux_romb, i, j;
    char c;
    FILE *f,*f2;
    f=fopen("figuri2.in","rt");
    f2=fopen("figuri2.out","wt");
    fscanf(f,"%i",&n);
    
    mat=alocMat(n);
    mat_aux_patrat=alocMat(n);
    mat_aux_romb=alocMat(n);
    
    for (i=0;i<n;i++)
        for (j=0;j<n;j++){
            fscanf(f,"%c",&c);
            if (c=='0')
                mat[i][j]=0;
            else if (c=='1')
                mat[i][j]=1;
            else
                j--;
        }          
    for (i=0;i<n;i++){
        mat_aux_patrat[0][i]=mat[0][i];
        mat_aux_patrat[i][0]=mat[i][0];
        mat_aux_romb[i][0]=mat[i][0];
        mat_aux_romb[i][n-1]=mat[i][n-1];
        mat_aux_romb[0][i]=mat[0][i];
        mat_aux_romb[1][i]=mat[1][i];
    }
    
    for (i=1;i<n;i++)
        for (j=1;j<n;j++)
            if (mat[i][j])
                mat_aux_patrat[i][j]=1+min3(mat_aux_patrat[i-1][j-1],mat_aux_patrat[i][j-1],mat_aux_patrat[i-1][j]);
            else
                mat_aux_patrat[i][j]=0;
    
    for (i=2;i<n;i++)
        for (j=1;j<n-1;j++)
            if (mat[i][j])
                mat_aux_romb[i][j]=1+min4(mat_aux_romb[i-1][j-1],mat_aux_romb[i-1][j],mat_aux_romb[i-1][j+1],mat_aux_romb[i-2][j]);
            else
                mat_aux_romb[i][j]=0;
                
    printf_max_nrmax(mat_aux_patrat,n,f2);
    printf_max_nrmax(mat_aux_romb,n,f2);
    
    return 0;
}