fork download
  1. class Matrix {
  2.  
  3. public static void main(String[] args){
  4. int[][] _matrix = new int[6][6];
  5. _matrix[0] = new int[]{0,0,0,0,1,0};
  6. _matrix[1] = new int[]{0,0,0,0,1,0};
  7. _matrix[2] = new int[]{0,0,0,1,1,0};
  8. _matrix[3] = new int[]{0,0,0,1,0,0};
  9. _matrix[4] = new int[]{0,0,0,1,1,0};
  10. _matrix[5] = new int[]{0,0,0,0,1,0};
  11.  
  12. _matrix[0] = new int[]{1,0,0,0,0,0};
  13. _matrix[1] = new int[]{1,1,1,1,0,0};
  14. _matrix[2] = new int[]{0,0,0,1,0,0};
  15. _matrix[3] = new int[]{0,0,0,0,0,0};
  16. _matrix[4] = new int[]{0,0,0,1,1,0};
  17. _matrix[5] = new int[]{0,0,0,0,1,0};
  18.  
  19. int _m = 0;
  20. for(int _n = 0; _n < _matrix[_m].length; _n++){
  21. int _value = _matrix[_m][_n];
  22.  
  23. if(_value == 1){
  24. System.out.println("start m = " + _m + " n = " + _n);
  25. neighbour(_matrix, _m, _n, -1, -1);
  26. break;
  27. }
  28. }
  29. }
  30.  
  31. public static void neighbour(int[][] matrix, int m, int n, int mm, int nn){
  32.  
  33. int _n = n - 1;
  34. if(_n >= 0 && _n != nn){//para não estourar o indice
  35. if(1== matrix[m][_n]){
  36. //para traz
  37. System.out.println(" m = " + m + " n = " + _n);
  38. neighbour(matrix, m, _n, -1, n);
  39. }
  40. }
  41.  
  42. int _m = m + 1;
  43. if(_m < matrix.length && _m != mm){//para não estourar o indice
  44. if(1== matrix[_m][n]){
  45. //para baixo
  46. System.out.println(" m = " + _m + " n = " + n);
  47. neighbour(matrix, _m, n, m, -1);
  48. }
  49. }
  50. /*
  51. *
  52. */
  53. _n = n + 1;
  54. if(_n < matrix[m].length && _n != nn){//para não estourar o indice
  55. if(1== matrix[m][_n]){
  56. //para frente
  57. System.out.println(" m = " + m + " n = " + _n);
  58. neighbour(matrix, m, _n, -1, n);
  59. }
  60. }
  61.  
  62. //nao explora para cima, pois queremos "descer" na matriz
  63. }
  64.  
  65. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
start m = 0 n = 0
      m = 1 n = 0
      m = 1 n = 1
      m = 1 n = 2
      m = 1 n = 3
      m = 2 n = 3