Aha, the fourth chapter of the second section of the rescue of small Ha Java implementation

Source: Internet
Author: User

 PackageCorejava; Public classFourtwo {Static intM//(M,n) is a few rows or columns    Static intN; Static intP//(P,Q) as the end point    Static intQ; Static intmin=9999; Static int[][]a=New int[51] [51];//Store Map    Static int[][]b=New int[51] [51];//Storage Path    StaticString []record=Newstring[9999];  Public Static voidDfsintXintYintStep) {        int[][]next={{0,1},{1,0},{0,-1},{-1,0}}; if(x==p&y==q) {System.out.println ("Step" +step);  for(inti=0;i<step;i++) {System.out.print (record[i]); } System.out.println (" "); if(step<min) {min=step; }            return; }         for(intk=0;k<4;k++)        {            intTx=x+next[k][0]; intTy=y+next[k][1]; if(tx<0| | tx>=m| | ty<0| | ty>=N) {Continue; }            if(a[tx][ty]==0&b[tx][ty]==0) {Record[step]= "(" +tx+ "," +ty+ ")"; B[tx][ty]=1; DFS (Tx,ty,step+1);//if this line of code changes to step++;d FS (tx,ty,step), then the value of step in the for loop is changed and there is a problemB[tx][ty]=0; }                                }        return; }     Public Static voidMain (String []args) {m= 5; N= 4; intStartx=0; intStarty=0; P=3; Q=2; B[startx][starty]=1; record[0]= "(" +0+ "," +0+ ")"; a[0][2]=1; a[2][2]=1; a[3][1]=1; a[4][3]=1; DFS (Startx,starty,0); System.out.println ("Min" +min); }}

Here it feels too troublesome to get the input and set the fixed value directly. At the same time Dfs (TX,TY,STEP+1); Here it bothers me, began not to consider the step in the cycle of use, after step=step+1 changed the loop step value, query for a long time to know the cause of the error.

Aha, the fourth chapter of the second section of the rescue of small Ha Java implementation

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.