fork(1) download
  1. import java.util.Scanner;
  2.  
  3. public class Main{
  4. static int depth=0;
  5. static int parr[][]=null;
  6. static int n,m,enr,enc,maxstep=0;
  7.  
  8. public static void main(String args[])
  9. {
  10. int str,stc;
  11. Scanner scan=new Scanner(System.in);
  12. n=scan.nextInt();
  13. m=scan.nextInt();
  14. str=scan.nextInt()-1;
  15. stc=scan.nextInt()-1;
  16. enr=scan.nextInt()-1;
  17. enc=scan.nextInt()-1;
  18. parr=new int [n][m];
  19. parr[str][stc]=1;
  20.  
  21. find(str,stc,1);
  22. System.out.println("Maximum step="+maxstep);
  23.  
  24. }
  25.  
  26. public static void find(int str,int stc,int step)
  27. {
  28. //depth++;
  29. //System.out.println(depth);
  30. //System.out.println(step);
  31. for(int i=1;i<=4;i++)
  32. {
  33.  
  34. int nstr=str,nstc=stc;
  35. if(i==1)
  36. {
  37. if(str>0 && parr[str-1][stc]!=1)
  38. nstr-=1;
  39. }
  40.  
  41. if(i==2)
  42. {
  43. if(stc>0 && parr[str][stc-1]!=1)
  44. nstc-=1;
  45. }
  46.  
  47. if(i==3)
  48. {
  49. if(str<n-1 && parr[str+1][stc]!=1)
  50. nstr+=1;
  51. }
  52.  
  53. if(i==4)
  54. {
  55. if(stc<n-1 && parr[str][stc+1]!=1)
  56. nstc+=1;
  57. }
  58.  
  59. if(str==nstr && stc==nstc)
  60. {
  61. continue;
  62. }
  63. else
  64. {
  65. parr[nstr][nstc]=1;
  66. step++;
  67. //chk target reached
  68. if(nstr==enr && nstc==enc)
  69. {
  70. if(maxstep==0)maxstep=step;else if(step>maxstep)maxstep=step;
  71. step--;
  72. parr[nstr][nstc]=0;
  73. return;
  74. }
  75. else
  76. {
  77. find(nstr,nstc,step);
  78. step--;
  79. // depth--;
  80. // System.out.println(depth);
  81. parr[nstr][nstc]=0;
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. }
  90.  
  91. }
  92. }
  93.  
  94. }
  95.  
Success #stdin #stdout 1.46s 216128KB
stdin
6 6
1 1
6 6
stdout
Maximum step=35