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. 数据预处理:PCA,SVD,whitening,normalization

    数据预处理是为了让算法有更好的表现,whitening.PCA.SVD都是预处理的方式: whitening的目标是让特征向量中的特征之间不相关,PCA的目标是降低特征向量的维度,SVD的目标是提高稀 ...

  2. Mask_RCNN openpose AlphaPose Kinect姿态识别

    1.Mask_RCNN ---------------------------------------------------------------------------------------- ...

  3. 680. Valid Palindrome II 对称字符串-可删字母版本

    [抄题]: Given a non-empty string s, you may delete at most one character. Judge whether you can make i ...

  4. spring框架 事务 xml配置方式

    user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...

  5. linux安全关机脚本

    linux安全关机脚本 在断电4分钟后判断关键 目的:在断电以后服务器连接UPS,UPS最多只能支持5分钟也会没电,所以在这里做个判断,如果断电4分钟后,市电还没来就关机. 以下两个设备为两个下路由器 ...

  6. dataframe 用法总结

    http://pda.readthedocs.io/en/latest/chp5.html data = [] 列表初始化 data = (,) data = {} 字典初始化 data = pd.D ...

  7. Regularization and model selection

    Suppose we are trying select among several different models for a learning problem.For instance, we ...

  8. 编写高质量代码改善C#程序的157个建议——建议7: 将0值作为枚举的默认值

    建议7: 将0值作为枚举的默认值 允许使用的枚举类型有byte.sbyte.short.ushort.int.uint.long和ulong.应该始终将0值作为枚举类型的默认值.不过,这样做不是因为允 ...

  9. 在MYSQL中运用全文索引(FULLTEXT index)

    在MYSQL中使用全文索引(FULLTEXT index) MYSQL的一个很有用的特性是使用全文索引(FULLTEXT index)查找文本的能力.目前只有使用MyISAM类型表的时候有效(MyIS ...

  10. C++语法知识小结(持续更新中)

    1)在适用构造函数创建对象时,有时会创建临时对象.如 Stock::Stock(const std::string & co,long n,double pr); 在使用时,下面两条语句有根本 ...