Question

Youre given a chess board with dimension n x n. Theres a king at the bottom right square of the board marked with s. The ki

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class ChessGame {

        static Scanner in = new Scanner(System.in);
        
        String chessBoard[][];
        int size;
        
        int maxPoints = 0;
        int maxPointsRoutes = 0;
        
        Integer solutions[][];
        
        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);
                }
                if(isValidCell(startR - 1, startC - 1)) {
                        findSolutionHelper(startR - 1, startC, pointOfCell + points);
                }
        }

        private void findSolution() {
                solutions = new Integer[size][size];
                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. Thanks!

Add a comment
Know the answer?
Add Answer to:
You're given a chess board with dimension n x n. There's a king at the bottom...
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
  • 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...

  • King in N moves. In chess, a king can move one space in any of the...

    King in N moves. In chess, a king can move one space in any of the 8 directions (2 vertical, 2 horizontal, 4 diagonals). Suppose our chessboard is infinite in every direction, and a king starts on a particular square. (a) How many different squares are possible locations for the king after N or fewer moves? (b) Answer the same question for a “crippled king” that can only move horizontally and vertically (no diagonal moves allowed). For both parts, show...

  • Things are tense on the battlefield and our army is on the verge of defeat. The only chance is to...

    Programming language is Java 8 Thank you Things are tense on the battlefield and our army is on the verge of defeat. The only chance is to retreat for now, regroup, and then initiate another attack. To do this however, the king must be rescued first. A brave soldier named S wants to rescue the king. S wants to get to the king as fast as possible while avoiding the obstacles on the field such as pits, trees, bodies, etc....

  • Rooks can move any number of squares horizontally or vertically on a chess board. The n...

    Rooks can move any number of squares horizontally or vertically on a chess board. The n rooks problem is to arrange rooks on an n×n board in such a way that none of the rooks could bump into another by making any of its possible horizontal or vertical moves. For this problem, the variables are each column (labeled 0, 1, ... , n−1), the the domain consists of each possible row (also labeled 0, 1, ... ,n−1). In each column...

  • 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...

  • Concepts: multi-dimension array and the Knight's Tour Problem A chess board consists of an 8 x 8 "array" of squares: int board[ROW][COL]={0}; A knight may move perpendicular to the edges o...

    Concepts: multi-dimension array and the Knight's Tour Problem A chess board consists of an 8 x 8 "array" of squares: int board[ROW][COL]={0}; A knight may move perpendicular to the edges of the board, two squares in any of the 4 directions, then one square at right angles to the two-square move. The following lists all 8 possible moves a knight could make from board [3][3]: board[5][4] or board[5][2] or board[4][5] or board[4][1] or board[1][4] or board[1][2] or board[2][5] or board[2][1]...

  • 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...

  • 5: Legend has it that the game of chess was invented for the amusement of a...

    5: Legend has it that the game of chess was invented for the amusement of a Persian shah - or an Indian maharajah, or a Chinese emperor - who became so enthusiastic that he wanted to reward the inventor, who desired only one grain of wheat on the first square of the chessboard, two grains on the second square, four on the third, and so on, doubling the number of grains for each successive square on the 64-square chessboard. (From...

  • plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints...

    plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints out the total number of tree arcs and back arcs resulting from the traversal. Use our standard convention that when there is a choice of white or grey nodes, the one with the lowest index should be chosen. Input format:...

  • Under the standard (x, y)-coordinate system, a grid of points with integer coordinates is given i...

    Under the standard (x, y)-coordinate system, a grid of points with integer coordinates is given in the picture below. Here, the bottom-left point is the origin (0, 0) and the topright point has coordinate (6, 3). The coordinates of the remaining points can easily be computed accordingly. 2 During the 74th Hunger Game, Katniss and Peeta are separated inside a grid maze. Katniss is currently at the point (0, 0) and Peeta is (badly injured) at the point (x, y)...

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