
Explain the code below, its about solving 8 puzzle using A*
#include <bits/stdc++.h>
using namespace std;
typedef
int calc(vvi & bo, vvi & best) {
int ret = 0;
vector<pair<int, int>> use(9);
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j ++) {
use[bo[i][j]] = {i, j};
}
}
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j ++) {
ret += abs(use[best[i][j]].first - i) + abs(use[best[i][j]].second - j);
}
}
return ret;
}
void print(vvi & bo) {
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j ++) {
cout << (bo[i][j] ? to_string(bo[i][j]) : " ") << " \n"[j == 2];
}
}
cout << '\n';
}
int dx[] = {0, 1, -1, 0};
int dy[] = {1, 0, 0, -1};
vector<vvi> gen(vvi & bo) {
vector<vvi> ret;
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j ++) if(not bo[i][j]) {
for(int k = 0; k < 4; k ++) {
int ni = i + dx[k], nj = j + dy[k];
if(ni >= 0 and nj >= 0 and ni < 3 and nj < 3) {
swap(bo[ni][nj], bo[i][j]);
ret.emplace_back(bo);
swap(bo[ni][nj], bo[i][j]);
}
}
}
}
return ret;
}
struct state {
int c = 0, s = 0;
vvi p, cur;
};
struct cmp {
bool operator() (state & l, state & r) {
return ((l.c == r.c) ? l.s > r.s : l.c > r.c);
}
};
int main() {
priority_queue<state, vector<state>, cmp> pq;
map<vvi, int> vis;
map<vvi, vvi> from;
map<vvi, int> timecost;
vvi bo(3, vector<int>(3));
vvi best = bo;
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j ++) {
cin >> bo[i][j];
best[i][j] = (1 + i * 3 + j) % 9;
}
}
state og;
og.c = calc(bo, best);
og.cur = bo;
pq.emplace(og);
while(!pq.empty()) {
auto ele = pq.top(); pq.pop();
if(not vis[ele.cur]) {
vis[ele.cur] = 1;
from[ele.cur] = ele.p;
timecost[ele.cur] = ele.s;
if(not ele.c) {
break;
}
auto children = gen(ele.cur);
state te;
te.p = ele.cur;
te.s = ele.s + 1;
for(auto child : children) {
te.c = calc(child, best);
te.cur = child;
pq.emplace(te);
}
}
}
if(vis[best]) {
stack<vvi> use;
vvi te = best;
while(te.size()) {
use.emplace(te);
te = from[te];
}
while(not use.empty()) {
print(use.top());
use.pop();
}
}

Step by stepSolved in 2 steps

- Complete the following function that counts the even numbers in a 2D vector of integers. int count_evens(const std::vector<std::vector<int>>& v) { // Add your code... } Please add output screenshot!arrow_forwardPython Big-O Notation/Time Complexity Pls answer only if u know big-o Thanks! Item #3.arrow_forwarddef dotProduct(Vs): V1 #get first vector from Vs ......... V2 #get second vector from Vs #check if they are in the same length ......... if ......... ...... ... #complete as needed ......... for in ...... ... ......... . dp #dp: dot-product ......... #complete as needed #u = (-2 4 -2) and v = (-3 6 3) u = . .; #setup u an v as a numpy array V Vc = [. ., #test the function using u and v print (.) #print "Dot-product of vectors isarrow_forward
- Resize vector countDown to have newSize elements. Populate the vector with integers {newSize, newSize - 1, …, 1}. Ex: If newSize = 3, then countDown = {3, 2, 1}, and the sample program outputs:3 2 1 Go! #include <iostream>#include <vector>using namespace std; int main() { vector<int> countDown(0); int newSize; unsigned int i; cin >> newSize; /* Your solution goes here */ for (i = 0; i < countDown.size(); ++i) { cout << countDown.at(i) << " "; } cout << "Go!" << endl; return 0;}arrow_forwardUse iterators to print each element of v separted by spaces. #include <iostream>#include <vector> using namespace std; int main(){vector<int> v = {1, 3, 5}; (.....) }arrow_forwardComplete the function: bool search(vector<int> v, int key). This function searches for value key in vector v and return true if key is found else return false. In C++arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





