Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. // sokoban.cpp: определяет точку входа для консольного приложения.
  2. //
  3. #include <stdafx.h>
  4. #include <conio.h>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <string>
  8. #include <queue>
  9. #include <vector>
  10. #include <stack>
  11.  
  12. using namespace std;
  13.  
  14. const int MAX = 12;
  15. char field[MAX][MAX];
  16. int m=0, n=0, Lx=0, Ly=0;
  17. int dx[4]={-1, 0, 1, 0};
  18. int dy[4]={0, 1, 0, -1};  
  19.  
  20. class Position {
  21.     public:
  22.         int Hx, Hy, Bx, By;    
  23.         Position* prev;
  24.         Position() {
  25.             Hx=Hy=Bx=By=0;
  26.         }
  27.         Position (int hx, int hy, int bx, int by, Position* pr) {
  28.             Hx = hx;
  29.             Hy = hy;
  30.             Bx = bx;
  31.             By = by;
  32.             prev = pr->prev;
  33.         }
  34.         void draw (int nmb) {  
  35.             cout << "Step # " << nmb << endl;
  36.              for (int i = 1; i < m+1; ++i) {
  37.                  for (int j =1; j < n+1; ++j) {
  38.                      if (i == Hx && j == Hy) {
  39.                         cout << "H";
  40.                         continue;
  41.                         }
  42.                      if (i == Bx && j == By) {
  43.                         cout << "B";
  44.                         continue;
  45.                         }  
  46.                      if (i == Lx && j == Ly) {
  47.                         cout << "L";
  48.                         continue;
  49.                         }
  50.                      cout << field[i][j];
  51.                  }
  52.                  cout << endl;  
  53.              }
  54.              cout << endl;                  
  55.         }
  56. };  
  57.  
  58. bool is_finish (Position& last) {
  59.     if (last.Bx==Lx && last.By == Ly) return true;
  60.     else return false;           
  61.     }
  62.  
  63. void rout (vector<Position*>& pv) {
  64.     Position* curr = pv.back();
  65.     stack<Position> s;
  66.     int i = 0;
  67.     while (curr != NULL) {
  68.         s.push(*curr);
  69.         curr = curr->prev;
  70.     }
  71.     cout << "Number of steps: " << s.size() << endl;
  72.     while (!s.empty()) {
  73.         s.top().draw(i);
  74.         s.pop();
  75.         ++i;
  76.     }
  77. }
  78.  
  79. bool solver (Position &start, int &Bx, int &By) {
  80.     int was[MAX][MAX][MAX][MAX] = {};  
  81.     queue<Position*> q;  
  82.     vector<Position*> pv;
  83.     q.push(&start);
  84.      
  85.     while (!q.empty()) {
  86.         Position* curr = q.front();
  87.         was[curr->Hx][curr->Hy][curr->Bx][curr->By] = 2;
  88.         for (int i = 0; i<4; i++) {              
  89.             int newHx = curr->Hx + dx[i];
  90.             int newHy = curr->Hy + dy[i];
  91.             int newBx = curr->Bx;
  92.             int newBy = curr->By;
  93.             if (field[newHx][newHy] == '#')continue;
  94.             if (newHx == Bx && newHy == By) {
  95.                 newBx = curr->Bx + dx[i];
  96.                 newBy = curr->By + dy[i];
  97.                 if (field[newBx][newBy] == '#') continue;  
  98.             }
  99.             if (was[newHx][newHy][newBx][newBy] != 0) continue;
  100.  
  101.             Position* pos = new Position(newHx, newHy, newBx, newBy, curr);
  102.             if (is_finish(*pos)) {
  103.                 pv.push_back(pos);
  104.                 rout(pv);
  105.                 return true;
  106.             }
  107.             q.push(pos);
  108.             pv.push_back(curr);
  109.             was[newHx][newHy][newBx][newBy] = 1;
  110.         }
  111.         q.pop();
  112.     }
  113.     return false;
  114. }
  115.  
  116. int main () {
  117.     ifstream input ("input.txt");
  118.     int Hx, Hy, Bx, By;
  119.     input >> m >> n ;
  120.     cout << m << " " << n << endl;
  121.     // fill in the field with blocks
  122.     for (int i = 0; i<MAX; ++i) {
  123.         for (int j = 0; j<MAX; ++j) {
  124.             field[i][j]='#';
  125.         }
  126.     }
  127.     // fill in the field from input.txt
  128.     for (int i = 0; i<m; i++) {
  129.         string tmp;
  130.         input >> tmp;
  131.         for (int j = 0; j<n; j++) {
  132.             field[i+1][j+1]=tmp[j];
  133.             if (tmp[j]=='H') {
  134.                 Hx = i+1;
  135.                 Hy = j+1;
  136.                 field[i+1][j+1] = '.';
  137.             }
  138.             if (tmp[j]=='B') {
  139.                 Bx = i+1;
  140.                 By = j+1;
  141.                 field[i+1][j+1] = '.';
  142.             }
  143.             if (tmp[j]=='L') {
  144.                 Lx = i+1;
  145.                 Ly = j+1;
  146.                 field[i+1][j+1] = '.';
  147.             }
  148.         }
  149.     }
  150.     Position start(Hx, Hy, Bx, By, NULL);
  151.     if(!solver(start, Bx, By)) {
  152.         cout << "No solution" << endl;
  153.     }  
  154.     _getch();  
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement