We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
For something different, here's a 'supercomputer' solution in Java 17. This performs N-way search over all the knights at once, given N threads. Note it may run slower on single-thread hardware.
classResult{/* * Compute minimal distance for a knight making L-shaped moves. * * Notes: Essentially a mere implementation problem, but we can * parallelize the search, and store identical states (homologs). * * Uses a reductive design for potential concurrency gains, * backed by a Red-Black Tree of dependencies (ConcurrentHashMap). * (remove .parallel for sequential hardware) * * 𝚯(N) runtime for each Knight, given N = board size (n * n). * * Total runtime 𝚯(N log N) with unlimited processors and neighbor * reducing on, otherwise 𝚯(knights * searches) = 𝚯(n * N) = 𝚯(n³). * * 𝚯(N) space complexity for each search */staticMap<Set<Integer>,Integer>memo=newConcurrentHashMap<>();publicstaticList<List<Integer>>knightOnAChessboard(intn){returnIntStream.range(1,n).parallel().mapToObj(x->IntStream.range(1,n).parallel().map(y->memoize(newKnight(x,y,n))).boxed().toList()).toList();}/* Store mirrors of this Knight for simple dynamic programming. */privatestaticIntegermemoize(Knightk){returnmemo.merge(k.homolog(),distanceOf(k),(a,b)->a);}/* Compute the board distance for this Knight, via BFS */privatestaticIntegerdistanceOf(Knightk){finalPointdestination=newPoint(k.n-1,k.n-1);finalPointorigin=newPoint(newPoint(0,0));Deque<Point>queue=newArrayDeque<>(List.of(origin));Set<Point>alreadyVisited=newHashSet<>(List.of(origin));k.atlas.put(origin,0);do{Pointcurrent=queue.remove();if(current.equals(destination)){returnk.atlas.get(current);}else{alreadyVisited.add(current);queue.addAll(k.neighborsOf(current));queue.removeAll(alreadyVisited);}}while(!queue.isEmpty());return-1;}}/* * Knight moving in (dx, dy) fashion on chessboard. * The knight keeps an 'odometer' of the minimum moves * to reach a given square. */classKnight{Map<Point,Integer>atlas=newConcurrentHashMap<>();finalintx;finalinty;finalintn;Knight(intx,inty,intn){this.x=x;this.y=y;this.n=n;}/* Enumerate all positions reachable by this Knight */publicList<Point>neighborsOf(Pointp){returnDirection.all().flatMap(d->travel(p,d)).filter(this::isValid).toList();}privateStream<Point>travel(Pointp,Directiond){returnStream.of(pointOf(p,d,x,y),pointOf(p,d,y,x));}privatePointpointOf(Pointp,Directionsign,intdx,intdy){Pointq=p.getLocation();q.translate(dx*sign.dx,dy*sign.dy);atlas.merge(q,atlas.get(p)+1,Math::min);returnq;}/* Represent the cardinal directions */privateenumDirection{U(1,1),D(-1,-1),L(-1,1),R(1,-1);finalintdx;finalintdy;privateDirection(intdx,intdy){this.dx=dx;this.dy=dy;}staticStream<Direction>all(){returnArrays.stream(Direction.values());}}privatebooleanisValid(Pointp){returnp.y<n&&p.x<n&&p.y>=0&&p.x>=0;}Set<Integer>homolog(){if(x==y)returnSet.of(x);returnSet.of(x,y);}}
KnightL on a Chessboard
You are viewing a single comment's thread. Return to all comments →
For something different, here's a 'supercomputer' solution in Java 17. This performs N-way search over all the knights at once, given N threads. Note it may run slower on single-thread hardware.
https://github.com/profinite/HackerRank/blob/main/Algorithms/Search/KnightOnAChessboard/Solution.java