C-language algorithm for mice to walk the maze

Source: Internet
Author: User

1.Algorithm Gossip: The mouse Goes Astray (a)
Description: The mouse maze is the basic question of recursive solution, we use 2 in a two-dimensional array to represent the maze wall, using a table
Show the path of the mouse, try to find the path from the entrance to the exit by the program.

Solution: The mouse's way has the upper, the left, the bottom, the right four directions, after each forward one lattice to choose One Direction forward, cannot before
Go back to select the next forward direction, so in the array test four direction sequentially, until the exit, which is
The basic problem of handing back.

Code:

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

int migo[7][7]={

{2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 0, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2}};//maze diagram

int starti=1,startj=1;//starting point
int endi=5,endj=5;//exit
int success=0;

int visit (int i,int j)
{
Migo[i][j]=1;
if (I==ENDI&AMP;&AMP;J==ENDJ)//Judge There is no exit
Success=1;
if (success!=1&&migo[i][j+1]==0) visit (i,j+1);//Four way, right, bottom, left, top
if (success!=1&&migo[i+1][j]==0) visit (I+1,J);
if (success!=1&&migo[i][j-1]==0) visit (i,j-1);
if (success!=1&&migo[i-1][j]==0) visit (I-1,J);
if (success!=1)
migo[i][j]=0;
return success;
}
int main ()
{
int i,j;
printf ("Show Maze: \ n");
for (i=0;i<7;i++)
{
for (j=0;j<7;j++)
if (migo[i][j]==2)
printf ("█");
Else
printf ("");
printf ("\ n");
}
if (Visit (STARTI,STARTJ) ==0)
{
printf ("\ nthe exit was not found! \ n ");
}
Else
{
printf ("\ n show path: \ n");
for (i=0;i<7;i++)
{
for (j=0;j<7;j++)
{
if (migo[i][j]==2)
printf ("█");
else if (migo[i][j]==1)
printf ("◇");
Else
printf ("");
}
printf ("\ n");
}
}
return 0;
}
2.Algorithm Gossip: The mouse Goes Astray (ii)

Description: Because of the maze design, the mouse to walk the maze of the entrance to the exit path may not be more than one, how to find all the path?

The solution is to make all the paths look complicated, but it's easier to show the path when the mouse walks to the exit and then retreat
Go back to the next position to re-select a place to continue to pass it, than to find a single path is simple, our program just make
Just a little change.

Code:

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

int migo[9][9]={{2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 2, 0, 2, 2, 0, 2},
{2, 0, 2, 0, 0, 2, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 0, 0, 0, 2, 0, 2},
{2, 2, 0, 2, 2, 0, 2, 0, 2},
{2, 0, 0, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2}};
Maze diagram

int starti=1,startj=1;//starting point
int endi=7,endj=7;//exit

void visit (int i,int j)
{
int m,n;
Migo[i][j]=1;
if (I==ENDI&AMP;&AMP;J==ENDJ)//Judge There is no exit
{
for (m=0;m<9;m++)
{
for (n=0;n<9;n++)
{
if (migo[m][n]==2)
printf ("█");
else if (migo[m][n]==1)
printf ("◇");
Else
printf ("");
}
printf ("\ n");
}
}
if (migo[i][j+1]==0) visit (i,j+1);//Four way, right, bottom, left, top
if (migo[i+1][j]==0) visit (I+1,J);
if (migo[i][j-1]==0) visit (i,j-1);
if (migo[i-1][j]==0) visit (I-1,J);
migo[i][j]=0;
}
int main ()
{
int i,j;
printf ("Show Maze: \ n");
for (i=0;i<9;i++)
{
for (j=0;j<9;j++)
if (migo[i][j]==2)
printf ("█");
Else
printf ("");
printf ("\ n");
}
Visit (STARTI,STARTJ);
return 0;
}

C-language algorithm for mice to walk the maze

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.