美文网首页
AcWing 172. 立体推箱子(搜索)

AcWing 172. 立体推箱子(搜索)

作者: 良木lins | 来源:发表于2020-04-06 17:24 被阅读0次

广度优先搜索

原题链接

感悟:这类题目的基本框架还是很简单的,剪枝都不用思考很多。但状态的表示是个难题,特别的麻烦,只能先总结点大概的处理思路。这题我是想不到这么好的处理方式,特别是三维数组都不是很会用,刷完搜索就去刷刷数据结构类的题吧。
本题是个单源最短路径的问题


广搜的基本框架(bfs)

  • 题目所给的所有状态的表示
  • 开始与结束状态的储存
  • 基本四个方向的表示
  • 每走一步状态的储存
int bfs()
{
    queue<> q;
    q.push(start);//起始点先入队列
    
    while(q.size())
    {
        auto t = q.front(); q.pop();//队头出来执行命令
        
        for(int i = 0; i < 4; i++)//循环四个方向走
        {
            if(!check()) ; //判重,检查
            if() q.push(); //满足条件,则入队
        }
    }
    return ;
}

本题思路

  • 1.把图存起来;2.箱子可立可躺,躺又可横可竖,三种状态。
    • 三种状态表示,特别是躺的,最好找准一个基点(左上),不然很难处理
    • struct State{ int x, y, s;};//s = 0立 1横 2竖
  • 找开始与结束,开始分三种,都用State存好
  • 四个方向的表示关键是加了一个状态,用三维数组处理
  • 每一步的表示也是多一个状态,三维数组
ACcode
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 505;

struct State{
    int x, y, s;
};

int n, m;
char g[N][N];
int dist[N][N][3];

bool check(int x, int y)
{
    if(x < 0 || x >= n || y < 0 || y >= m) return false;
    if(g[x][y] == '#') return false;
    return true;
}

int bfs(State start, State end)
{
    queue<State> q;
    memset(dist, -1, sizeof(dist));
    dist[start.x][start.y][start.s] = 0;
    q.push(start);
    
    int d[3][4][3] = { //三个状态,四个方向,走完后的三种状态之一
        {{-2, 0, 2}, {0, 1, 1}, {1, 0, 2}, {0, -2, 1}},
        {{-1, 0, 1}, {0, 2, 0}, {1, 0, 1}, {0, -1, 0}},
        {{-1, 0, 0}, {0, 1, 2}, {2, 0, 0}, {0, -1, 2}}
    };
    
    while(q.size())
    {
        auto t = q.front(); //这里的auto就是State
        q.pop();
        
        for(int i = 0; i< 4; i++)
        {
            State next = {t.x+d[t.s][i][0], t.y+d[t.s][i][1], d[t.s][i][2]};
            
            int x = next.x, y = next.y;
            if(!check(x, y)) continue;  //检查这一个点
            if(next.s == 0) {           //立的不能在‘E’
                if(g[x][y] == 'E') continue;
            }
            else if(next.s == 1) {      //横的第二个位置
                if(!check(x, y+1)) continue;
            }
            else {                      //竖的第二个位置
                if(!check(x+1, y)) continue;
            }
            
            if(dist[next.x][next.y][next.s] == -1)
            {
                dist[next.x][next.y][next.s] = dist[t.x][t.y][t.s] + 1;
                q.push(next);
            }
        }
    }
    
    return dist[end.x][end.y][end.s];
}

int main()
{
    while(scanf("%d%d", &n, &m), n || m)
    {
        for(int i = 0; i < n; i++) scanf("%s", g[i]);
        
        State start = { -1 }, end;
        for(int i = 0; i < n; i++)  //储存开始,结束状态
            for(int j = 0; j < m; j++)
                if(g[i][j] == 'X' && start.x == -1)
                {
                    if(g[i+1][j] == 'X') start = {i, j, 2};
                    else if(g[i][j+1] == 'X') start = {i, j, 1};
                    else start = {i, j, 0};
                }
                else if(g[i][j] == 'O') end = {i, j, 0};
                
        int cnt = bfs(start, end);
        if(cnt == -1) printf("Impossible\n");
        else printf("%d\n", cnt); 
    }
    return 0;
}

相关文章

  • AcWing 172. 立体推箱子(搜索)

    广度优先搜索 原题链接 感悟:这类题目的基本框架还是很简单的,剪枝都不用思考很多。但状态的表示是个难题,特别的麻烦...

  • 推箱子

    效果图 资料图片 代码

  • AcWing 167. 木棒(搜索)

    深度搜索 + 剪枝 感悟:开始的时候自己能写一些出来,基本就写个主函数,能想到两个剪枝条件,啊,还得加倍努力啊!!...

  • AcWing 166. 数独(搜索)

    深度优先搜索 原题链接 优化非常重要,在这题里更是如此 常见的优化技巧(本题前三种都有使用) 优化搜索顺序 排除冗...

  • AcWing 170. 加成序列(搜索)

    迭代加深 原题链接 感悟:之前用紫书学了下迭代加深,自我感觉应该还是可以的,这次在来实践的时候才发现,除了知道大概...

  • AcWing 165. 小猫爬山(搜索)

    深度优先搜索(dfs) 体会 要考虑的问题 枚举对象 dfs的参数 返回条件 剪枝技巧原题链接 枚举对象: 车...

  • 剑指week4

    1.二叉搜索树的后序遍历序列 [https://www.acwing.com/problem/content/44...

  • 推箱子C代码

  • 推箱子小游戏

    以前做的小游戏,很多地方不足也不做修改了,比较有趣,用到了一点算法,所以摘过来放在这儿 GitHub地址:http...

  • 推箱子的人们

    早上搭地铁上班,发现推箱子的人日渐增多。哦,快要过年了,游子要归家。箱子色泽鲜艳,小巧轻便,不用想都知道,箱子里面...

网友评论

      本文标题:AcWing 172. 立体推箱子(搜索)

      本文链接:https://www.haomeiwen.com/subject/wjibphtx.html