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

write a java prog that include an array of strings of size 50    String[] strings...
write a java prog that include an array of strings of size 50    String[] strings = new String[50]; then find the max length of the inputs inside a method keep reading from the user until user enters keyword to stop input : may ala jony ram asd fgghff daniel jwana output : max length : 10
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
Java Write a method that removes duplicates from an array of strings and returns a new...
Java Write a method that removes duplicates from an array of strings and returns a new array, free of any duplicate strings.
Write a MARIE assembly language program that will read an “array” of positive decimal values stored...
Write a MARIE assembly language program that will read an “array” of positive decimal values stored in memory and output the smallest value. Use variable addr to store the location of the first data item. Use variable length to store the number of items in your array. Your code should be organized such that adding an additional array item would only involve adding the data line (ie. 021 dec 400) and updating the length variable (ie. length, dec 5). You...
given an array, write code to scan the array for a particular purpose . use java
given an array, write code to scan the array for a particular purpose . use java
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...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of characters using Caesar cipher please attach samples run of the code #include <iostream> #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string> #define MAX_SIZE 200 void encrypt_str(char xyz[], int key); // Function prototype for the function to encrypt the input string. void decrypt_str(char xyz[], int key); // Function prototype for the function to decrypt the encrypted string. using namespace std; int main() { char input_str[MAX_SIZE];...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT