KnightL on a Chessboard


Problem Statement :


KnightL is a chess piece that moves in an L shape. We define the possible moves of  as any movement from some position  to some  satisfying either of the following:


Note that  and  allow for the same exact set of movements. For example, the diagram below depicts the possible locations that  or  can move to from its current location at the center of a  chessboard:


Observe that for each possible movement, the Knight moves  units in one direction (i.e., horizontal or vertical) and  unit in the perpendicular direction.


Input Format

A single integer denoting .

Constraints

Output Format

Print exactly  lines of output in which each line  (where ) contains  space-separated integers describing the minimum number of moves  must make for each respective  (where ). If some  cannot reach position , print -1 instead.



Solution :



title-img


                            Solution in C :

In  C++  :






#include<bits/stdc++.h>

using namespace std;

const int BIG = 2e9;
int n, d[50][50];

void can(queue<pair<int, int>> &q, int i, int j, int dd){
    if(i >= 0 && i < n && j >= 0 && j < n && d[i][j] > dd){
        d[i][j] = dd;
        q.push({i, j});
    }
}

int bfs(int n, int a, int b){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            d[i][j] = BIG;
        }
    }

    d[0][0] = 0;
    queue<pair<int, int>> q;
    q.push({0, 0});
    while(!q.empty()){
        pair<int, int> v = q.front();
        q.pop();
        int x = v.first;
        int y = v.second;
        int dist = d[x][y] + 1;
        can(q, x + a, y + b, dist);
        can(q, x + a, y - b, dist);
        can(q, x - a, y + b, dist);
        can(q, x - a, y - b, dist);
        can(q, x + b, y + a, dist);
        can(q, x + b, y - a, dist);
        can(q, x - b, y + a, dist);
        can(q, x - b, y - a, dist);
    }
    int ans = d[n - 1][n - 1];
    if(ans == BIG){
        return -1;
    }
    return ans;
}

int main(){
    cin >> n;
    for(int i = 1; i < n; i++){
        for(int j = 1; j < n; j++){
            cout << bfs(n, i, j) << " ";
        }
        cout << "\n";
    }
}










In   Java  :







import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        // your code goes here
        int[][] results = new int[n-1][n-1];
        
        for(int i = 1; i < n; i++){ //decres i and j when entering
            for(int j = 1; j < n; j++){
                int[][] T = new int[n][n];
                Queue<Tupel> q = new LinkedList<Tupel>();
                q.add(new Tupel(0,0));
                while(!q.isEmpty()){
                    Tupel t = q.remove();
                    
                    if(t.x == 0 && t.y == 0){
                        T[0][0] = 1;
                        
                    }
                    
                    if(t.x-i >= 0 && t.y-j >= 0 && T[t.x-i][t.y-j] == 0){
                        T[t.x-i][t.y-j] = T[t.x][t.y]+1;
                        q.add(new Tupel(t.x-i, t.y-j));
                    }
                        
                    if(t.x-j >= 0 && t.y-i >= 0 && T[t.x-j][t.y-i] == 0){
                        T[t.x-j][t.y-i] = T[t.x][t.y]+1;
                        q.add(new Tupel(t.x-j, t.y-i));
                    }
                        
                    if(t.x-i >= 0 && t.y+j < n && T[t.x-i][t.y+j] == 0){
                        T[t.x-i][t.y+j] = T[t.x][t.y]+1;
                        q.add(new Tupel(t.x-i, t.y+j));
                    }    
                        
                    if(t.x-j >= 0 && t.y+i < n && T[t.x-j][t.y+i] == 0){
                        T[t.x-j][t.y+i] = T[t.x][t.y]+1;
                        q.add(new Tupel(t.x-j, t.y+i));
                    }
                        
                    if(t.x+i < n && t.y+j < n && T[t.x+i][t.y+j] == 0){
                        T[t.x+i][t.y+j] = T[t.x][t.y]+1;
                        if(t.x+i == n-1 && t.y+j == n-1){
                            break;
                        }
                        q.add(new Tupel(t.x+i, t.y+j));
                    }
                        
                    if(t.x+j < n && t.y+i < n && T[t.x+j][t.y+i] == 0){
                        T[t.x+j][t.y+i] = T[t.x][t.y]+1;
                        if(t.x+i == n-1 && t.y+j == n-1){
                            break;
                        }
                        q.add(new Tupel(t.x+j, t.y+i));
                    }
                        
                    if(t.x+i < n && t.y-j >= 0 && T[t.x+i][t.y-j] == 0){
                        T[t.x+i][t.y-j] = T[t.x][t.y]+1;
                        q.add(new Tupel(t.x+i, t.y-j));
                    }
                        
                    if(t.x+j < n && t.y-i >= 0 && T[t.x+j][t.y-i] == 0){
                        T[t.x+j][t.y-i] = T[t.x][t.y]+1;
                        q.add(new Tupel(t.x+j, t.y-i));
                    }
                       
                }
                
                results[i-1][j-1] = T[n-1][n-1]-1;
            }
        }
        
        for(int i = 0; i < n-1; i++){
            for(int j = 0; j < n-1; j++){
                System.out.print(results[i][j] + " ");
            }
            System.out.println();
        }
    }
    
}

