Question

In: Computer Science

quiz 1 write your java code Have the function SearchingChallenge(strArr) read the array of strings stored...

quiz 1 write your java code Have the function SearchingChallenge(strArr) read the array of strings stored in strArr which will be a 4x4 matrix of the characters 'C', 'H', 'F', 'O', where C represents Charlie the dog, H represents its home, F represents dog food, and O represents and empty space in the grid. Your goal is to figure out the least amount of moves required to get Charlie to grab each piece of food in the grid by moving up, down, left, or right, and then make it home right after. Charlie cannot move onto the home before all pieces of food have been collected. For example: if strArr is ["FOOF", "OCOO", "OOOH", "FOOO"], then this looks like the following grid: For the input above, the least amount of steps where the dog can reach each piece of food, and then return home is 11 steps, so your program should return the number 11. The grid will always contain between 1 and 8 pieces of food. Examples Input: {"OOOO", "OOFF", "OCHO", "OFOO"} Output: 7 Input: {"FOOO", "OCOH", "OFOF", "OFOO"} Output: 10 ---------------------------------- you have the following code what edit on it import java.util.*; import java.io.*; class Main { public static String SearchingChallenge(String[] strArr) { // code goes here return strArr[0]; } public static void main (String[] args) { // keep this function call here Scanner s = new Scanner(System.in); System.out.print(SearchingChallenge(s.nextLine())); } }

Solutions

Expert Solution

Java code for above problem

import java.util.*;
import java.io.*;
class Main
{
   static int min_distance=0;
   static int x[]={-1,0,1,0};
   static int y[]={0,1,0,-1};
   public static String SearchingChallenge(String strArr)
   {
       min_distance=Integer.MAX_VALUE;
       String [] arr=strArr.split(" ");
       Point start=null,end=null;
       int food_count=0;
       int R=arr.length;
       int C=arr[0].length();
       boolean [][] visited=new boolean[R][C];
       for(int i=0;i<R;i++)
       {
           for(int j=0;j<C;j++)
           {
               if(arr[i].charAt(j)=='C')
                   start=new Point(i,j);
               else if(arr[i].charAt(j)=='H')
                   end=new Point(i,j);
               else if(arr[i].charAt(j)=='F')
                   food_count++;
           }
       }
       if(start==null || end==null)
           return String.valueOf(min_distance);
       min_path(arr,R,C,visited,start,end,food_count,0);
       return String.valueOf(min_distance);
   }
   public static void min_path(String arr[],int R,int C,boolean visited[][],Point start,Point end,int food_count,int path_count)
   {
       if(visited[start.i][start.j])
           return;
       if(start.i==end.i && start.j==end.j && food_count==0)
       {
           min_distance=Math.min(min_distance,path_count);
           return;
       }
       visited[start.i][start.j]=true;
       for(int a=0;a<4;a++)
       {
           int newX=start.i+x[a];
           int newY=start.j+y[a];
           if(newX>=0 && newX<R && newY>=0 && newY<C && !visited[newX][newY])
           {
               if(arr[newX].charAt(newY)=='F')
                   min_path(arr,R,C,visited,new Point(newX,newY),end,food_count-1,path_count+1);
               else
                   min_path(arr,R,C,visited,new Point(newX,newY),end,food_count,path_count+1);
           }
       }
       visited[start.i][start.j]=false;
   }
   public static void main (String[] args)
   {
       Scanner s = new Scanner(System.in);
       System.out.println(SearchingChallenge(s.nextLine()));
   }
}
class Point
{
   int i,j;
   Point(int i,int j)
   {
       this.i=i;
       this.j=j;
   }
   public String toString()
   {
       return i+" "+j;
   }
}

Sample output

(a):

if input is "OOOO OOFF OCHO OFOO", then output is 7.

(b):

if input is "FOOO OCOH OFOF OFOO", then output is 10.

Mention in comments if any mistakes or errors are found. Thank you.


Related Solutions

IN JAVA write a program that creates an array of strings with 8 people in it....
IN JAVA write a program that creates an array of strings with 8 people in it. Second,  Assign a random rank between 1 to 8 to each of the players. The rankings do not change throughout the tournament. Finally, Sort the players based on the rankings and print the data (show rankings of players, in square brackets, at every step after they are ranked). USING JAVA COLLECTIONS IS NOT ALLOWED
Seasons Re-Visited Write a program that contains an array of strings in main(). Your array of...
Seasons Re-Visited Write a program that contains an array of strings in main(). Your array of strings will contain the seasons, “Winter”, “Spring”, “Summer”, and “Fall”. Your program will then contain a function that will use your seasons array to display a menu and ask the user to choose their favorite season and return that value to the main() function. Your function must include a data validation loop. The user must enter in a 1 for winter, a 2 for...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output of longestMorseCodeWords should be an array of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically.For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] let words = ["gin", "zen", "gig", "msg"] longestMorseCodeWords(words) The Morse...
Write a java script function that accepts integer array as input, and display a new array...
Write a java script function that accepts integer array as input, and display a new array by performing fol lowing modifications, • The values in odd index positions must be incremented by 1 • The values in even index positions must be decremented by 1. • Assume the array index starts from 0. Input 1: arr = [1,2,3,4] Output 1: arr = [0,3,2,5 it done html and javascript and plz do it in simple way
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
1.Write the java code to create a two dimensional String array of sizes 12 by 8;...
1.Write the java code to create a two dimensional String array of sizes 12 by 8; Given the java array:       double[] test = new double[3]; 2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell. 3. Can you have different types of data is a three dimensional array? 4....
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in an array. NOT in linked List. Implement an array-based Linked List in your language. Use double as the item. You need to create a driver includes several items and inserts them in order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT