Question

Please write code in java You’re given a chess board with dimension n x n. There’s...

Please write code in java

You’re given a chess board with dimension n x n. There’s a king at the bottom right square of the board marked with s. The king needs to reach the top left square marked with e. The rest of the squares are labeled either with an integer p (marking a point) or with x marking an obstacle. Note that the king can move up, left and up-left (diagonal) only. Find the maximum points the king can collect and the number of such paths the king can take in order to do so. Input Format The first line of input consists of an integer t. This is the number of test cases. Each test case contains a number n which denotes the size of board. This is followed by n lines each containing n space separated tokens. Output Format For each case, print in a separate line the maximum points that can be collected and the number of paths available in order to ensure maximum, both values separated by a space. If e is unreachable from s, print 0 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

37 else if(p0ints == maxPoints) t <terminated> ChessGame [J maxPointsRoutes +1; 38 39 40 return; e 2 3 41 2 x 2 1 2 s 42 if (

import java.util.Scanner;

public class ChessGame {

        static Scanner in = new Scanner(System.in);
        
        String chessBoard[][];
        int size;
        
        int maxPoints = 0;
        int maxPointsRoutes = 0;
        
        public ChessGame() {
                size = Integer.parseInt(in.nextLine());
                chessBoard = new String[size][size];
                
                for(int i=0; i<size; i++) {
                        chessBoard[i] = new String[size];
                        String line = in.nextLine();
                        String tokens[] = line.split(" ");
                        for(int j=0; j<size; j++) {
                                chessBoard[i][j] = tokens[j];
                        }
                }
        }
        
        private boolean isValidCell(int r, int c) {
                return (r >= 0) && (r < size) && (c >= 0) && (c < size);
        }
        
        private void findSolutionHelper(int startR, int startC, int points) {
                
                if(chessBoard[startR][startC].equals("e")) {
                        if(points > maxPoints) {
                                maxPoints = points;
                                maxPointsRoutes = 1;
                        } else if(points == maxPoints) {
                                maxPointsRoutes += 1;
                        }
                        return;
                }

                if(chessBoard[startR][startC].equals("x")) {
                        // route not possible
                        return;
                }
                
                int pointOfCell = 0;

                if(!chessBoard[startR][startC].equals("s")) {
                        pointOfCell = Integer.parseInt(chessBoard[startR][startC]);
                }
                
                if(isValidCell(startR, startC - 1)) {
                        findSolutionHelper(startR, startC - 1, pointOfCell + points);
                }
                if(isValidCell(startR - 1, startC)) {
                        findSolutionHelper(startR - 1, startC, pointOfCell + points);
                }
        }

        private void findSolution() {
                maxPoints = 0;
                maxPointsRoutes = 0;
                findSolutionHelper(size-1, size-1, 0);
                System.out.println(maxPoints + " " + maxPointsRoutes);
        }
        
        public static void main(String[] args) {
                int numTestCases = Integer.parseInt(in.nextLine());
                
                while(numTestCases-- > 0) {
                        new ChessGame().findSolution();
                }
                
        }

}


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Please write code in java You’re given a chess board with dimension n x n. There’s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You're given a chess board with dimension n x n. There's a king at the bottom...

    You're given a chess board with dimension n x n. There's a king at the bottom right square of the board marked with s. The king needs to reach the top left square marked with e. The rest of the squares are labeled either with an integer p (marking a point) or with x marking an obstacle. Note that the king can move up, left and up-left (diagonal) only. Find the maximum points the king can collect and the number...

  • please use JAVA to solve it. Checkers is played on an N XN checker board, with...

    please use JAVA to solve it. Checkers is played on an N XN checker board, with one side playing the black pieces and the ofther side playing the white pieces. Glven a board size N and the positions of the black and white checkers, create a program to print out the resulting checker board. INPUT Row, Column Integer N denoting the size of the board Integer B denoting the number of black pieces to be placed on the board, followed...

  • Java problem In this problem, the first input is a positive integer called n that will...

    Java problem In this problem, the first input is a positive integer called n that will represent the number of lines to process. The lines to be processed have one or more integers separated by whitespaces. For each of these lines, you must output: The minimum value of the integers The maximum value of the integers The sum of the integers It is worth to mention that the number of integers of each line is not known a priori and...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end show the exact Output that's shown in the Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int...

  • Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 {    public static void main( String [] args )    {        //N denoting the size of the board        int n;       ...

  • Must be done in Java. PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL....

    Must be done in Java. PROBLEM 2 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. PROBLEM 1 INFORMATION IF YOU NEED IT AS WELL: Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem3. CODE PROVIDED FOR PROBLEM 2: import java.util.Scanner; public class Problem2 { public static void main( String [] args ) { //N...

  • Please code in java There are N employees already working for a company and M new...

    Please code in java There are N employees already working for a company and M new candidates eligible for the work. At the end of the financial year, each of the N employees demand for an increment. You are provided with the data of current_salary and the salary he/she expects. Also, there are M candidates we can recruit. We have the data of salary demanded by each eligible candidate. The company can spend maximum of X units of money. Now...

  • A company wants to invest into building a golf course. They could choose the land to...

    A company wants to invest into building a golf course. They could choose the land to build the course from a rectangle area of size MxN. The rectangle area is divided into MxN cells. They measured the elevation of each cell and recorded it into an integer number. The company decided that the golf course should be a KxK square (K ≤ min(M,N)). Thus, there are (M-K+1)(NK+1) possible KxK squares that can be used to build the golf course. For...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • Java Programming Rash is known about his love for racing sports. He is an avid Formula...

    Java Programming Rash is known about his love for racing sports. He is an avid Formula 1 fan. He went to watch this year's Indian Grand Prix at New Delhi. He noticed that one segment of the circuit was a long straight road. It was impossible for a car to overtake other cars on this segment. Therefore, a car had to lower down its speed if there was a slower car in front of it. While watching the race, Rash...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT