[Ybtoj Chapter 5 Example 3] Three-dimensional Sokoban [Guangsu]

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here


Problem solving ideas

First of all, this is Guangsou.
The state and end of the box at the beginning can be preprocessed.
Then 1 × 2 1 × 21×How to record the situation of 2 ?
We can record the first grid, and then directly judge the state of the second grid.
Then it is very troublesome for the box to move, we have to usedx, dy dx,dydx,d y represents the moving direction,dt dtd t represents the state after moving.
Finally, just search directly.


Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;

const int dx[4][5] = {
    
     {
    
    }, {
    
     0, 0, 0, -2, 1 }, {
    
     0, 1, -1, 0, 0 }, {
    
     0, 0, 0, 2, -1 } };
const int dy[4][5] = {
    
     {
    
    }, {
    
     0, 1, -2, 0, 0 }, {
    
     0, 0, 0, 2, -1 }, {
    
     0, 1, -1, 0, 0 } };
const int dt[4][5] = {
    
     {
    
    }, {
    
     0, 2, 2, 3, 3 }, {
    
     0, 2, 2, 1, 1 }, {
    
     0, 3, 3, 1, 1 } };

char lyx[600][600];
bool flag = 0;
int n, m, h, t, tx, ty, v[600][600][4];

struct c {
    
    
    int x, y, t, c;
} a[6000000];

bool check(int x, int y, int t) {
    
    
    if (x < 1 || y < 1 || x > n || y > m || v[x][y][t] == 1)
        return 0;
    if (t == 1) {
    
    
        if (lyx[x][y] == '#' || lyx[x][y] == 'E')
            return 0;
    }
    if (t == 2) {
    
    
        if (lyx[x][y] == '#' || lyx[x][y + 1] == '#' || y + 1 > m)
            return 0;
    }
    if (t == 3) {
    
    
        if (lyx[x][y] == '#' || lyx[x + 1][y] == '#' || x + 1 > n)
            return 0;
    }
    return 1;
}

void bfs() {
    
    
    h = 0, t = 1;
    while (h < t) {
    
    
        h++;
        for (int i = 1; i <= 4; i++) {
    
    
            int xx = a[h].x + dx[a[h].t][i], yy = a[h].y + dy[a[h].t][i], tt = dt[a[h].t][i];
            if (check(xx, yy, tt)) {
    
    
                t++;
                v[xx][yy][tt] = 1;
                a[t].x = xx, a[t].y = yy, a[t].t = tt, a[t].c = a[h].c + 1;
                if (xx == tx && yy == ty && tt == 1) {
    
    
                    printf("%d\n", a[t].c);
                    return;
                }
            }
        }
    }
    printf("Impossible\n");
}

int main() {
    
    
    while (scanf("%d%d", &n, &m)) {
    
    
        memset(v, 0, sizeof(v));
        if (n == 0 && m == 0)
            break;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++) cin >> lyx[i][j];
        flag = 0;
        for (int i = 1; i <= n; i++) {
    
    
            for (int j = 1; j <= m; j++) {
    
    
                if (lyx[i][j] == 'O')
                    tx = i, ty = j;
                if (lyx[i][j] == 'X' && !flag) {
    
    
                    a[1].x = i, a[1].y = j, flag = 1;
                    if (lyx[i][j + 1] == 'X')//状态2:横着躺着
                        a[1].t = 2;
                    else if (lyx[i + 1][j] == 'X')//状态3:竖着躺着
                        a[1].t = 3;
                    else
                        a[1].t = 1, v[i][j][a[i].t] = 1;//状态1:立着
                }
            }
        }
        bfs();
    }
}

Guess you like

Origin blog.csdn.net/kejin2019/article/details/113002327