Solver.java
package hw7;
import java.util.Iterator;
import edu.princeton.cs.algs4.Graph;
import edu.princeton.cs.algs4.BreadthFirstPaths;
public class Solver {
public static String solve(char[][] grid) {
// TODO
/*
* 1. Construct a graph using
grid
* 2. Use BFS to find shortest path
from start to finish
* 3. Return the sequence of moves
to get from start to finish
*/
// Hardcoded solution to
toyTest
return "RRRDDDDDDDDDRRRRRR";
}
}
ToyTest.java
package hw7;
import static org.junit.Assert.*;
import org.junit.Test;
public class ToyTest {
private void checkSol(char[][] grid, String path, int
distance) {
int size = grid.length;
int row = -1;
int col = -1;
// Start at s;
for(int i = 0; i < size; i++)
{
for(int j = 0; j
< size; j++) {
if (grid[i][j] == 's') {
row = i;
col = j;
}
}
}
// Move according to path
for(char c : path.toCharArray())
{
switch(c)
{
case 'U':
row -= 1;
break;
case 'D':
row += 1;
break;
case 'R':
col += 1;
break;
case 'L':
col -= 1;
break;
default:
fail("Illegal character in solution: " +
c);
}
// Make sure you
haven't moved outside the grid.
assertTrue(row
>= 0 && row < size && col >= 0 &&
col < size);
// Make sure you
haven't moved into a '*'
assertTrue(grid[row][col] != '*');
}
// Make sure you end at 'f'
assertTrue(grid[row][col] ==
'f');
// Make sure it is not longer than
shortest distance.
assertTrue(path.length() <=
distance);
}
@Test
public void toyTest() {
String[] data =
{
"s ",
" ",
" * ",
" * *****",
" * ",
" * ",
" * ",
" * ",
" * ",
" * f"
};
char[][] grid;
/* Solution to this grid is
hardcoded */
grid =
GridUtilities.fromStringArray(data);
String solution =
Solver.solve(grid);
checkSol(grid, solution, 18);
/* Hardcoded solution does not
solve this grid */
grid =
GridUtilities.rotateClockwise(grid);
solution =
Solver.solve(grid);
checkSol(grid, solution, 18);
}
}
GridUtilties.java
package hw7;
import java.io.*;
import java.util.ArrayList;
public class GridUtilities {
public static char[][] fromStringArray(String[] in)
{
int size = in.length;
char[][] result = new
char[size][];
for(int i = 0; i < size; i++)
{
String row =
in[i];
if (row.length()
!= size)
throw new RuntimeException(String.format("Row %d
and col 0 have different sizes", i));
result[i] =
row.toCharArray();
}
return result;
}
public static char[][] fromFile(String filename)
throws IOException {
try (BufferedReader in = new
BufferedReader(new FileReader(filename))) {
String row =
in.readLine();
ArrayList<String> temp = new
ArrayList<String>(row.length());
while (row !=
null) {
temp.add(row);
row = in.readLine();
}
String[] table =
temp.toArray(new String[0]);
return
fromStringArray(table);
}
}
public static char[][] rotateClockwise(char[][] in)
{
int size = in.length;
char[][] out = new
char[size][size];
for(int row = 0; row < size;
row++)
for(int col = 0;
col < size; col++)
out[row][col] = in[size-1-col][row];
return out;
}
public static String toString(char[][] grid) {
StringBuilder sb = new
StringBuilder(grid.length * (grid.length+5));
for(char[] row : grid) {
sb.append(new
String(row));
sb.append('\n');
}
return sb.toString();
}
}
I have implemented source code :
I just implemented Source-code it will work based on that u can write code
Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver { public static...
import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>(); // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED"); Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } } for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...
package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private static int cardNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private static char suitName[] = { 'c', 'd', 'h', 's' }; public static void main(String[] args) throws CardException, DeckException, HandException { Scanner kb = new Scanner(System.in); System.out.println("How many Players? "); int numHands = kb.nextInt(); int cards = 0; if (numHands > 0) { cards = 52 / numHands; System.out.println("Each player gets " + cards + " cards\n"); } else...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 { public static void main (String [] args) { // ============================================================ // Step 2. Declaring Variables You Need // These constants are used to define 2D array and loop conditions final int NUM_ROWS = 4; final int NUM_COLS = 3; String filename = "Input.txt"; // A String variable used to save the lines read from input...
This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...
Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggling with a part of a question assigned by the professor. He gave us this code to fill in: import java.util.Arrays; import java.util.Random; public class RobotGo { public static Random r = new Random(58); public static void main(String[] args) { char[][] currentBoard = createRandomObstacle(createBoard(10, 20), 100); displayBoard(startNavigation(currentBoard)) } public static int[] getRandomCoordinate(int maxY, int maxX) { int[] coor = new...
*JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> { private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10; public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0;...
import java.util.Scanner; public class Client{ public static void main(String args[]){ Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5); Scanner keyboard = new Scanner(System.in); int i = 0; int total = 0; while(true){ i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue(); dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...
Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections; class Grid { private boolean bombGrid[][]; private int countGrid[][]; private int numRows; private int numColumns; private int numBombs; public Grid() { this(10, 10, 25); } public Grid(int rows, int columns) { this(rows, columns, 25); } public Grid(int rows, int columns, int numBombs) { this.numRows = rows; this.numColumns = columns; this.numBombs = numBombs; createBombGrid(); createCountGrid(); } public int getNumRows() { return numRows;...
import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...