Question

import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

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 (i >= 0 && i < visited.length && j >= 0 && j < visited[i].length && !visited[i][j]);
}

private void findWordsFromLocation(int i, int j) {
boolean visited[][] = new boolean[grid.length][grid[0].length];
findWords("", i, j, visited);
}

private void findWords(String wordTillNow, int i, int j, boolean visited[][]) {
if (visited[i][j] || wordTillNow.length() > 6) {
return;
}

wordTillNow = wordTillNow + grid[i][j];
visited[i][j] = true;
  
if(words.contains(wordTillNow) && wordTillNow.length() > 1) {
foundWords.add(wordTillNow);
}
  
for (int rowOffset = -1; rowOffset < 2; rowOffset++) {

for (int colOffset = -1; colOffset < 2; colOffset++) {
if (rowOffset != 0 || colOffset != 0) {
if(isValidIndex(i + rowOffset, j + colOffset, visited)) {
findWords(wordTillNow, i+rowOffset, j + colOffset, visited);
}
}
}
}
  
visited[i][j] = false;
}

public static void main(String[] args) throws FileNotFoundException {

char grid[][] = { { 'M', 'S', 'E', 'F'}, { 'R', 'A', 'T', 'D'}, { 'L', 'O', 'N', 'E'},
{ 'K', 'A', 'F', 'B'}};
  
FindWordInMaze finder = new FindWordInMaze(grid);
finder.findWords();
}

}
can you chnage this program into c++. And can this have a better user freindly interface i am getting confused between java and c++ syntaxes.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Converted java file to C++ file

FindWordInMaze.cpp

-------------------------------------

#include<bits/stdc++.h>
#define MAX 10
using namespace std;
int row,col;
bool visited[MAX][MAX];
class FindWordInMaze
{
private:
vector<vector<char>>grid;
vector<string>words;
unordered_set<string>foundWords;
public:


FindWordInMaze(vector<vector<char>>grid)
{
this->grid = grid;
words.push_back("START");
words.push_back("NOTE");
words.push_back("SAND");
words.push_back("STONED");

sort(words.begin(),words.end());
}
bool isValidIndex(int i,int j);
void findWords(string wordTillNow, int i, int j);
void findWordsFromLocation(int i, int j);
void findWords();
};
bool FindWordInMaze::isValidIndex(int i, int j)
{
return (i>=0 && i<row && j>=0 && j<col && !visited[i][j]);
}
void FindWordInMaze::findWords(string wordTillNow, int i, int j)
{
if(visited[i][j] || wordTillNow.length()>6)
{
return;
}
wordTillNow = wordTillNow + grid[i][j];
visited[i][j] = true;

if(find(words.begin(),words.end(),wordTillNow)!=words.end() && wordTillNow.length())
{
foundWords.insert(wordTillNow);
}
for(int rowOffset = -1; rowOffset<2; rowOffset++)
{
for(int colOffset = -1; colOffset<2; colOffset++)
{
if(rowOffset!=0 || colOffset!=0)
{
if(isValidIndex(i+rowOffset,j+colOffset))
{
findWords(wordTillNow, i+rowOffset,j+colOffset);
}
}
}
}
visited[i][j] = false;
}
void FindWordInMaze::findWordsFromLocation(int i, int j)
{

findWords("",i,j);
}
void FindWordInMaze::findWords()
{
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[i].size();j++)
{
findWordsFromLocation(i,j);
}
}

for(string w: foundWords)
{
cout<<w<<" ";
}
}
int main()
{
vector<vector<char>>grid={ { 'M', 'S', 'E', 'F'},
{ 'R', 'A', 'T', 'D'},
{ 'L', 'O', 'N', 'E'},
{ 'K', 'A', 'F', 'B'}};
row = grid.size();
col = grid[0].size();
FindWordInMaze finder(grid);
finder.findWords();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    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...

  • What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class...

    What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...

  • Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...

    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;...

  • 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...

    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...

  • package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private...

    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...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */    public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...

  • I need help with the last method listed in the problem: Implement a class Grid that...

    I need help with the last method listed in the problem: Implement a class Grid that stores measurements in a rectangular grid. The grid has a given number of rows and columns, and a description string can be added for any grid location. Supply the following constructor and methods: public Grid(int numRows, int numColumns) public void add(int row, int column, String description) public String getDescription(int row, int column) public ArrayList getDescribedLocations() Here, Location is an inner class that encapsulates the...

  • I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String...

    I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String name, major, status; private int rank;    public Student() { this.name = this.major = this.status = ""; this.rank = 0; }    public Student(String name, String major, String status) { this.name = name; this.major = major; this.status = status; } public String getName() { return name; } public String getMajor() { return major; } public String getStatus() { return status; }    public int...

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */ public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null; //TODO...

  • How can I make a test case with Junit? import java.util.ArrayList; import board.Board; public class Pawn...

    How can I make a test case with Junit? import java.util.ArrayList; import board.Board; public class Pawn extends Piece{    public Pawn(int positionX, int positionY, boolean isWhite) {        super("P", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               //check side, different color pawns go on different ways        if(isWhite) {            if(positionX != 7 && positionY != 0) {           ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT