Explain the code below, its about solving 8 puzzle using A* algorithm       #include  using namespace std;  typedef vector> vvi;  int calc(vvi & bo, vvi & best) {  int ret = 0;  vector> 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 gen(vvi & bo) {  vector 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, cmp> pq;  map vis;  map from;  map timecost;  vvi bo(3, vector(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 use;  vvi te = best;  while(te.size()) {  use.emplace(te);  te = from[te];  }  while(not use.empty()) {  print(use.top());  use.pop();  }  }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Explain the code below, its about solving 8 puzzle using A* algorithm

 

 

 


#include <bits/stdc++.h>
 using namespace std;
 typedef vector<vector<int>> vvi;
 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();
 }
 }

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education