Huawei interview Questions: Maze problem C language source

Source: Internet
Author: User

Defines a two-dimensional array n*m (where 2<=n<=10;2<=m<=10), as shown in the 5x5 array:


int maze[5][5] = {


0, 1, 0, 0, 0,


0, 1, 0, 1, 0,


0, 0, 0, 0, 0,


0, 1, 1, 1, 0,


0, 0, 0, 1, 0,


};


It represents a maze, of which 1 represents a wall, 0 means that the road can be walked, can only walk sideways or vertical walk, can not be inclined to walk, asked to compile the program to find the shortest route from the upper left to the lower right corner. The entry point is [0,0], and the first space is the way to go.

Input

A NXM two-dimensional array that represents a maze. The data guarantee has a unique solution, regardless of the case of multiple solutions, that is, the maze has only one channel.

Output

The shortest path in the upper-left corner to the lower-right corner, formatted as shown in the sample.

5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Output
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(bis)

#include "stdio.h" #include "stdlib.h" #include "string.h" #define MAX_PATH 256int maze[10][10] = {0};int Route[100][2] = {0 };int Main () {int row=0,line=0;scanf ("%d%d", &row,&line), for (int. i=0;i<row;i++) {for (int j=0;j<line;j++ {scanf ("%d", &maze[i][j]);}} Go Maze//stack: Record previous position int xcurrent = 0;int ycurrent = 0;int Count=0;while (true) {if (maze[xcurrent+1][ycurrent]==0 && Xcurrent+1<row) {//Return to previous position if (route[count-1][0]==xcurrent+1 && route[count-1][1]==ycurrent) {maze[ xcurrent][ycurrent]=1;//is set to Wall count--;xcurrent++;} else{route[count][0]=xcurrent;route[count][1]=ycurrent;count++;xcurrent++;}} else if (maze[xcurrent][ycurrent+1]==0 && ycurrent<line) {if (route[count-1][0]==xcurrent && route[ count-1][1]==ycurrent+1) {maze[xcurrent][ycurrent]=1;//is set to Wall count--;ycurrent++;} else{route[count][0]=xcurrent;route[count][1]=ycurrent;count++;ycurrent++;}} else if (maze[xcurrent-1][ycurrent]==0 && xcurrent-1>=0) {if (route[count-1][0]==xcurrent-1 &&Amp Route[count-1][1]==ycurrent) {maze[xcurrent][ycurrent]=1;//is set to Wall count--;xcurrent--;} else{route[count][0]=xcurrent;route[count][1]=ycurrent;count++;xcurrent--;}} else if (maze[xcurrent][ycurrent-1]==0 && ycurrent-1>=0) {if (route[count-1][0]==xcurrent && route[ Count-1][1]==ycurrent-1) {maze[xcurrent][ycurrent]=1;//is set to Wall count--;ycurrent--;} else{route[count][0]=xcurrent;route[count][1]=ycurrent;count++;ycurrent--;}} if (xcurrent==row-1 && ycurrent==line-1) {route[count][0]=xcurrent;route[count][1]=ycurrent;count++;break;}} for (int i=0;i<count;i++) {printf ("(%d,%d) \ n", route[i][0],route[i][1]);} return 0;}


Huawei interview Questions: Maze problem C language source

Related Article

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.