Skip to main content
deleted 185 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

Java 8, 421 408 403 400400 351 bytes

int M[][],v[][],l,L;m->{int i=(l=m.length)*(L=m[0].length);for(M=m,v=new int[l][L];m[--i%l][i/l]!=65;);return f(i%l,i/l,-1>>>1,-1);};intint fz(int x,int y,int r,int d){if(M[x][y]>65)return r>d?d:r;d+=v[x][y]=1;r=v(x+1,y)x<l&y<L&x>=0&y>=0&&M[x][y]>32&v[x][y]<1?f(x+1x,y,r,d):r;r=v(x,y+1)?r;}int f(int x,y+1int y,int r,int d){return M[x][y]>65?r>d?d:r;r=vr:z(x,y-1,y)?fz(x-1,y,r,d):r;r=vz(x,y-1)?fy+1,z(xx+1,y-1,r,d+=v[x][y]=1),d):r;v[x][y]=0;return r;}boolean v(int x,int yd){return x<l&y<L&x>=0&y>=0&&M[x][y]>32&v[x][y]<1;,d)+(v[x][y]=0);}

-857 bytes thanks to @ceilingcat.

Try it online.Try it online.

int M[][],                // Matrix on class-level, starting uninitialized
    v[][],                // Visited matrix on class-level, starting uninitialized
    l,L;                  // x and y dimensions on class-level, starting uninitialized

m->{                      // Method with integer-matrix as input and integer as return
 int i=(l=m.length)       //  Set `l` to the amount of rows
       *(L=m[0].length);  //  Set `L` to the amount of columns
                          //  And set `i` to the product of the two
 for(M=m,                 //  Set `M` to the input-matrix
     v=new int[l][L];     //  Create the visited-matrix filled with 0s
     m[--i%l][i/l]!=65;); //  Loop as long as the current cell doesn't contain an 'A'
 return f(                //  Start the recursive method `f` with:
   i%l,i/l,               //   The current cell as the starting x,y-coordinate
   -1>>>1,                //   Integer.MAX_VALUE as starting minimum-distance
   -1);}                  //   And -1 as amount of steps

int fz(int x,int y,int r,int d){
                          // Create theMethod recursive`z` method
to check if(M[x][y]>65)whether we can travel to the given cell
  return x<l&y<L&x>=0&y>=0//  If the currentx,y-coordinate cellis containswithin 'B':the matrix boundaries,
    return&&M[x][y]>32 r>d?d:r;         //  and Returnthat the minimum ofcurrent thecell min-distancedoes andNOT amountcontain ofa stepsspace,
  d+=v[x][y]=1;  &v[x][y]<1?           //  Markand thewe currenthaven't cellvisited asthis visitedcell yet:
      f(x,y,r,d)          //   Return a call to the recursive method //`f` with Andthe increasesame thearguments
 amount of steps by: 1 at the same time
  r=v(x+1,y)?              //  IfElse:
 we can travel south:
 r;}                  //   Return the row

