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 :
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
Array Manipulation
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu
View Solution →Print the Elements of a Linked List
This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode
View Solution →Insert a Node at the Tail of a Linked List
You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink
View Solution →Insert a Node at the head of a Linked List
Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below
View Solution →Insert a node at a specific position in a linked list
Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e
View Solution →Delete a Node
Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo
View Solution →