Q:

Solving Maze And Printing The Correct Path in C

I am trying to make a code to solve the rat in a maze problem. '1' is for walls and '0' is for free path. All the paths taken are stored in a 2D array path[200][2]. Also print the path solution by replacing the path character with '*'. Also I am replacing the path already taken by '1' so that we don't follow that path again.
My Code:
#include<stdio.h>

int main()
{
char maze[10][10],mazeO[10][10];
int path[200][2],count=-1,N,M,cx,cy,sx,sy,ex,ey;

printf("\nEnter the maze limit(Max=10): (M & N)");
    scanf("%d%d",&M,&N);

    printf("\nEnter the maze:\n");

    for(int i=0;i<M;++i) {
        for(int j=0;j<N;++j)
        {
                scanf(" %c",&maze[i][j]);
                mazeO[i][j]=maze[i][j];
        }
    }
    printf("\nEnter the source cordinates(sx,sy):");
    scanf("%d%d",&sx,&sy);  

    printf("\nEnter the target cordinates(ex,ey):");
    scanf("%d%d",&ex,&ey);

    cx=sx;cy=sy;
    while(cx!=ex || cy!=ey)
    {
        if(maze[cx][cy+1]=='0'&&(cy+1)<N)
        {   
            maze[cx][cy] = '1';
            count++;
            path[count][0]=cx;
            path[count][1]=cy;

            cy=cy+1;
         }
        else if(maze[cx-1][cy]=='0'&&(cx-1)>0)
        {   
            maze[cx][cy] = '1';
            count++;
            path[count][0]=cx;
            path[count][1]=cy;

            cx=cx-1;
         }
        else if(maze[cx][cy-1]=='0'&&(cy-1)>0)
        {   
            maze[cx][cy] = '1';
            count++;
            path[count][0]=cx;
            path[count][1]=cy;

            cy=cy-1;
         }
        else if(maze[cx+1][cy]=='0'&&(cx+1)<M)
        {
            maze[cx][cy] = '1';
            count++;
            path[count][0]=cx;
            path[count][1]=cy;

            cx=cx+1;
         }
         else
         {
            cx=path[count][0];
            cy=path[count][1];
            count--;    
         }
    }

     for(int i=0;i<=count;i++)
        mazeO[path[i][0]][path[i][1]]='*';

     mazeO[ex][ey]='*';

    for(int i=0;i<M;++i)
    {

        for(int j=0;j<N;++j)
        {
            printf(" %c",mazeO[i][j]);
        }
        printf("\n");
    }
}

When I am trying to run the above code with the following inputs:
M = 6    N = 7
sx = 1   sy = 0
ex = 4   ey = 6

Given input Maze:
1 1 1 1 1 1 1
0 0 0 0 0 1 1
1 1 0 1 1 1 1 
0 0 0 1 1 1 1 
1 1 0 0 0 0 0 
1 1 1 1 1 1 1

I am getting a segmentation error in Ubuntu. I am unable to find where I did wrong. Why am I getting this segmentation fault?
EDIT:
I have edited the code according to suggestion in comments:
#include<stdio.h>

int main()
{
char maze[10][10],mazeO[10][10];
int path[200][2],count=-1,N,M,cx,cy,sx,sy,ex,ey;

printf("\nEnter the maze limit(Max=10): (M & N)");
    scanf("%d%d",&M,&N);

    printf("\nEnter the maze:\n");

    for(int i=0;i<M;++i) {
        for(int j=0;j<N;++j)
        {
                scanf(" %c",&maze[i][j]);
                mazeO[i][j]=maze[i][j];
        }
    }
    printf("\nEnter the source cordinates(sx,sy):");
    scanf("%d%d",&sx,&sy);  

    printf("\nEnter the target cordinates(ex,ey):");
    scanf("%d%d",&ex,&ey);

    cx=sx;cy=sy;
    while(cx!=ex || cy!=ey)
    {
        if((cy+1)<N && maze[cx][cy+1]=='0')
        {   
            maze[cx][cy] = '1';
            count++;
            path[count][0]=cx;
            path[count][1]=cy;

            cy=cy+1; 
        }
        else if((cx-1)>0 && maze[cx-1][cy]=='0')
        {   
            maze[cx][cy] = '1';
            count++;
            path[count][0]=cx;
            path[count][1]=cy;

            cx=cx-1;
         }
        else if((cy-1)>0 && maze[cx][cy-1]=='0')
        {   
            maze[cx][cy] = '1';
            count++;
            path[count][0]=cx;
            path[count][1]=cy;

            cy=cy-1;
         }
        else if((cx+1)<M && maze[cx+1][cy]=='0')
        {
            maze[cx][cy] = '1';
            count++;
            path[count][0]=cx;
            path[count][1]=cy;

            cx=cx+1;
         }
         else
         {
            cx=path[count][0];
            cy=path[count][1];

            count--;    
         }
    }

     for(int i=0;i<=count;i++)
        mazeO[path[i][0]][path[i][1]]='*';

     mazeO[ex][ey]='*';

    for(int i=0;i<M;++i)
    {

        for(int j=0;j<N;++j)
        {
            printf(" %c",mazeO[i][j]);
        }
        printf("\n");
    }
}

Still the segmentation fault is taking place. I tried it on Windows OS and in windows it isn't giving any error but it isn't printing the correct path maze.
FINAL WORKING SOLUTION:
All I needed was maze[cx][cy] = '1' inside the else loop. Also changed the if loop condition (cy-1)>0 to (cy-1)>=0 and (cx-1)>0 to (cx-1)>=0. Also included the count-- within the if(count>=0) loop.

A:

As @Klas-Lindbäck already mentioned, you do not handle count becoming negative. It will then start indexing path[] outside of the array and bad things happen.
The reason that the path is not found is because (5,6) is a wall. Maybe you meant (4,6) ?
Also, the left column and upper row are not allowed to be access because you check (cy-1)>0 instead of >= 0.
Apart from that your backtrack step needs to set maze[cx][cy] to 1 to prevent from entering the same path over and over again.

