当前位置: 首页 > news >正文

poj 1475 Pushing Boxes


双重bfs:

  在推箱子游戏中人推箱子只有两种情况:

    人推箱子:那么箱子此时所处的位置就是人接下来会到达的位置

    人找位子推箱子:那么人就要到达箱子下一步到达位子对应的相反方向得那一个格子

  所以,主干是箱子的移动过程,箱子每移动一步就对接下来人所处的位子进行考虑。也就是在一次bfs的过程中不断套用另一个bfs

View Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int maxn = 22;
char map[maxn][maxn];
bool visPerson[maxn][maxn];
bool visBox[maxn][maxn];
int R , C;
int dir[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
char pushes[4] = { 'E','W','S','N' };
char walks[4] = { 'e','w','s','n' };
string path;
struct Node{
    int br , bc , pr , pc;
    string ans;
};
bool inMap(int r , int c) {
    return r>=0 && r<R && c>=0 && c<C;
}
bool bfs2(int sr, int sc, int er, int ec, int br, int bc, string &ans) {
    Node node , tmpnode;
    memset(visPerson,0,sizeof(visPerson));
    queue<Node> Q;
    node.pr = sr;
    node.pc = sc;
    node.ans = "";
    Q.push(node);
    visPerson[br][bc] = true;
    while(!Q.empty()) {
        node = Q.front();  Q.pop();
        if(node.pr == er && node.pc == ec) {
            ans = node.ans;
            return true;
        }
        if(visPerson[node.pr][node.pc]) continue;
        visPerson[node.pr][node.pc] = true;
        for(int i=0;i<4;i++) {
            int nr = node.pr + dir[i][0];
            int nc = node.pc + dir[i][1];
            if(inMap(nr,nc) && !visPerson[nr][nc] && map[nr][nc]!='#') {
                tmpnode.pr = nr;
                tmpnode.pc = nc;
                tmpnode.ans =node.ans + walks[i];
                Q.push(tmpnode);
            }
        }
    }
    return false;
}
bool bfs1(int sr ,int sc, int br, int bc) {
    Node node , tmpnode;
    memset(visBox,0,sizeof(visBox));
    queue<Node> Q;
    node.pr = sr;
    node.pc = sc;
    node.br = br;
    node.bc = bc;
    node.ans = "";
    Q.push(node);
    while(!Q.empty()) {
        node = Q.front();  Q.pop();
        if(visBox[node.br][node.bc])
            continue;
        visBox[node.br][node.bc] = true;
        if(map[node.br][node.bc]=='T') {
            path = node.ans;
            return true;
        }
        visBox[node.br][node.bc] = true;
        for(int i=0;i<4;i++) {
            int nextr = node.br + dir[i][0];
            int nextc = node.bc + dir[i][1];
            int backr = node.br - dir[i][0];
            int backc = node.bc - dir[i][1];
            string ans = "";
            if( inMap(nextr,nextc) && inMap(backr,backc)
            && map[nextr][nextc]!='#' && map[backr][backc]!='#'
            && !visBox[nextr][nextc] ) {
                if(bfs2(node.pr, node.pc, backr, backc, node.br, node.bc, ans)) {
                    tmpnode.pr = node.br;
                    tmpnode.pc = node.bc;
                    tmpnode.br = nextr;
                    tmpnode.bc = nextc;
                    tmpnode.ans = node.ans +ans +pushes[i];
                    Q.push(tmpnode);
                }
            }
        }
    }
    return false;
}
int main() {
    int cas = 1;
    int sr , sc , br , bc;
    while(~scanf("%d%d",&R,&C) && R) {
        for(int i=0;i<R;i++) scanf("%s",map[i]);
        for(int i=0;i<R;i++)
            for(int j=0;j<C;j++) {
                if(map[i][j]=='S') {
                    sr = i; sc = j;
                }
                if(map[i][j]=='B') {
                    br = i; bc = j;
                }
            }
        path = "";
        printf("Maze #%d\n",cas++);
        bfs1(sr,sc,br,bc) ? cout<<path<<endl : cout<<"Impossible."<<endl;
        cout<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/lenohoo/archive/2012/06/30/2571049.html

相关文章:

  • 初识 ActivityLifecycleCallbacks
  • Zim - 普通人的Org-mode
  • 带参数存储过程的小例子
  • NSLog输出对象
  • 需要Review的题目:
  • lame的ios 静态库创建shell
  • 浅谈设计模式在iOS开发实战项目中的应用
  • string的Equels问题小记
  • JS创建函数:函数声明和函数表达式
  • 快给你的app上锁吧(android数字解锁)
  • 2012财富世界500强发布,大陆首超日本,新增12家
  • F#中的事件(下)
  • BW数据源深入研究【转自WKingChen的博客】
  • JsonModel 的使用
  • 【总结】后缀数组
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • co.js - 让异步代码同步化
  • conda常用的命令
  • Django 博客开发教程 16 - 统计文章阅读量
  • HTTP请求重发
  • JAVA_NIO系列——Channel和Buffer详解
  • Laravel核心解读--Facades
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • Phpstorm怎样批量删除空行?
  • SQLServer插入数据
  • Windows Containers 大冒险: 容器网络
  • 大整数乘法-表格法
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • 走向全栈之MongoDB的使用
  • FaaS 的简单实践
  • #stm32整理(一)flash读写
  • (12)Hive调优——count distinct去重优化
  • (9)STL算法之逆转旋转
  • (二)pulsar安装在独立的docker中,python测试
  • (生成器)yield与(迭代器)generator
  • (四)linux文件内容查看
  • (四)库存超卖案例实战——优化redis分布式锁
  • (四)模仿学习-完成后台管理页面查询
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (一)Dubbo快速入门、介绍、使用
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • ***通过什么方式***网吧
  • .bat批处理(一):@echo off
  • .NET 5种线程安全集合
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .NET程序员迈向卓越的必由之路
  • ::before和::after 常见的用法
  • @EventListener注解使用说明
  • @Tag和@Operation标签失效问题。SpringDoc 2.2.0(OpenApi 3)和Spring Boot 3.1.1集成
  • [ffmpeg] 定制滤波器
  • [HackMyVM]靶场 Quick3
  • [HNOI2018]排列
  • [JS7] 显示从0到99的100个数字