# Learning Log---recursive, non-recursive, maze problem

Source: Internet
Author: User

Recursive algorithm Design method:

The sufficient and necessary conditions for solving problems that are suitable for recursive algorithms are:

(1) The problem has some kind of the nature of the description of the sub-problem that can be borrowed.
(2) The sub-problem of a finite step (also known as the primitive problem) has a direct solution existence.
When a problem exists in the above two basic elements, the method of designing the recursive algorithm for the problem is:
(1) The solution to the original problem is expressed in the form of a pair problem solving.
(2) Design recursive export.

Factorial:

`import java.util.scanner;public class jiechengdemo {    public  Static long jiecheng (Int num)     {         if (num==1)         {             return 1;        }         else        {             return num*jiecheng (num-1);  //recursive call          }    }      public  static void main (String[] args)  {                 int num;         Long result;  &nbSp;     scanner in = new scanner (System.in);         system.out.println ("Please enter a positive integer:");         Num = in.nextint ();        result =  Jiechengdemo.jiecheng (num); the factorial of         system.out.println (num+) is: "+ result);         }}`

Binary lookup Recursive implementation:

`import java.util.scanner;public class binarysearch {         public static boolean bsearch (int[] arr,int des,int low,int  High)     {        int mid; //intermediate subscript          if (Low > high)          {            return false;         }        mid =  ( Low+high)/2;        if (Des == arr[mid])          {            return  true;        }         Else if (DES&NBSP;&LT;&NBSp;arr[mid])         {            return bsearch (arr,des,low,mid-1);  //recursive call              }        else         {           return  Bsearch (Arr,des,mid+1,high);            }     }             public  Static void main (String[] args)  {        //  Todo auto-generated method stub        int[] arr  = {1,6,11,17,23,45,56,76,87,93};        int des;      &nbSp;  scanner in = new scanner (system.in);         system.out.println ("Please enter a positive integer (0-99):");         des =  in.nextint ();         if (Binarysearch.bsearch (Arr, des,  0, arr.length-1))         {             system.out.println (des+ "in the array! ");        }        else         {             system.out.println (des+ "is not in the array!) ");         }    }}`
` Fibonacci Gschirnwirt Series:  `
`public class bolienac {      //get the value of the nth item in the Wave list      public static int getnums (int n)     {         if (n==1| | n==2)         {             return 1;        }         else        {             return getnums (n-1) +getnums (n-2);         }    }             public static void main (String[] args)     {         //1,1,2,3,5,8,13        int n=7;        int result=0;         for (int i=n;i>=1;i--)         {             result+=bolienac.getnums (i);         }        system.out.println (the sum of the preceding "+n+" items of the Wave-list is: "+ result);     }    }`

`public class gcbnumber {        //the recursive algorithm of greatest common divisor. &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;INT&NBSP;GCB (Int bignum,int smallnum)      {        if (bignum<0| | smallnum<0)         {             return -1; //said no greatest common divisor was found.         }        if (smallNum== 0)         {             return bigNum;        }         if (Bignum < smallnum)         {             //Here is also a good way to apply a function      &nbSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;GCB (Smallnum,bignum);         }        else         {           //here is a large number divided by decimal to take the remainder and decimal   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;GCB (SmallNum,bigNum%smallNum);             }    }             public static void main (String[]  args)     {         int smallnum =  30;         int bigNum = 25;   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;INT&NBSP;RESULT&NBSP;=&NBSP;GCBNUMBER.GCB (BigNum, smallNum);      &nbsP;   if (result!=-1)          {    The greatest common divisor of           system.out.println (bigNum+ "and" +smallNum+ "are:" + Result);         }             }}`

Recursive process and runtime stack:

The execution process of recursive functions has three characteristics:
(1) The function name is the same;
(2) constant self-invocation;
(3) The last function to be called is returned first.
The stack that the system uses to hold recursive function call information is called the run-time stack.
Each level of recursive invocation of the information required to hold the data constitutes a working record of the runtime stack
The work record at the top of the stack holds information about the current calling function, so the work record at the top of the stack is also known as the activity record.

