Skip to main content

Number of cells a queen can move with obstacles on the chessborad

Consider a N X N chessboard with a Queen and K obstacles. The Queen cannot pass through obstacles. Given the position (x, y) of Queen, the task is to find the number of cells the queen can move.
Examples:
Input : N = 8, x = 4, y = 4, 
        K = 0
Output : 27


Input : N = 8, x = 4, y = 4, 
        K = 1, kx1 = 3, ky1 = 5
Output : 24

//JAVA CODE

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    static int minimum_no(int a,int b){
        if(a>=b)
            return b;
        return a;
    }
    static int totalsteps(int n,int k,int x,int y,int[]kx1,int[]ky1){
        int up= n-x;
        int down=x-1;
        int left=y-1;
        int right=n-y;

        int left_diagnoalup=minimum_no(n-x,y-1);
        int left_diagnoaldown=minimum_no(x-1,y-1);
        int right_diagonalup=minimum_no(n-x,n-y);
        int right_diagonaldown=minimum_no(x-1,n-y);

         //obstacles        for(int i=0;i<k;i++){
            if(x>kx1[i] && y<ky1[i]){
                int rd=minimum_no(kx1[i]-1,n-ky1[i] ) +1;
                right_diagonaldown -=minimum_no(left_diagnoaldown,rd);
            }
            if(x<kx1[i] && y<ky1[i]){
                int ru=(minimum_no(n-kx1[i],n-ky1[i]) + 1);
                right_diagonalup -=minimum_no(ru,right_diagonalup);
            }
            if(x<kx1[i] && y>ky1[i]){
                int lu=(minimum_no(n-kx1[i],ky1[i]-1) +1);
                left_diagnoalup -=minimum_no(lu,left_diagnoalup);
            }
            if(x>kx1[i] && y>ky1[i]){
                int ld= minimum_no(kx1[i]-1,ky1[i]-1) +1;
                left_diagnoaldown-=minimum_no(ld,left_diagnoaldown);
            }
            if(x==kx1[i] && y>ky1[i])
                left-=ky1[i];
            if(x==kx1[i] && y<ky1[i])
                right=(ky1[i]-y-1);
            if(y==ky1[i] && x<kx1[i])
                up=(kx1[i]-x-1);
            if(y==ky1[i] && x>kx1[i])
                down-=kx1[i];   
        }
        return up+down+left_diagnoaldown+left_diagnoalup+left+right+right_diagonaldown+right_diagonalup;
    }
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        int x=sc.nextInt();
        int y=sc.nextInt();
        int[]kx1= {3};
        int[]ky1={5};
        System.out.println(totalsteps(n,k,x,y,kx1,ky1));
    }
}

Comments

Post a Comment

Popular posts from this blog

zoho taxi question (3rd Round)

Taxi application Design a Call taxi booking application -There are n number of taxi’s. For simplicity, assume 4. But it should work for any number of taxi’s. -The are 6 points(A,B,C,D,E,F) -All the points are in a straight line, and each point is 15kms away from the adjacent points. -It takes 60 mins to travel from one point to another -Each taxi charges Rs.100 minimum for the first 5 kilometers and Rs.10 for the subsequent kilometers. -For simplicity, time can be entered as absolute time. Eg: 9hrs, 15hrs etc. -All taxi’s are initially stationed at A. -When a customer books a Taxi, a free taxi at that point is allocated -If no free taxi is available at that point, a free taxi at the nearest point is allocated. -If two taxi’s are free at the same point, one with lower earning is allocated -Note that the taxi only charges the customer from the pickup point to the drop point. Not the distance it travels from an adjacent point to pickup the customer. -If no taxi is free a...

Inser 0 after Consecutive (K times) Of 1 is Found

Example: Input: Number of bits: 12 Bits: 1 0 1 1 0 1 1 0 1 1 1 1 Consecutive K: 2 Output: 1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 0 //JAVA CODE import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System. in ); int n = sc.nextInt(); int k=sc.nextInt(); int []a = new int [n]; int temp= 0 ; //Inputs for ( int i= 0 ;i<n;i++) a[i]=sc.nextInt(); //print for ( int i= 0 ;i<n;i++){ System. out .print(a[i]+ " " ); if (a[i]== 1 ){ temp++; if (temp==k) { System. out .print( "0 " ); temp= 0 ; } } else temp= 0 ; } } }

QUESTION You’re given an even number n. If n=4, you have to print the following pattern : 4444 4334 4334 4444 If n=6, then the pattern should be like this : 666666 655556 654456 654456 655556 666666

// JAVA CODE import java.util.*; import java.lang.*; import java.io.*; class zohoquestion{     public static void main(String args[]) {         Scanner sc= new Scanner(System.in);         int n=sc.nextInt();int limit=0;         int s=n;         int a[][]= new int[n][n];         int l=0;int r=n-1;         while(limit<n){             for(int i=l;i<=r;i++){                 for(int j=l;j<=r;j++)                     if(i==l || i==r || j==l || j==r)                     a[i][j]=n;             }             l++;r--;n--;limit++;         }         for(int i=0;i<s;i++){ ...