int f(x+1int x,int y,int r,int d):r;{
       //   Set the min-distance to a recursive call southwards
  r=v(x,y+1)?       // Create the recursive method `f`
  return M[x][y]>65?      //  If wethe cancurrent travelcell eastcontains 'B':
    f(x,y+1,r, r>d?d):r;r       //   Set the min-distance to a recursive call eastwards
//  r=v(x-1,y)? Return the minimum of the min-distance and amount of steps
   // : If we can travel north:
    f(x-1,y,r,d):r;       //   Set the min-distance// to aElse:
 recursive call northwards
  r=vz(x,y-1)?,             //  If weDo cana travelrecursive west:`z`-call westward,
    f   z(x,y-1,ry,d):r;       //   Set the// min-distance to do a recursive `z`-call westwardsnorthward,
  v[x][y]=0;       z(x,y+1,         //  Unmark thedo currenta cellrecursive as`z`-call visitedeastward,
  return r;}         z(x+1,y,r,     // And return the amount of stepsdo asa result

booleanrecursive v(int`z`-call xsouthward,int y){  
  // Method to check whether we can travel to the given celld+=v[x][y]=1),d),d),d)
  return x<l&y<L&x>=0&y>=0//  If the x,y-coordinate is within the matrix boundaries
    &&M[x][y]>32          //   Checkafter thatwe've first marked the current cell does NOT contain aas spacevisited
    &v[x][y]<1;}   +(v[x][y]=0);}       //   And we haven'tunmark visitedthe thiscurrent cell yetas visited afterwards

Java 8, 421 408 403 400 bytes

int M[][],v[][],l,L;m->{int i=(l=m.length)*(L=m[0].length);for(M=m,v=new int[l][L];m[--i%l][i/l]!=65;);return f(i%l,i/l,-1>>>1,-1);};int f(int x,int y,int r,int d){if(M[x][y]>65)return r>d?d:r;d+=v[x][y]=1;r=v(x+1,y)?f(x+1,y,r,d):r;r=v(x,y+1)?f(x,y+1,r,d):r;r=v(x-1,y)?f(x-1,y,r,d):r;r=v(x,y-1)?f(x,y-1,r,d):r;v[x][y]=0;return r;}boolean v(int x,int y){return x<l&y<L&x>=0&y>=0&&M[x][y]>32&v[x][y]<1;}

-8 bytes thanks to @ceilingcat.

Try it online.

int M[][],                // Matrix on class-level, starting uninitialized
    v[][],                // Visited matrix on class-level, starting uninitialized
    l,L;                  // x and y dimensions, starting uninitialized

m->{                      // Method with integer-matrix as input and integer as return
 int i=(l=m.length)       //  Set `l` to the amount of rows
       *(L=m[0].length);  //  Set `L` to the amount of columns
                          //  And set `i` to the product of the two
 for(M=m,                 //  Set `M` to the input-matrix
     v=new int[l][L];     //  Create the visited-matrix filled with 0s
     m[--i%l][i/l]!=65;); //  Loop as long as the current cell doesn't contain an 'A'
 return f(                //  Start the recursive method with:
   i%l,i/l,               //   The current cell as the starting x,y-coordinate
   -1>>>1,                //   Integer.MAX_VALUE as starting minimum-distance
   -1);}                  //   And -1 as amount of steps

int f(int x,int y,int r,int d){
                          // Create the recursive method
  if(M[x][y]>65)          //  If the current cell contains 'B':
    return r>d?d:r;       //   Return the minimum of the min-distance and amount of steps
  d+=v[x][y]=1;           //  Mark the current cell as visited
                          //  And increase the amount of steps by 1 at the same time
  r=v(x+1,y)?             //  If we can travel south:
    f(x+1,y,r,d):r;       //   Set the min-distance to a recursive call southwards
  r=v(x,y+1)?             //  If we can travel east:
    f(x,y+1,r,d):r;       //   Set the min-distance to a recursive call eastwards
  r=v(x-1,y)?             //  If we can travel north:
    f(x-1,y,r,d):r;       //   Set the min-distance to a recursive call northwards
  r=v(x,y-1)?             //  If we can travel west:
    f(x,y-1,r,d):r;       //   Set the min-distance to a recursive call westwards
  v[x][y]=0;              //  Unmark the current cell as visited
  return r;}              // And return the amount of steps as result

boolean v(int x,int y){   // Method to check whether we can travel to the given cell
  return x<l&y<L&x>=0&y>=0//  If the x,y-coordinate is within the matrix boundaries
    &&M[x][y]>32          //   Check that the current cell does NOT contain a space
    &v[x][y]<1;}          //   And we haven't visited this cell yet

Java 8, 421 408 403 400 351 bytes

int M[][],v[][],l,L;m->{int i=(l=m.length)*(L=m[0].length);for(M=m,v=new int[l][L];m[--i%l][i/l]!=65;);return f(i%l,i/l,-1>>>1,-1);}int z(int x,int y,int r,int d){return x<l&y<L&x>=0&y>=0&&M[x][y]>32&v[x][y]<1?f(x,y,r,d):r;}int f(int x,int y,int r,int d){return M[x][y]>65?r>d?d:r:z(x,y-1,z(x-1,y,z(x,y+1,z(x+1,y,r,d+=v[x][y]=1),d),d),d)+(v[x][y]=0);}

-57 bytes thanks to @ceilingcat.

Try it online.

int M[][],                // Matrix on class-level, starting uninitialized
    v[][],                // Visited matrix on class-level, starting uninitialized
    l,L;                  // x and y dimensions on class-level, starting uninitialized

m->{                      // Method with integer-matrix as input and integer as return
 int i=(l=m.length)       //  Set `l` to the amount of rows
       *(L=m[0].length);  //  Set `L` to the amount of columns
                          //  And set `i` to the product of the two
 for(M=m,                 //  Set `M` to the input-matrix
     v=new int[l][L];     //  Create the visited-matrix filled with 0s
     m[--i%l][i/l]!=65;); //  Loop as long as the current cell doesn't contain an 'A'
 return f(                //  Start the recursive method `f` with:
   i%l,i/l,               //   The current cell as the starting x,y-coordinate
   -1>>>1,                //   Integer.MAX_VALUE as starting minimum-distance
   -1);}                  //   And -1 as amount of steps

int z(int x,int y,int r,int d){
                          // Method `z` to check whether we can travel to the given cell
  return x<l&y<L&x>=0&y>=0//  If the x,y-coordinate is within the matrix boundaries,
    &&M[x][y]>32          //  and that the current cell does NOT contain a space,
    &v[x][y]<1?           //  and we haven't visited this cell yet:
      f(x,y,r,d)          //   Return a call to the recursive method `f` with the same arguments
    :                     //  Else:
     r;}                  //   Return the row

int f(int x,int y,int r,int d){
                          // Create the recursive method `f`
  return M[x][y]>65?      //  If the current cell contains 'B':
     r>d?d:r              //   Return the minimum of the min-distance and amount of steps
    :                     //  Else:
     z(x,y-1,             //   Do a recursive `z`-call westward,
       z(x-1,y,           //   do a recursive `z`-call northward,
         z(x,y+1,         //   do a recursive `z`-call eastward,
           z(x+1,y,r,     //   do a recursive `z`-call southward, 
             d+=v[x][y]=1),d),d),d)
                          //   after we've first marked the current cell as visited
     +(v[x][y]=0);}       //   And unmark the current cell as visited afterwards
added 18 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

Java 8, 421 408 403403 400 bytes

int M[][],v[][],l,L;m->{int i=(l=m.length)*(L=m[0].length);for(M=m,v=new int[l][L];;)if(m[int[l][L];m[--i%l][i/l]==65l]!=65;)return;return f(i%l,i/l,-1>>>1,-1);};int f(int x,int y,int r,int d){if(M[x][y]>65)return r>d?d:r;d+=v[x][y]=1;r=v(x+1,y)?f(x+1,y,r,d):r;r=v(x,y+1)?f(x,y+1,r,d):r;r=v(x-1,y)?f(x-1,y,r,d):r;r=v(x,y-1)?f(x,y-1,r,d):r;v[x][y]=0;return r;}boolean v(int x,int y){return x<l&y<L&x>=0&y>=0&&M[x][y]>32&v[x][y]<1;}

-58 bytes thanks to @ceilingcat.

Try it online.Try it online.

int M[][],                // Matrix on class-level, starting uninitialized
    v[][],                // Visited matrix on class-level, starting uninitialized
    l,L;                  // x and y dimensions, starting uninitialized

m->{                      // Method with integer-matrix as input and integer as return
 int i=(l=m.length)       //  Set `l` to the amount of rows
       *(L=m[0].length);  //  Set `L` to the amount of columns
                          //  And set `i` to the product of the two
 for(M=m,                 //  Set `M` to the input-matrix
     v=new int[l][L];     //  Create the visited-matrix filled with 0s
     ;)                   //  Loop indefinitely:
   if(m[--i%l][i/l]==65l]!=65;) ; //  Loop Ifas long as the current cell containsdoesn't contain an 'A':
 return f(   return f(            //    Start the recursive method with:
       i%l,i/l,           //    //   The current cell as the starting x,y-coordinate
   -1>>>1,    -1>>>1,            //     Integer.MAX_VALUE as starting minimum-distance
       -1);}              //    //   And -1 as amount of steps

int f(int x,int y,int r,int d){
                          // Create the recursive method
  if(M[x][y]>65)          //  If the current cell contains 'B':
    return r>d?d:r;       //   Return the minimum of the min-distance and amount of steps
  d+=v[x][y]=1;           //  Mark the current cell as visited
  d++;                        //  And increase the amount of steps by 1 at the same time
  r=v(x+1,y)?             //  If we can travel south:
    f(x+1,y,r,d):r;       //   Set the min-distance to a recursive call southwards
  r=v(x,y+1)?             //  If we can travel east:
    f(x,y+1,r,d):r;       //   Set the min-distance to a recursive call eastwards
  r=v(x-1,y)?             //  If we can travel north:
    f(x-1,y,r,d):r;       //   Set the min-distance to a recursive call northwards
  r=v(x,y-1)?             //  If we can travel west:
    f(x,y-1,r,d):r;       //   Set the min-distance to a recursive call westwards
  v[x][y]=0;              //  Unmark the current cell as visited
  return r;}              // And return the amount of steps as result

boolean v(int x,int y){   // Method to check whether we can travel to the given cell
  return x<l&y<L&x>=0&y>=0//  If the x,y-coordinate is within the matrix boundaries
    &&M[x][y]>32          //   Check that the current cell does NOT contain a space
    &v[x][y]<1;}          //   And we haven't visited this cell yet

Java 8, 421 408 403 bytes

int M[][],v[][],l,L;m->{int i=(l=m.length)*(L=m[0].length);for(M=m,v=new int[l][L];;)if(m[--i%l][i/l]==65)return f(i%l,i/l,-1>>>1,-1);};int f(int x,int y,int r,int d){if(M[x][y]>65)return r>d?d:r;d+=v[x][y]=1;r=v(x+1,y)?f(x+1,y,r,d):r;r=v(x,y+1)?f(x,y+1,r,d):r;r=v(x-1,y)?f(x-1,y,r,d):r;r=v(x,y-1)?f(x,y-1,r,d):r;v[x][y]=0;return r;}boolean v(int x,int y){return x<l&y<L&x>=0&y>=0&&M[x][y]>32&v[x][y]<1;}

-5 bytes thanks to @ceilingcat.

Try it online.

int M[][],                // Matrix on class-level, starting uninitialized
    v[][],                // Visited matrix on class-level, starting uninitialized
    l,L;                  // x and y dimensions, starting uninitialized

m->{                      // Method with integer-matrix as input and integer as return
 int i=(l=m.length)       //  Set `l` to the amount of rows
       *(L=m[0].length);  //  Set `L` to the amount of columns
                          //  And set `i` to the product of the two
 for(M=m,                 //  Set `M` to the input-matrix
     v=new int[l][L];     //  Create the visited-matrix filled with 0s
     ;)                   //  Loop indefinitely:
   if(m[--i%l][i/l]==65)  //   If the current cell contains an 'A':
     return f(            //    Start the recursive method with:
       i%l,i/l,           //     The current cell as the starting x,y-coordinate
       -1>>>1,            //     Integer.MAX_VALUE as starting minimum-distance
       -1);}              //     And -1 as amount of steps

int f(int x,int y,int r,int d){
                          // Create the recursive method
  if(M[x][y]>65)          //  If the current cell contains 'B':
    return r>d?d:r;       //   Return the minimum of the min-distance and amount of steps
  d+=v[x][y]=1;           //  Mark the current cell as visited
  d++;                    //  And increase the amount of steps by 1 at the same time
  r=v(x+1,y)?             //  If we can travel south:
    f(x+1,y,r,d):r;       //   Set the min-distance to a recursive call southwards
  r=v(x,y+1)?             //  If we can travel east:
    f(x,y+1,r,d):r;       //   Set the min-distance to a recursive call eastwards
  r=v(x-1,y)?             //  If we can travel north:
    f(x-1,y,r,d):r;       //   Set the min-distance to a recursive call northwards
  r=v(x,y-1)?             //  If we can travel west:
    f(x,y-1,r,d):r;       //   Set the min-distance to a recursive call westwards
  v[x][y]=0;              //  Unmark the current cell as visited
  return r;}              // And return the amount of steps as result

boolean v(int x,int y){   // Method to check whether we can travel to the given cell
  return x<l&y<L&x>=0&y>=0//  If the x,y-coordinate is within the matrix boundaries
    &&M[x][y]>32          //   Check that the current cell does NOT contain a space
    &v[x][y]<1;}          //   And we haven't visited this cell yet

Java 8, 421 408 403 400 bytes

int M[][],v[][],l,L;m->{int i=(l=m.length)*(L=m[0].length);for(M=m,v=new int[l][L];m[--i%l][i/l]!=65;);return f(i%l,i/l,-1>>>1,-1);};int f(int x,int y,int r,int d){if(M[x][y]>65)return r>d?d:r;d+=v[x][y]=1;r=v(x+1,y)?f(x+1,y,r,d):r;r=v(x,y+1)?f(x,y+1,r,d):r;r=v(x-1,y)?f(x-1,y,r,d):r;r=v(x,y-1)?f(x,y-1,r,d):r;v[x][y]=0;return r;}boolean v(int x,int y){return x<l&y<L&x>=0&y>=0&&M[x][y]>32&v[x][y]<1;}

-8 bytes thanks to @ceilingcat.

Try it online.

int M[][],                // Matrix on class-level, starting uninitialized
    v[][],                // Visited matrix on class-level, starting uninitialized
    l,L;                  // x and y dimensions, starting uninitialized

m->{                      // Method with integer-matrix as input and integer as return
 int i=(l=m.length)       //  Set `l` to the amount of rows
       *(L=m[0].length);  //  Set `L` to the amount of columns
                          //  And set `i` to the product of the two
 for(M=m,                 //  Set `M` to the input-matrix
     v=new int[l][L];     //  Create the visited-matrix filled with 0s
     m[--i%l][i/l]!=65;); //  Loop as long as the current cell doesn't contain an 'A'
 return f(                //  Start the recursive method with:
   i%l,i/l,               //   The current cell as the starting x,y-coordinate
   -1>>>1,                //   Integer.MAX_VALUE as starting minimum-distance
   -1);}                  //   And -1 as amount of steps

int f(int x,int y,int r,int d){
                          // Create the recursive method
  if(M[x][y]>65)          //  If the current cell contains 'B':
    return r>d?d:r;       //   Return the minimum of the min-distance and amount of steps
  d+=v[x][y]=1;           //  Mark the current cell as visited
                          //  And increase the amount of steps by 1 at the same time
  r=v(x+1,y)?             //  If we can travel south:
    f(x+1,y,r,d):r;       //   Set the min-distance to a recursive call southwards
  r=v(x,y+1)?             //  If we can travel east:
    f(x,y+1,r,d):r;       //   Set the min-distance to a recursive call eastwards
  r=v(x-1,y)?             //  If we can travel north:
    f(x-1,y,r,d):r;       //   Set the min-distance to a recursive call northwards
  r=v(x,y-1)?             //  If we can travel west:
    f(x,y-1,r,d):r;       //   Set the min-distance to a recursive call westwards
  v[x][y]=0;              //  Unmark the current cell as visited
  return r;}              // And return the amount of steps as result

boolean v(int x,int y){   // Method to check whether we can travel to the given cell
  return x<l&y<L&x>=0&y>=0//  If the x,y-coordinate is within the matrix boundaries
    &&M[x][y]>32          //   Check that the current cell does NOT contain a space
    &v[x][y]<1;}          //   And we haven't visited this cell yet
Commonmark migration
Source Link

#Java 8, 421 408 403 bytes

Java 8, 421 408 403 bytes

#Java 8, 421 408 403 bytes

Java 8, 421 408 403 bytes

added 26 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384
Loading