Recursive conversion to non-recursive algorithm:

In general, the following two cases of recursive algorithm can be converted to non-recursive algorithm:
(1) There is a non-recursive algorithm for the loop structure without the help of the stack, such as factorial calculation problem, Fibonacci sequence calculation problem, binary lookup problem, etc.
(2) There is a non-recursive algorithm using the loop structure of the stack. All recursive algorithms can be converted to a loop-structured non-recursive algorithm by means of stacks.

Backtracking Method : Also a classical idea in recursive algorithm

The basic idea of backtracking is to decompose the original problem into an algorithm for solving several sub-problems, including a number of nodes, each node having several search branches. When searching for a node and discovering that it is no longer possible to continue searching, let the search process backtrack (that is, return) to the previous node of the node, and continue searching for other branches of the node that have not yet been searched, and if the node is found to be unable to continue the search, Let the search process go back to the previous node of the node to continue the search process until the search has solved the problem or searched all the searchable branches without a solution to exist.

Maze problem:

`public class mymaze {        private static  I subscript     private static int startposj;//entrance of the int startposi;//Inlet, J subscript   I subscript     private  of    private static int endposi;//exit J Subscript         //for exit static int endposj;//set the coordinates of the maze entry      public void setstart (INT&NBSP;STARTPOSI,INT&NBSP;STARTPOSJ)     {        MyMaze.startPosI = startPosI;        MyMaze.startPosJ = startPosJ;    }         //set the coordinate     public void setend of the maze exit (Int endPosI,int &NBSP;ENDPOSJ)     {       MyMaze.endPosI =  Endposi;       mymaze.endposj = endposj;    }       Algorithm of   //Maze search pathway     /*  cell: Maze map      *  J-coordinate of I      *  j:  inlet of  i:  entrance      **/     public static void visited (INT[][]&NBSP;CELL,INT&NBSP;I,INT&NBSP;J)     {        cell[i][j]=1;                 if (I==ENDPOSI&AMP;&AMP;J==ENDPOSJ)  //found the exit         {             system.out.println ("Find a Pathway:");             for (int m=0;m<cell.length;m++)              {&nbsP;               for (int n=0;n <cell[0].length;n++)                  {                     if (cell[m][n]==2)                      {                         system.out.print ("2" );                     }                     else if (cell[m][n]==1)                      {                         system.out.print ("*");                          }                     else                     {                          System.out.print (" ");                     }                 }                system.out.println ();             }         }else{             //to the left for access             if (cell[i][j-1]==0)              {                 visited (cell,i,j-1);             }            //to the right for access.             if (cell[i][j+1]==0)              {                 visited (cell,i,j+1);            }             //looking up access              if (cell[i-1][j]==0)              {                Visited (CELL,I-1,J);            }             //Down for access              if (cell[i+1][j]==0)              {                 Visited (CELL,I+1,J);            }              }                //below the change 0 is critical, this is critical, is the nature of backtracking, if the road can not go through, then the original way to restore, the function of the previous layer             the end of the call, the function of the previous layer is to find the other way. Every search is not set to 0, until            the last step to print, and then                    cell[i][j]=0;     }        public static void main ( String[] args)  {        // todo auto-generated  method stub                //initializing a maze map         int[][] maze={                 {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,2,2},                  {2,0,0,0,0,0,0,0,2},                  {2,2,2,2,2,2,2,2,2}         };                mymaze  cell = new mymaze ();         cell.setstart (1, &NBSP;1);//Set the entry coordinates of the maze         cell.setend (7, 7);//Set the export coordinates of the maze          cell.visited (maze, 1, 1);     }}`

Learning Log---recursive, non-recursive, maze problem

Related Keywords:

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.