Minimize steps for Knight to collect maximum points (Knight in Geekland)
Last Updated : 22 Aug, 2026
A knight is stationed at position (x, y) in Geekland, represented as an n×m matrix, where each cell holds some points.
At the i-th step, the knight can collect the total points from all cells reachable in exactly i knight-moves (moving the same way a knight moves on a chessboard), without revisiting any cell.
The knight also has a magical power to pull points from the future: if he collects y points at step x, he can absorb all the points collectable at step (x + y) as well, without spending an additional step.
If step (x + y) itself yields z points, he can further absorb step (x + y + z), and so on, while always remaining at step x and collecting the cumulative total of every step reachable this way.
Find the minimum step count that yields the maximum total points collectable this way.
Note: The knight moves exactly like a knight on a chessboard. Indexing is 0-based.
Examples:
Input: x = 2, y = 1, mat[][] = [[7,6,8], [9,1,4], [6,2,8]] Output: 0 Explanation:
At step 0, the knight is at (2,1) and has collected 2 points, the value of its own cell. At step 1, the knight can reach cells (0,0) and (0,2) in a single knight-move, worth 7 and 8 points, totaling 15. At step 2, cells (1,0) and (1,2) become reachable, worth 9 and 4 points, totaling 13. Since the knight has 2 points at step 0, its magical power lets it absorb step (0+2)=2's points as well, adding 13 to its total and giving 2+13=15 points, all while still standing at step 0. At step 1 alone, the knight already has 15 points, matching step 0's combined total. Since both step 0 and step 1 reach the same maximum of 15 points, and the problem asks for the smallest step count that achieves the maximum, the answer is 0.
Input: x = 0, y = 2, mat[][] = [[1,1,2,1,1], [1,3,1,3,1], [2,1,1,1,2], [1,3,1,3,1]] Output: 0 Explanation: The step-wise points are [2, 4, 15, 6, 4] for steps 0 through 4. At step 0, the knight collects 2 points and uses his magical power to jump to step (0+2)=2, collecting 15 more points, for a total of 17 - the maximum achievable, reached at the smallest step count of 0.
BFS Layering with Jump-Chain Aggregation - O(n*m) Time and O(n*m) Space
The idea is to perform a BFS from the knight's starting position to group cells by their minimum knight-move distance and compute the total points collected at each step. Then process these step totals from right to left to aggregate all points obtainable through the magical jump chain and return the earliest step with the maximum total.
Perform a BFS from the starting cell using knight moves, where each BFS level represents cells reachable in exactly that many moves.
Compute the total points of all cells in every BFS level and store them according to their corresponding step.
Traverse the step totals from the last step towards the first so that the result of every future jump is already available.
For each step, if jumping ahead by its collected points lands on a valid step, add the already aggregated points of that destination step.
Repeat this process until every step stores the maximum points obtainable when starting from that step.
Traverse the final aggregated values and return the smallest step number having the maximum total points.
C++
#include<bits/stdc++.h>usingnamespacestd;intdx[8]={-2,-1,1,2,2,1,-1,-2};intdy[8]={1,2,2,1,-1,-2,-2,-1};boolisSafe(inti,intj,intn,intm){returni>=0&&i<n&&j>=0&&j<m;}intknightInGeekland(intx,inty,vector<vector<int>>&mat){intn=mat.size();intm=mat[0].size();vector<vector<bool>>visited(n,vector<bool>(m,false));visited[x][y]=true;queue<pair<int,int>>q;q.push({x,y});vector<int>pointsPerStep;// collect total points reachable at each successive knight-move stepwhile(!q.empty()){intsize=q.size();intpoints=0;for(inti=0;i<size;i++){auto[cx,cy]=q.front();q.pop();points+=mat[cx][cy];for(intk=0;k<8;k++){intnx=cx+dx[k];intny=cy+dy[k];if(isSafe(nx,ny,n,m)&&!visited[nx][ny]){visited[nx][ny]=true;q.push({nx,ny});}}}pointsPerStep.push_back(points);}// apply the magical power: jump forward by the collected points at each stepfor(inti=pointsPerStep.size()-1;i>=0;i--){if(i+pointsPerStep[i]<(int)pointsPerStep.size())pointsPerStep[i]=pointsPerStep[i]+pointsPerStep[i+pointsPerStep[i]];}// find the smallest step achieving the maximum totalintmaxPoints=-1,ans=-1;for(inti=0;i<(int)pointsPerStep.size();i++){if(pointsPerStep[i]>maxPoints){maxPoints=pointsPerStep[i];ans=i;}}returnans;}intmain(){intx=0,y=2;vector<vector<int>>mat={{1,1,2,1,1},{1,3,1,3,1},{2,1,1,1,2},{1,3,1,3,1}};cout<<knightInGeekland(x,y,mat)<<endl;return0;}
Java
importjava.util.Queue;importjava.util.List;importjava.util.ArrayList;importjava.util.LinkedList;classGfG{staticint[]dx={-2,-1,1,2,2,1,-1,-2};staticint[]dy={1,2,2,1,-1,-2,-2,-1};staticbooleanisSafe(inti,intj,intn,intm){returni>=0&&i<n&&j>=0&&j<m;}staticintknightInGeekland(intx,inty,int[][]mat){intn=mat.length;intm=mat[0].length;boolean[][]visited=newboolean[n][m];visited[x][y]=true;Queue<int[]>q=newLinkedList<>();q.add(newint[]{x,y});List<Integer>pointsPerStep=newArrayList<>();// collect total points reachable at each successive knight-move stepwhile(!q.isEmpty()){intsize=q.size();intpoints=0;for(inti=0;i<size;i++){int[]cell=q.poll();intcx=cell[0],cy=cell[1];points+=mat[cx][cy];for(intk=0;k<8;k++){intnx=cx+dx[k];intny=cy+dy[k];if(isSafe(nx,ny,n,m)&&!visited[nx][ny]){visited[nx][ny]=true;q.add(newint[]{nx,ny});}}}pointsPerStep.add(points);}// apply the magical power: jump forward by the collected points at each stepfor(inti=pointsPerStep.size()-1;i>=0;i--){intval=pointsPerStep.get(i);if(i+val<pointsPerStep.size())pointsPerStep.set(i,val+pointsPerStep.get(i+val));}// find the smallest step achieving the maximum totalintmaxPoints=-1,ans=-1;for(inti=0;i<pointsPerStep.size();i++){if(pointsPerStep.get(i)>maxPoints){maxPoints=pointsPerStep.get(i);ans=i;}}returnans;}publicstaticvoidmain(String[]args){intx=0,y=2;int[][]mat={{1,1,2,1,1},{1,3,1,3,1},{2,1,1,1,2},{1,3,1,3,1}};System.out.println(knightInGeekland(x,y,mat));}}
Python
fromcollectionsimportdequedefisSafe(i,j,n,m):return0<=i<nand0<=j<mdefknightInGeekland(x,y,mat):n=len(mat)m=len(mat[0])dx=[-2,-1,1,2,2,1,-1,-2]dy=[1,2,2,1,-1,-2,-2,-1]visited=[[False]*mfor_inrange(n)]visited[x][y]=Trueq=deque([(x,y)])pointsPerStep=[]# collect total points reachable at each successive knight-move stepwhileq:size=len(q)points=0for_inrange(size):cx,cy=q.popleft()points+=mat[cx][cy]forkinrange(8):nx=cx+dx[k]ny=cy+dy[k]ifisSafe(nx,ny,n,m)andnotvisited[nx][ny]:visited[nx][ny]=Trueq.append((nx,ny))pointsPerStep.append(points)# apply the magical power: jump forward by the collected points at each stepforiinrange(len(pointsPerStep)-1,-1,-1):val=pointsPerStep[i]ifi+val<len(pointsPerStep):pointsPerStep[i]=val+pointsPerStep[i+val]# find the smallest step achieving the maximum totalmaxPoints=-1ans=-1foriinrange(len(pointsPerStep)):ifpointsPerStep[i]>maxPoints:maxPoints=pointsPerStep[i]ans=ireturnansx,y=0,2mat=[[1,1,2,1,1],[1,3,1,3,1],[2,1,1,1,2],[1,3,1,3,1]]print(knightInGeekland(x,y,mat))
C#
usingSystem;usingSystem.Collections.Generic;classGfG{staticint[]dx={-2,-1,1,2,2,1,-1,-2};staticint[]dy={1,2,2,1,-1,-2,-2,-1};staticboolisSafe(inti,intj,intn,intm){returni>=0&&i<n&&j>=0&&j<m;}staticintknightInGeekland(intx,inty,int[][]mat){intn=mat.Length;intm=mat[0].Length;bool[,]visited=newbool[n,m];visited[x,y]=true;Queue<(int,int)>q=newQueue<(int,int)>();q.Enqueue((x,y));List<int>pointsPerStep=newList<int>();// collect total points reachable at each successive knight-move stepwhile(q.Count>0){intsize=q.Count;intpoints=0;for(inti=0;i<size;i++){var(cx,cy)=q.Dequeue();points+=mat[cx][cy];for(intk=0;k<8;k++){intnx=cx+dx[k];intny=cy+dy[k];if(isSafe(nx,ny,n,m)&&!visited[nx,ny]){visited[nx,ny]=true;q.Enqueue((nx,ny));}}}pointsPerStep.Add(points);}// apply the magical power: jump forward by the collected points at each stepfor(inti=pointsPerStep.Count-1;i>=0;i--){intval=pointsPerStep[i];if(i+val<pointsPerStep.Count)pointsPerStep[i]=val+pointsPerStep[i+val];}// find the smallest step achieving the maximum totalintmaxPoints=-1,ans=-1;for(inti=0;i<pointsPerStep.Count;i++){if(pointsPerStep[i]>maxPoints){maxPoints=pointsPerStep[i];ans=i;}}returnans;}staticvoidMain(){intx=0,y=2;int[][]mat={newint[]{1,1,2,1,1},newint[]{1,3,1,3,1},newint[]{2,1,1,1,2},newint[]{1,3,1,3,1}};Console.WriteLine(knightInGeekland(x,y,mat));}}
JavaScript
functionisSafe(i,j,n,m){returni>=0&&i<n&&j>=0&&j<m;}functionknightInGeekland(x,y,mat){constn=mat.length;constm=mat[0].length;constdx=[-2,-1,1,2,2,1,-1,-2];constdy=[1,2,2,1,-1,-2,-2,-1];constvisited=Array.from({length:n},()=>newArray(m).fill(false));visited[x][y]=true;constq=[[x,y]];constpointsPerStep=[];// collect total points reachable at each successive knight-move stepwhile(q.length>0){constsize=q.length;letpoints=0;for(leti=0;i<size;i++){const[cx,cy]=q.shift();points+=mat[cx][cy];for(letk=0;k<8;k++){constnx=cx+dx[k];constny=cy+dy[k];if(isSafe(nx,ny,n,m)&&!visited[nx][ny]){visited[nx][ny]=true;q.push([nx,ny]);}}}pointsPerStep.push(points);}// apply the magical power: jump forward by the collected points at each stepfor(leti=pointsPerStep.length-1;i>=0;i--){constval=pointsPerStep[i];if(i+val<pointsPerStep.length)pointsPerStep[i]=val+pointsPerStep[i+val];}// find the smallest step achieving the maximum totalletmaxPoints=-1,ans=-1;for(leti=0;i<pointsPerStep.length;i++){if(pointsPerStep[i]>maxPoints){maxPoints=pointsPerStep[i];ans=i;}}returnans;}// Driver Codeconstx=0,y=2;constmat=[[1,1,2,1,1],[1,3,1,3,1],[2,1,1,1,2],[1,3,1,3,1]];console.log(knightInGeekland(x,y,mat));