חזור לתיקייה: מועד א

examA_q4

חיפוש בתיקיה
Ctrl K
לחץ כאן או הקש Ctrl K כדי לחפש בכל התיקיה

    examA_q4

    02340114

    מבוא למדמח מ

    רשימות הרצאה לקריאה בשבת

    examA_q4

    הופק מתוך תקיית התואר של שקד

    uni-folder.vercel.app

    exama-q4.c

    הערה (קובץ קוד)

    קובץ זה הוא קובץ קוד מסוג C.

    #include <stdio.h>
    #include <stdlib.h>
    
    #define N 5
    
    #define DONT_KNOW "I_dont_know"
    
    void printIDontKnow()
    {
        printf("%s", DONT_KNOW);
        exit(0);
    }
    
    int examA_q4(int mat[N][N]);
    
    
    int main(void)
    {
        // uncomment next line if you don't know the answer
        // printIDontKnow();
    
        int mat[N][N];
        for (int i = 0; i < N; ++i)
        {
            for (int j = 0; j < N; ++j)
            {
                if (scanf("%d", &mat[i][j]) != 1)
                {
                    return 1;
                }
            }
        }
        printf("%d", examA_q4(mat));
        return 0;
    }
    
    // this function returns the minimum of two values(excluding (-1)s)
    int min1(int a, int b)
    {
        if (a == -1)
        {
            return b;
        }
        if (b == -1)
        {
            return a;
        }
    
        return b < a ? b : a;
    }
    
    // this function returns the min of 4 values(excluding (-1)s)
    int min4(int a, int b, int c, int d)
    {
        return min1(a, min1(b, min1(c, d)));
    }
    
    /*
     * This function is a backtracking function that counts the time in the maze.
     *
     * input:
     *      1. int mat[N][N]: the maze represented in a matrix.
     *      2. int row: the current row.
     *      3. int column: the current column.
     * output(int):
     *      The shortest time throw the maze.
     */
    int countRooms(int mat[N][N], int row, int column)
    {
        if (row == (N - 1) && column == (N - 1))
        {
            return mat[row][column];
        }
    
        int originalVal = mat[row][column];
        mat[row][column] = -2;
    
        // check right room
        int tR = -1;
        if (column + 1 < N && mat[row][column + 1] > 0)
        {
            tR = countRooms(mat, row, column + 1);
        }
    
        // check left room
        int tL = -1;
        if (column - 1 >= 0 && mat[row][column - 1] > 0)
        {
            tL = countRooms(mat, row, column - 1);
        }
    
        // check down room
        int tD = -1;
        if (row + 1 < N && mat[row + 1][column] > 0)
        {
            tD = countRooms(mat, row + 1, column);
        }
    
        // check up room
        int tU = -1;
        if (row - 1 >= 0 && mat[row - 1][column] > 0)
        {
            tU = countRooms(mat, row - 1, column);
        }
    
        // compare
        mat[row][column] = originalVal;
        int theSmallest = min4(tR, tL, tD, tU);
        return (theSmallest < 0) ? -1 : (theSmallest + mat[row][column]);
    }
    
    /*
     * This function finds the shortest path in the maze.
     *
     * input:
     *      1. int mat[N][N]: the maze represented in the 2D matrix.
     * output(int):
     *      the time of the fastest way throw the maze.(from 0,0 to N-1,N-1)
     */
    int examA_q4(int mat[N][N])
    {
        // we always pass the first room, so if it's not empty to all the routes
        // the time in it needs to be added.
        return countRooms(mat, 0, 0);
    }