PI-ITBA / 2024_01

8 stars 0 forks source link

Parcial 2Q2022 - Ejer2 #364

Closed pedrogonzaleznunez closed 3 months ago

pedrogonzaleznunez commented 3 months ago

Buenas, queria saber si estaba bien resuelto el ejercicio, mas que nada me importa la parte de direcciones. Muchas gracias.

#include <stdio.h>

#define FOUND 1
#define NOT_FOUND 0
#define FUERA_DE_RANGO 0
#define N 6
#define CANT_DIRECCIONES 8

#define INRANGE(fils,cols) ((fils >= 0) && (fils < N) && (cols >= 0) && (cols < N))

int palEnMat(char matriz[][N], int fils, int cols, char * s){

    int dirs[8][2] = {
        {0,1}, // i,j
        {-1,1},
        {-1,0},
        {-1,-1},
        {0,-1},
        {1,-1},
        {1,0},
        {1,1},
    };

    if(!(INRANGE(fils, cols)) || matriz[fils][cols] != *s)
        return FUERA_DE_RANGO;

    if(*s == 0)
        return FOUND;

    //itero todas las direcciones
    for(int i = 0; i < CANT_DIRECCIONES; i++){
        if(direcciones(matriz,dirs[i],s+1,cols,fils))
            return FOUND;
    }
    return NOT_FOUND;
}

int direcciones(char matriz[][N], int dirs[], char * s, int x, int y){
    int posx = x + dirs[0];
    int posy = y + dirs[1];

    int k = 0;

    while(INRANGE(posx,posy) && s[k] != '\0'){
        if(matriz[posx][posy] != s[k])
            return NOT_FOUND;

        k++;
    }

    return s[k] == '\0';

}