class Tupel{
    int x;
    int y;
    public Tupel(int x, int y){
        this.x = x;
        this.y = y;
    }
}










In  C  :








#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int n; 
int **checked;

typedef struct node{
    int i;
    int j;
    int g;
    int h;
    struct node *next;
}node_t;

node_t *head = NULL;
node_t *tail = NULL;


void insert(int i, int j, int g, int h){
    if(checked[i][j]==1) return;
    //printf("Insert: %d %d\n",i,j);
    checked[i][j] = 1;
    node_t *newnode = (node_t*)malloc(sizeof(node_t));
    newnode->i = i;
    newnode->j = j;
    newnode->g = g;
    newnode->h = h;
    newnode->next = NULL;
    if(head==NULL){
        head=newnode;
    }
    else{
        tail->next=newnode;
    }
    tail=newnode;
}

int isEmpty(){
    if(head==NULL){
        return 1;
    }
    else return 0;
}

node_t *dequeue(){
    node_t *ret;
    ret = head;
    head = head->next;
    return ret;
}

void testone(int i, int j,int g){
    if(i<0) return;
    if(i>=n) return;
    if(j<0) return;
    if(j>=n) return;
    
    insert(i,j,g,0);
}

void initchecked(){
    int i,j;
    for(i=0; i<n; i++){
        for(j=0; j<n; j++){
            checked[i][j] = 0;
        }
    }
}

void Astro(int a, int b){
    node_t *node;
    
    tail = NULL;
    head = NULL;
    
    initchecked();
    insert(0,0,0,0);
    while(!isEmpty()){
        node = dequeue();
        //printf("Test: %d %d\n",node->i, node->j);
        if(node==NULL){
            printf("-1 ");
            return ;
        }
        else if(node->i==n-1 && node->j==n-1){
            printf("%d ",node->g);
            return ;
        }
        
        testone(node->i - b, node->j + a, node->g+1);
        testone(node->i - a, node->j + b, node->g+1);
        testone(node->i + a, node->j + b, node->g+1);
        testone(node->i + b, node->j + a, node->g+1);
        
        
        testone(node->i - b, node->j - a, node->g+1);
        testone(node->i - a, node->j - b, node->g+1);
        testone(node->i + a, node->j - b, node->g+1);
        testone(node->i + b, node->j - a, node->g+1);
    }
    printf("-1 ");
}

int main(){
    scanf("%d",&n);
    checked = (int **)malloc(n*sizeof(int *));
    for(int i=0; i<n; i++){
        checked[i] = (int *)malloc(n*sizeof(int *));
    }
    
    for(int i=1; i<=n-1; i++){
        for(int j=1; j<=n-1; j++){
            Astro(i,j);
        }
        printf("\n");
    }
    return 0;
}








In  Python3 :








#!/bin/python3

import sys


n = int(input().strip())
# your code goes here
A=[]
for a in range(1,n):
    B=[]
    for b in range(1,n):
        P={(0,0)}
        c=0
        f=True
        V=set()
        while f:
            c+=1
            Q=set()
            
            V=V|P
            for p in P:
                if (p[0]==n-1)&(p[1]==n-1):
                    f=False
                    break
                if (0<=p[0]+a<n)&(0<=p[1]+b<n):
                    Q.add((p[0]+a,p[1]+b))
                if (0<=p[0]-a<n)&(0<=p[1]-b<n):
                    Q.add((p[0]-a,p[1]-b))
                if (0<=p[0]-a<n)&(0<=p[1]+b<n):
                    Q.add((p[0]-a,p[1]+b))
                if (0<=p[0]+a<n)&(0<=p[1]-b<n):
                    Q.add((p[0]+a,p[1]-b))       
                    
                if (0<=p[0]+b<n)&(0<=p[1]+a<n):
                    Q.add((p[0]+b,p[1]+a))
                if (0<=p[0]-b<n)&(0<=p[1]-a<n):
                    Q.add((p[0]-b,p[1]-a))
                if (0<=p[0]-b<n)&(0<=p[1]+a<n):
                    Q.add((p[0]-b,p[1]+a))
                if (0<=p[0]+b<n)&(0<=p[1]-a<n):
                    Q.add((p[0]+b,p[1]-a))                    

                    
            
            P=set()
            P=P|Q
            P=P-V
            if len(P)==0:
                break
        if not f:
            B.append(c-1)
        else:
            B.append(-1)
    A.append(B)
for a in A:
    for b in a:
        print(b,end= " ")
    print("")
                        








View More Similar Problems

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →