Question: Please I need help converting the following C++ code to java code. #include #include #include #include #include using namespace std; #include State.h #include Stat.h const

Please I need help converting the following C++ code to java code.
#include
#include
#include
#include
#include
using namespace std;
#include "State.h"
#include "Stat.h"
const int DISPLAYED = 15;
// Depth-First Search
bool DFS(const State& init, const State& goal, Stat& stat)
{
stat.reset();
stat.start();
int displayed = 0;
stack frontier;
set explored;
frontier.push(init);
explored.insert(init);
while (!frontier.empty())
{
stat.concurency++;
State state = frontier.top();
frontier.pop();
explored.insert(state);
if (displayed++ < DISPLAYED)
cout << state << endl;
if (state == goal) // solved
{
stat.stop();
stat.steps = state.step;
return true;
}
for (auto& nb: state.neighbors())
{
if (!explored.count(nb) )
{
nb.step = state.step + 1;
frontier.push(nb);
}
}
}
stat.stop();
return false; // not solutions
}
// Breadth-First Search
bool BFS(const State& init, const State& goal, Stat& stat)
{
stat.reset();
stat.start();
int displayed = 0;
queue frontier;
set explored;
frontier.push(init);
explored.insert(init);
while (!frontier.empty())
{
stat.concurency++;
State state = frontier.front();
frontier.pop();
explored.insert(state);
if (displayed++ < DISPLAYED)
cout << state << endl;
if (state == goal) // solved
{
stat.stop();
stat.steps = state.step;
return true;
}
for (auto& nb : state.neighbors())
{
if (!explored.count(nb))
{
nb.step = state.step + 1;
frontier.push(nb);
}
}
}
stat.stop();
return false; // not solutions
}
// A* Search
bool AStar(const State& init, const State& goal, Stat& stat)
{
stat.reset();
stat.start();
int displayed = 0;
std::priority_queue, Comparator> frontier{ Comparator(goal) };
set explored;
frontier.push(init);
explored.insert(init);
while (!frontier.empty())
{
stat.concurency++;
State state = frontier.top();
frontier.pop();
explored.insert(state);
if (displayed++ < DISPLAYED)
cout << state << endl;
if (state == goal) // solved
{
stat.stop();
stat.steps = state.step;
return true;
}
for (auto& nb : state.neighbors())
{
if (!explored.count(nb))
{
nb.step = state.step + 1;
frontier.push(nb);
}
}
}
stat.stop();
return false; // not solutions
}
int main()
{
//string initStr = "7 2 4 5 0 6 8 3 2";
//string goalStr = "2 2 3 4 5 6 7 8 0";
State init, goal;
cin >> init;
cin >> goal;
Stat stat;
DFS(init, goal, stat);
cout << "DFS..." << endl;
cout << stat << endl;
BFS(init, goal, stat);
cout << "BFS..." << endl;
cout << stat << endl;
AStar(init, goal, stat);
cout << "A*..." << endl;
cout << stat << endl;
return 0;
}
#ifndef STAT_H
#define STAT_H
#include
#include
using namespace std;
/*
Structure to collect algorithm statistics
*/
struct Stat
{
int steps;
int concurency;
clock_t startClock;
clock_t stopClock;
Stat()
{
reset();
}
void reset()
{
steps = 0;
concurency = 0;
startClock = 0;
stopClock = 0;
}
void start() { startClock = clock(); }
void stop() { stopClock = clock(); }
double getTime() const { return (double)(stopClock - startClock) / CLOCKS_PER_SEC; }
friend ostream & operator<<(ostream & out, const Stat & stat)
{
out << "Total number of steps: " << stat.steps << endl;
out << "Elapsed time: " << stat.getTime() << " s" << endl;
out << "Maximum number of concurent states: " << stat.concurency << endl;
return out;
}
};
#endif
#ifndef STATE_H
#define STATE_H
#include
#include
#include
#include
#include
#include
using namespace std;
const int DIM = 3;
/*
Structure representing game state
*/
struct State
{
int arr[DIM][DIM];
int emptyX, emptyY;
int step;
State() : step(0) {}
// Returns neighbor list for fourth directions
vector neighbors()
{
vector states;
for (int i = 0; i < 4; i += 1)
{
int dx = (i % 2) * (2 * (i / 2) - 1);
int dy = ((i + 1) % 2) * (2 * (i / 2) - 1);
if (emptyX + dx >= 0 && emptyX + dx < DIM &&
emptyY + dy >= 0 && emptyY + dy < DIM)
{
State st = *this;
st.emptyX = emptyX + dx;
st.emptyY = emptyY + dy;
swap(st.arr[emptyY][emptyX], st.arr[st.emptyY][st.emptyX]);
states.push_back(st);
}
}
return states;
}
//
friend bool operator==(const State& lhs, const State& rhs)
{
for (int i = 0; i < DIM; ++i)
for (int j = 0; j < DIM; ++j)
if (lhs.arr[i][j] != rhs.arr[i][j])
return false;
return true;
}
// Uses in set
friend bool operator<(const State& lhs, const State& rhs)
{
for (int i = 0; i < DIM; ++i)
for (int j = 0; j < DIM; ++j)
if (lhs.arr[i][j] < rhs.arr[i][j])
return true;
else if (lhs.arr[i][j] > rhs.arr[i][j])
return false;
return false;
}
// Input
friend istream& operator>>(istream& in, State& state)
{
for (int i = 0; i < DIM; ++i)
{
for (int j = 0; j < DIM; ++j)
{
in >> state.arr[i][j];
if (state.arr[i][j] == 0)
{
state.emptyX = j;
state.emptyY = i;
}
}
}
return in;
}
// Output
friend ostream& operator<<(ostream& out, const State& state)
{
out << "+-----+" << endl;
for (int i = 0; i < DIM; ++i)
{
out << "|" << state.arr[i][0];;
for (int j = 1; j < DIM; ++j)
out << " " << state.arr[i][j];
out << "|" << endl;
}
out << "+-----+" << endl;
return out;
}
// Hamming priority function
static int distance(const State& curr, const State& goal)
{
int dist = 0;
for (int i = 0; i < DIM; ++i)
for (int j = 0; j < DIM; ++j)
if (curr.arr[i][j] == goal.arr[i][j])
++dist;
return dist + curr.step;
}
};
// Compares two State by Hamming distance
class Comparator
{
State goal;
public:
Comparator(const State& goal) : goal(goal) {}
bool operator()(const State& lhs, const State& rhs) const
{
return State::distance(lhs, goal) < State::distance(rhs, goal);
}
};
#endif // STATE_H

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!