Advertisement
Kaidul

N Puzzle

Feb 2nd, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define Max 7
  5.  
  6. int k;
  7. int indx[Max][Max];
  8. #define _2D vector <vector <int> >
  9. struct tripple {
  10.     _2D state;
  11.     int eval;
  12.     string command;
  13.     tripple() {}
  14.     tripple(_2D grid, int cost, string cmd) {
  15.         state = grid;
  16.         eval = cost;
  17.         command = cmd;
  18.     }
  19.     bool operator < (const tripple &other) const {
  20.         return eval > other.eval;
  21.     }
  22. };
  23. priority_queue < tripple > pQueue;
  24. map < _2D, bool > visited;
  25. vector <string> solution;
  26. // number Of Misplaced Tiles
  27. int heuristic(_2D &grid) {
  28.     int cnt = 0, ret = 0;
  29.     for(int i = 0; i < k; ++i)
  30.         for(int j = 0; j < k; ++j) {
  31.             if(grid[i][j] != cnt) ++ret;
  32. //            indx[i][j] = cnt;
  33.             ++cnt;
  34.         }
  35.     return ret;
  36. }
  37.  
  38. int directionX[] = {0, -1, 0, 1};
  39. int directionY[] = {-1, 0, 1, 0};
  40. string dir[] = {"LEFT", "UP", "RIGHT", "DOWN"};
  41.  
  42. pair<int ,int> findEmpty(_2D &grid) {
  43.     for(int i = 0; i < k; ++i)
  44.         for(int j = 0; j < k; ++j)
  45.             if(grid[i][j] == 0) return make_pair(i, j);
  46. }
  47.  
  48. bool isValid(int x, int y) {
  49.     return x >= 0 and y >= 0 and x < k and y < k;
  50. }
  51.  
  52. bool isGoalState(_2D &grid) {
  53.     return heuristic(grid) == 0;
  54. }
  55. pair < _2D, string > source, destination;
  56. bool found = false;
  57. map < pair<_2D, string>, pair<_2D, string> > parent;
  58. void AStarHelper(_2D &grid, string cmd, int depth) {
  59.     if( isGoalState(grid) ) {
  60.         destination = make_pair(grid, cmd);
  61.         found = true;
  62.         return;
  63.     }
  64.     pair<int, int> empty = findEmpty(grid);
  65.     int x, y, eval;
  66.     _2D backup = grid;
  67.     for(int i = 0; i < 4; ++i) {
  68.         x = empty.first + directionX[i], y = empty.second + directionY[i];
  69.         if(isValid(x, y)) {
  70.             swap(grid[empty.first][empty.second], grid[x][y]);
  71.             if( !visited[grid] ) {
  72.                 eval = depth + heuristic(grid);
  73.                 visited[grid] = true;
  74.                 parent[make_pair(grid, dir[i])] = make_pair(backup, cmd);
  75.                 pQueue.push(tripple(grid, eval, dir[i]));
  76.             }
  77.             swap(grid[empty.first][empty.second], grid[x][y]);
  78.         }
  79.     }
  80.     while(!pQueue.empty()) {
  81.         tripple poped = pQueue.top();
  82.         pQueue.pop();
  83.         AStarHelper(poped.state, poped.command, depth + 1);
  84.         if(found) return;
  85.     }
  86.  
  87. }
  88.  
  89. pair < _2D, string > dummy;
  90.  
  91. void printSolution() {
  92.     pair < _2D, string > curr = destination;
  93.     while(curr != source) {
  94.         solution.push_back(curr.second);
  95.         curr = parent[curr];
  96.     }
  97.     for(int i = solution.size() - 1; i >= 0; --i)
  98.         cout << solution[i] << endl;
  99. }
  100.  
  101. void AStar(_2D &grid) {
  102.     visited[grid] = true;
  103.     int depth = 0;
  104.     int eval = depth + heuristic(grid);
  105.     source = make_pair(grid, string(""));
  106. //    parent[source] = dummy;
  107.     AStarHelper(grid, string(""), depth + 1);
  108.     printSolution();
  109. }
  110.  
  111. int main(void) {
  112.     freopen("input.txt", "r", stdin);
  113.     freopen("output.txt", "w", stdout);
  114.     vector < vector<int> > grid(Max);
  115.     cin >> k;
  116.     int input;
  117.     for(int i = 0; i < k; ++i) {
  118.         for(int j = 0; j < k; ++j) {
  119.             cin >> input;
  120.             grid[i].push_back(input);
  121.         }
  122.     }
  123.     AStar(grid);
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement