Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sokoban.cpp: определяет точку входа для консольного приложения.
- //
- #include <stdafx.h>
- #include <conio.h>
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <queue>
- #include <vector>
- #include <stack>
- using namespace std;
- const int MAX = 12;
- char field[MAX][MAX];
- int m=0, n=0, Lx=0, Ly=0;
- int dx[4]={-1, 0, 1, 0};
- int dy[4]={0, 1, 0, -1};
- class Position {
- public:
- int Hx, Hy, Bx, By;
- Position* prev;
- Position() {
- Hx=Hy=Bx=By=0;
- }
- Position (int hx, int hy, int bx, int by, Position* pr) {
- Hx = hx;
- Hy = hy;
- Bx = bx;
- By = by;
- prev = pr->prev;
- }
- void draw (int nmb) {
- cout << "Step # " << nmb << endl;
- for (int i = 1; i < m+1; ++i) {
- for (int j =1; j < n+1; ++j) {
- if (i == Hx && j == Hy) {
- cout << "H";
- continue;
- }
- if (i == Bx && j == By) {
- cout << "B";
- continue;
- }
- if (i == Lx && j == Ly) {
- cout << "L";
- continue;
- }
- cout << field[i][j];
- }
- cout << endl;
- }
- cout << endl;
- }
- };
- bool is_finish (Position& last) {
- if (last.Bx==Lx && last.By == Ly) return true;
- else return false;
- }
- void rout (vector<Position*>& pv) {
- Position* curr = pv.back();
- stack<Position> s;
- int i = 0;
- while (curr != NULL) {
- s.push(*curr);
- curr = curr->prev;
- }
- cout << "Number of steps: " << s.size() << endl;
- while (!s.empty()) {
- s.top().draw(i);
- s.pop();
- ++i;
- }
- }
- bool solver (Position &start, int &Bx, int &By) {
- int was[MAX][MAX][MAX][MAX] = {};
- queue<Position*> q;
- vector<Position*> pv;
- q.push(&start);
- while (!q.empty()) {
- Position* curr = q.front();
- was[curr->Hx][curr->Hy][curr->Bx][curr->By] = 2;
- for (int i = 0; i<4; i++) {
- int newHx = curr->Hx + dx[i];
- int newHy = curr->Hy + dy[i];
- int newBx = curr->Bx;
- int newBy = curr->By;
- if (field[newHx][newHy] == '#')continue;
- if (newHx == Bx && newHy == By) {
- newBx = curr->Bx + dx[i];
- newBy = curr->By + dy[i];
- if (field[newBx][newBy] == '#') continue;
- }
- if (was[newHx][newHy][newBx][newBy] != 0) continue;
- Position* pos = new Position(newHx, newHy, newBx, newBy, curr);
- if (is_finish(*pos)) {
- pv.push_back(pos);
- rout(pv);
- return true;
- }
- q.push(pos);
- pv.push_back(curr);
- was[newHx][newHy][newBx][newBy] = 1;
- }
- q.pop();
- }
- return false;
- }
- int main () {
- ifstream input ("input.txt");
- int Hx, Hy, Bx, By;
- input >> m >> n ;
- cout << m << " " << n << endl;
- // fill in the field with blocks
- for (int i = 0; i<MAX; ++i) {
- for (int j = 0; j<MAX; ++j) {
- field[i][j]='#';
- }
- }
- // fill in the field from input.txt
- for (int i = 0; i<m; i++) {
- string tmp;
- input >> tmp;
- for (int j = 0; j<n; j++) {
- field[i+1][j+1]=tmp[j];
- if (tmp[j]=='H') {
- Hx = i+1;
- Hy = j+1;
- field[i+1][j+1] = '.';
- }
- if (tmp[j]=='B') {
- Bx = i+1;
- By = j+1;
- field[i+1][j+1] = '.';
- }
- if (tmp[j]=='L') {
- Lx = i+1;
- Ly = j+1;
- field[i+1][j+1] = '.';
- }
- }
- }
- Position start(Hx, Hy, Bx, By, NULL);
- if(!solver(start, Bx, By)) {
- cout << "No solution" << endl;
- }
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement