Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; public class Solution { // Complete the findSquashes function below. static int findSquashes(int[][] plotDims, int[][]

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
// Complete the findSquashes function below.
static int findSquashes(int[][] plotDims, int[][] thorns, int[][] squashes, int[][] passages, int[] jump, int[] start) {
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = scanner.nextInt();
scanner.skip("( |[ \u2028\u2029\u0085])?");
// Read in plot dimensions: n lines of (r, c)
int[][] plotDims = new int[n][2];
for (int plotDimsRowItr = 0; plotDimsRowItr
String[] plotDimsRowItems = scanner.nextLine().split(" ");
scanner.skip("( |[ \u2028\u2029\u0085])?");
for (int plotDimsColumnItr = 0; plotDimsColumnItr
int plotDimsItem = Integer.parseInt(plotDimsRowItems[plotDimsColumnItr]);
plotDims[plotDimsRowItr][plotDimsColumnItr] = plotDimsItem;
}
}
int t = scanner.nextInt();
scanner.skip("( |[ \u2028\u2029\u0085])?");
// Read thorn locations: t lines of (i, x, y)
int[][] thorns = new int[t][3];
for (int thornsRowItr = 0; thornsRowItr
String[] thornsRowItems = scanner.nextLine().split(" ");
scanner.skip("( |[ \u2028\u2029\u0085])?");
for (int thornsColumnItr = 0; thornsColumnItr
int thornsItem = Integer.parseInt(thornsRowItems[thornsColumnItr]);
thorns[thornsRowItr][thornsColumnItr] = thornsItem;
}
}
int s = scanner.nextInt();
scanner.skip("( |[ \u2028\u2029\u0085])?");
// Read squash locations: s lines of (i, x, y, q)
int[][] squashes = new int[s][4];
for (int squashesRowItr = 0; squashesRowItr
String[] squashesRowItems = scanner.nextLine().split(" ");
scanner.skip("( |[ \u2028\u2029\u0085])?");
for (int squashesColumnItr = 0; squashesColumnItr
int squashesItem = Integer.parseInt(squashesRowItems[squashesColumnItr]);
squashes[squashesRowItr][squashesColumnItr] = squashesItem;
}
}
int m = scanner.nextInt();
scanner.skip("( |[ \u2028\u2029\u0085])?");
// Read passages m lines of ((i, x_i, y_i), (j, x_j, y_j))
int[][] passages = new int[m][6];
for (int passagesRowItr = 0; passagesRowItr
String[] passagesRowItems = scanner.nextLine().split(" ");
scanner.skip("( |[ \u2028\u2029\u0085])?");
for (int passagesColumnItr = 0; passagesColumnItr
int passagesItem = Integer.parseInt(passagesRowItems[passagesColumnItr]);
passages[passagesRowItr][passagesColumnItr] = passagesItem;
}
}
// Read in jump parameters a, b
int[] jump = new int[2];
String[] jumpItems = scanner.nextLine().split(" ");
scanner.skip("( |[ \u2028\u2029\u0085])?");
for (int jumpItr = 0; jumpItr
int jumpItem = Integer.parseInt(jumpItems[jumpItr]);
jump[jumpItr] = jumpItem;
}
// Read in starting point (pp, px, py)
int[] start = new int[3];
String[] startItems = scanner.nextLine().split(" ");
scanner.skip("( |[ \u2028\u2029\u0085])?");
for (int startItr = 0; startItr
int startItem = Integer.parseInt(startItems[startItr]);
start[startItr] = startItem;
}
int result = findSquashes(plotDims, thorns, squashes, passages, jump, start);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Hop Squash: Mr. McGregor Fights Back Peter Rabbit has found himself back in Mr. McGregor's garden (againl) but to his surprise, Mr. McGregor has made some changes after Peter's last raid. Instead of being organised in a single grid, the garden has now been partitioned into n plots, and each is organised as a grid of vegetable patches, with its own fence surrounding it The fencing is too high for Peter to jump over. Moreover, Mr. McGregor has cultivated patches of thomy roses at certain cells of the grid. These would be very painful for Peter to land on when he jumps, so he must avoid landing on those cells at all costs. While these interventions from Mr. McGregor are potent deterrents, his famous Butternut Squash too tasty to keep many a brave rabbit from trying to circumvent them As a consequence, a whole network of tunnels now exists beneath the plots, allowing Peter to burrow his way between the enclosed plots The burrows open up as holes in the plots. A hole will always be located in a vegetable patch (never in a thomy rose bush). Each cell may contain at most one hole. The passages beneath the holes may branch multiple times. Therefore it is possible fora hole in one garden to lead to one or more holes in other plots A single plot may contain multiple holes, and it is possible that a hole in a plot leads to another hole within the same plot. Plots are numbered from 1 to n. The i plot has r, rows and c, columns. Each cell is uniquely defined by three integers: d x y) to indicate that it is located in garden i at row x and column y. The positions x and y are always relative to the origin (top left corner) of plot i, and are 0-based. (Increasing coordinates are to the right and down) Coordinates like these are used to describe the locations of the holes, and the cells growing butternut squash and thorny roses. The locations of the butternut squash cells are presented along with a 4th integer to indicate the number of butternut squashes grown at that cell. When Peter jumps, he moves a cells in one direction and b cells in an orthogonal direction (ie. at right angles to the original movement). If his landing square would either be on a rose bush or outside of the plot that he is currently in, then he does not make the jump in that direction. In order for Peter to burrow, he must be located at a cell that contains a hole, and after he finishes moving, he will be at a hole on the other end of the burrow that he entered. Given a description of the dimensions of the plots, the burrows between them, the quantity and location of all the butternut squashes in the gardens, Peter's starting location and his jump characteristics, determine the maximum number of butternut squashes that he could collect. Input Format Line 1 contains a single integer n the number of plots The next n lines each contains a pair of integers

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

11. Store this politicalecological data in the data sets directory.

Answered: 1 week ago