woc累死了写了两个半小时。。。就是BFS?我太菜了。。。


刚开始以为让人预先跑一遍BFS,然后一会儿取两节加起来就好了,结果发现求出来的最短路(就是这个意思)会因箱子的移动而变化。。。。我死了QWQ

康康书上正解:双重BFS?!!让人推箱子的时候再跑一遍BFS?!!时间不就爆炸了?!!啊n,m只有20。。。我死。。。

乖乖打正解。。结果又不知道如何传人BFS时的答案。。。想了半天。。就是开一个全局跑完人的BFS直接用就行了。。。

然后BFS跳出的时候还写错了。。否则更新不了

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#define R register int
using namespace std;
const int d[][]={-,,,,,-,,};
char op[]={'n','s','w','e'},OP[]={'N','S','W','E'};
inline int g() {
R ret=; register char ch; while(!isdigit(ch=getchar()));
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret;
}
int n,m,sx,sy,tx,ty,bx,by;
char mp[][];
bool vis[][],mk[][][];
string ans;
struct node {int x,y,xx,yy; string ans; node() {}
node(int _x,int _y,int _xx,int _yy,string s):x(_x),y(_y),xx(_xx),yy(_yy),ans(s) {}
}bed,brt;
struct peo {int x,y; string ans; peo() {}
peo(int _x,int _y,string s):x(_x),y(_y),ans(s) {}
}ped,prt;
inline bool is(char ch) {return ch=='S'||ch=='T'||ch=='B'||ch=='#'||ch=='.';}
inline bool ckpos(int x,int y) {return x<||x>n||y<||y>m;}
bool bfs1(int sx,int sy,int i,int j,int xx,int yy) {
queue<peo>q;
if(ckpos(sx,sy)||mp[sx][sy]=='#') return false;
memset(vis,false,sizeof(vis)); vis[i][j]=true,vis[xx][yy]=true;
q.push(peo(xx,yy,"")); while(q.size()) {
prt=q.front(); q.pop();
if(prt.x==sx&&prt.y==sy) return true;
for(R i=;i<;++i) {
R x=prt.x+d[i][],y=prt.y+d[i][]; ped.x=x,ped.y=y;
if(ckpos(x,y)||vis[x][y]||mp[x][y]=='#') continue; vis[x][y]=true;
ped.ans=prt.ans+op[i]; q.push(ped);
}
} return false;
}
bool bfs() { queue<node>q;
q.push(node(bx,by,sx,sy,"")); while(q.size()) {
brt=q.front(); q.pop();
for(R i=;i<;++i) {
R x=brt.x+d[i][],y=brt.y+d[i][]; bed.x=x,bed.y=y;
if(brt.x==tx&&brt.y==ty) return true;
if(ckpos(x,y)||mk[x][y][i]||mp[x][y]=='#') continue;
if(bfs1(brt.x-d[i][],brt.y-d[i][],brt.x,brt.y,brt.xx,brt.yy)) {
bed.xx=brt.x,bed.yy=brt.y,bed.ans=brt.ans+prt.ans+OP[i];
mk[x][y][i]=true; q.push(bed);
}
}
} return false;
}
signed main() { R t=;
while(n=g(),m=g(),n!=) {
memset(mk,false,sizeof(mk));
for(R i=;i<=n;++i) for(R j=;j<=m;++j) {
while(!is(mp[i][j]=getchar()));
if(mp[i][j]=='S') sx=i,sy=j;
else if(mp[i][j]=='T') tx=i,ty=j;
else if(mp[i][j]=='B') bx=i,by=j;
}
printf("Maze #%d\n",++t);
if(bfs()) cout<<brt.ans<<'\n';
else printf("Impossible.\n");
putchar('\n');
}
}

2019.04.26

POJ1475 Pushing Boxes 华丽丽的双重BFS的更多相关文章

  1. POJ1475 Pushing Boxes(双搜索)

    POJ1475 Pushing Boxes  推箱子,#表示墙,B表示箱子的起点,T表示箱子的目标位置,S表示人的起点 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化 ...

  2. poj1475 Pushing Boxes[双重BFS(毒瘤搜索题)]

    地址. 很重要的搜索题.★★★ 吐槽:算是写过的一道码量比较大的搜索题了,细节多,还比较毒瘤.虽然是一遍AC的,其实我提前偷了大数据,但是思路还是想了好长时间,照理说想了半小时出不来,我就会翻题解,但 ...

  3. POJ-1475 Pushing Boxes (BFS+优先队列)

    Description Imagine you are standing inside a two-dimensional maze composed of square cells which ma ...

  4. POJ1475 Pushing Boxes(BFS套BFS)

    描述 Imagine you are standing inside a two-dimensional maze composed of square cells which may or may ...

  5. poj1475 Pushing Boxes(BFS)

    题目链接 http://poj.org/problem?id=1475 题意 推箱子游戏.输入迷宫.箱子的位置.人的位置.目标位置,求人是否能把箱子推到目标位置,若能则输出推的最少的路径,如果有多条步 ...

  6. Pushing Boxes POJ - 1475 (嵌套bfs)

    Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not ...

  7. poj1475 -- Pushing Boxes

    这道题其实挺有趣 的,这让我想起小时候诺基亚手机上的推箱子游戏(虽然一点也不好玩) (英文不好-->)  题意翻译: 初始人(S),箱子(B),目的地(T)用人把箱子推到 T最小步数及其路径(满 ...

  8. POJ1475(Pushing Boxes)--bbffss

    题目在这里 题目一看完就忙着回忆童年了.推箱子的游戏. 假设只有一个箱子.游戏在一个R行C列的由单位格子组成的区域中进行,每一步, 你可以移动到相邻的四个格子中的一个,前提是那个格子是空的:或者,如果 ...

  9. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...

随机推荐

  1. Java c3p0 连接 MySQL

    <?xml version="1.0" encoding="UTF-8"?> <!-- 需要导入c3p0驱动jar包和mysql驱动jar包 ...

  2. ssh时传递环境变量

    设置要传递的变量: -o SendEnv=Varname 但是不是每个都能传,受服务器上sshd_config里的下面两个选项的控制: AcceptEnv and PermitUserEnvironm ...

  3. HttpSession相关API

    //获取Session对象 request.getSession() request.getSession(boolean create) //获取SessionId getId() //获取当前se ...

  4. Codeforces 429B B. Working out

    题目意思: 给n*m的矩阵,每个格子有个数,A从(1,1)出发只能向下或右走,终点为(n,m),B从(n,1)出发只能向上或右走,终点为(1,m).两个人的速度不一样,走到的格子可以获的该格子的数,两 ...

  5. Charles常见问题

    Charles常见问题汇总 Charles是一款很好用的抓包修改工具,但是如果你不是很熟悉这个工具的话,肯定会遇到各种感觉很莫名其妙的状况,这里就来帮你一一解答下面再说说charles的一些其他常用的 ...

  6. (字符串)ZigZag Conversion

    [解析] 第一次看到这个题目的人,可能不知道ZigZag是什么意思,简单解释一下,就是把字符串原顺序012345……按下图所示排列: 发现所有行的重复周期都是 2 * nRows - 2 对于首行和末 ...

  7. POJ2442 Sequence(堆的骚操作)

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

  8. delphi 取json中数组的值(ISuperArray)

    { "action": "******", "data": [ { "Info1": { "ID": ...

  9. FileUtils 文件下载 文件导出

    public class FileUtils { /// <summary> /// 文件下载 /// </summary> /// <param name=" ...

  10. IIS 6.0 发布网站使用教程

    原文地址:http://wenku.baidu.com/view/95d8b49851e79b89680226aa.html