POJ 1475 Pushing Boxes 2021-12-27 搜索 搜索这种东西只要写之前规划得当还是蛮顺手的。。 mark[x1][y1][x2][y2]表示小人在(x1,y1) 盒子在(x2,y2)这种状态是否到过。 【POJ 1475 Pushing Boxes】剩下的就是优先队列 + bfs 了,另外开一个栈记录前驱以输出路径。 #include #include #include #include #include #include #include #include #include #pragma comment(linker, "/STACK:1024000000"); #define EPS (1e-8) #define LL long long #define ULL unsigned long long #define _LL __int64 #define _INF 0x3f3f3f3f #define Mod 9999991using namespace std; char Map[24][24]; int mark[22][22][22][22]; struct N { int x1,x2,y1,y2; }; struct Q { int ans,x1,y1,x2,y2,pre; int ans2,site; bool operator < (const Q &a)const{ return (a.ans2 < ans2 || (a.ans2 == ans2 && a.ans < ans)); } }q[200000]; bool Judge(Q f) { if(abs(f.x1-f.x2) + abs(f.y1-f.y2) <= 1) return true; return false; }int jx[] = {-1, 1, 0, 0}; int jy[] = { 0, 0,-1, 1}; void Out(int site,Q f) { if(site == -1) return ; Out(q[site].pre,q[site]); Q t = q[site]; if(t.x1 == f.x1) { if(t.y1+1 == f.y1) { if(t.x2 == f.x2 && t.y2+1 == f.y2) { printf("E"); } else { printf("e"); } } else if(t.y1-1 == f.y1) { if(t.x2 == f.x2 && t.y2-1 == f.y2) { printf("W"); } else { printf("w"); } } } else if(t.y1 == f.y1) { if(t.x1+1 == f.x1) { if(t.y2 == f.y2 && t.x2+1 == f.x2) { printf("S"); } else { printf("s"); } } else if(t.x1-1 == f.x1) { if(t.y2 == f.y2 && t.x2-1 == f.x2) { printf("N"); } else { printf("n"); } } } }void bfs(N s,int n,int m) { memset(mark,-1,sizeof(mark)); mark[s.x1][s.y1][s.x2][s.y2] = 0; priority_queue pq; Q f,t; int S = 0,E = 0; f.ans = 0; f.ans2 = 0; f.pre = -1; f.x1 = s.x1; f.x2 = s.x2; f.y1 = s.y1; f.y2 = s.y2; f.site = 0; q[E++] = f; pq.push(f); while(pq.empty() == false) { f = pq.top(); pq.pop(); if(Map[f.x2][f.y2] == 'T') { Out(f.pre,f); puts(""); return ; }t.pre = f.site; t.ans = f.ans+1; for(int i = 0 ; i < 4; ++i) { t.x1 = f.x1 + jx[i]; t.y1 = f.y1 + jy[i]; t.x2 = f.x2; t.y2 = f.y2; if(1 <= t.x1 && t.x1 <= n && 1 <= t.y1 && t.y1 <= m && Map[t.x1][t.y1] != '#' && -1 == mark[t.x1][t.y1][t.x2][t.y2]) { if(t.x1 != t.x2 || t.y1 != t.y2) { mark[t.x1][t.y1][t.x2][t.y2] = t.ans; t.site = E; t.ans2 = f.ans2; q[E++] = t; pq.push(t); } else { t.x2 = f.x2 + jx[i]; t.y2 = f.y2 + jy[i]; if(1 <= t.x2 && t.x2 <= n && 1 <= t.y2 && t.y2 <= m && Map[t.x2][t.y2] != '#' && -1 == mark[t.x1][t.y1][t.x2][t.y2]) { t.ans2 = f.ans2+1; t.site = E; q[E++] = t; pq.push(t); mark[t.x1][t.y1][t.x2][t.y2] = t.ans; } } } } } printf("Impossible.\n"); }int main() { int i,j,n,m; int icase = 1; while(scanf("%d %d",&n,&m) && (n||m)) { for(i = 1; i <= n; ++i) scanf("%s",Map[i]+1); N s; for(i = 1; i <= n; ++i) { for(j = 1; j <= m; ++j) { if(Map[i][j] == 'S') s.x1 = i,s.y1 = j; else if(Map[i][j] == 'B') s.x2 = i,s.y2 = j; } } printf("Maze #%d\n",icase++); bfs(s,n,m); puts(""); } return 0; } 推荐阅读 宁波轨道交通(宁波地铁9 艾力绅一共出了几款 艾力绅出新款吗 卤汁凤爪怎么做 卤汁凤爪怎么做的 MyBatis|MyBatis Generator配置 可爱的海底生物简笔画 海洋生物简笔画 夏天用什么牌子四件套合适?推荐几款适合夏天的四件套 mysql中进行多表联查方式 mysql数据库多表联合查询 湿疹会不会遗传下一代 河蚌怎样和排骨一起烧汤 体系结构分析,主流数据库管理系统的体系结构分析 三角战略隐藏结局怎么触发 三角战略隐藏结局达成方法介绍 如何处理Win8.1运用IE11显示花屏的问题 买电动车预算有限,爱玛和雅迪买哪个好? 2020-10-20 [Vue] 多个连续修饰符的效果 Python中if判断语句的综合应用(猜拳游戏(包含随机数知识点)) 戒烟最难熬的是哪几天有什么症状 拥有一双天生的桃花眼是怎样的体验? ofo小黄车免费周卡怎么领取?ofo周卡领不到怎么回事 佳能清零软件使用图解-怎么让佳能mg2580墨盒清零 题解|【HNOI2017】大佬-dalao #|蓝桥杯 - [2013年第四届真题]危险系数(割点) 搜索|Wannafly模拟赛3-B 贝伦卡斯泰露(DFS) 思维题|Maze(CodeForces - 377A )(思维,广搜) 搜索|[CF235E]Number Challenge online|Codeforces #245 (Div. 2)C. Xor-tree(DFS&&贪心 算法|《自制搜索引擎》笔记 搜索|业务同学入门搜索,推荐的一些套路方案 ACM|Pushing Boxes (poj 1475 嵌套bfs)