Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a program in java: The Problem: Given the initial state of the 8 by 8 grid, showing the initial locations of the drones, the
Write a program in java:
The Problem:
Given the initial state of the by grid, showing the initial locations of the drones, the locations
each of the drones must deliver their food and the nofly zone squares, determine the fewest
number of remote control button presses necessary to have all of the drones successfully deliver
their food. If this is impossible, determine that the task is not possible.
The entire by grid is surrounded by nofly zone squares not drawn in but just assumed to
be there.
Two drones may occupy the same grid square at the same time so long as its not a nofly zone
square.
A drone treats each groups location except for the one its delivering to as a nofly zone. Therefore, a drone can not fly through another drone's dropoff location and treats it like a nofly zone. For example, D cannot got through G
The drones are controlled by one remote so they must move simultaneously. For example, when the down is pressed, all drones on the grid move down by one.
The Input:
The first line of input contains a single integer, n n representing the number of drones
for the input case. The corresponding drones are labeled D through Dn and the corresponding
groups to which they are delivering food are labeled G through Dn
The following eight lines each contain space separated strings of two characters each,
representing that row of the input grid. If the two characters are the grid square is an open
square. If the characters start with D then that represents
drone's starting position. If the the character starts with G then that represents the destination drone is delivering food. Finally, if the two characters are XX that means the corresponding grid square is a nofly zone. These are capital letter Xs
The Output:
Output a single integer: if its impossible to get the drones to all make their delivering or a
positive integer representing the fewest number of remote control button pushes necessary to get
the drones to all make their deliveries.
Additionally, please use bitmasking to solve as described in this example:
The key information we need to know about the state is the location of each drone. Each drone has
a row number to and a column number to We can store a row number using three bits
and a column number using three bits. Thus, the position of one drone can be stored as a single
integer in between and which takes up bits. If we have n drones, we can simply use a single
integer that has n bits. Thus, if we have four drones, an integer which uses the least significant
bits in between and
suffices to completely store the state of the board.
For example, for the first sample, the first drone is at row column which is equal to position
x the second drone is at row column position x and the third
drone is at row column position x This means the integer storing the initial
position of the board is:
In binary, this number is:
Drone is highlighted in purple, drone is highlighted in blue and drone is highlighted in yellow.
Its more natural to think about them as drone drone and drone Notice that the bits in
purple, when split into groups of three convert to the bits in blue when split into convert
to and the bits in yellow when split into groups of convert to So the BFS then starts
at board position for the first sample.
In the BFS for each possible board state integer well store the distance number of remote
control moves, from the initial position to that board state. Since we can make an array of size
well store our distances in an array of size n where n is the number of drones for the input case.
Thus, distance in the beginning and distancex for all other values of x in the
beginning of the BFS for the first sample input. Eventually, the distance array will fill with values
etc. When the final board position of is reached, the
BFS can return the corresponding shortest distance.Sample Input
Sample Output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started