【POJ 1475】 Pushing Boxes
【题目链接】
http://poj.org/problem?id=1475
【算法】
双重BFS
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 30 int n,m,i,j,px,py,bx,by,ex,ey,TC;
string t;
char mp[MAXN][MAXN];
bool visited1[MAXN][MAXN],visited2[MAXN][MAXN]; const int dx[] = {,,-,};
const int dy[] = {-,,,};
const char box_path[] = "WENS";
const char people_path[] = "wens"; struct Box
{
int bx,by,px,py;
string path;
};
struct People
{
int x,y;
string path;
};
inline bool ok(int x,int y)
{
return x >= && x <= n && y >= && y <= m;
}
inline bool bfs2(int sx,int sy,int ex,int ey,int nx,int ny)
{
int i,tx,ty;
People cur;
queue< People > q;
while (!q.empty()) q.pop();
memset(visited2,false,sizeof(visited2));
q.push((People){sx,sy,""});
while (!q.empty())
{
cur = q.front();
q.pop();
if (cur.x == ex && cur.y == ey)
{
t = cur.path;
return true;
}
for (i = ; i < ; i++)
{
tx = cur.x + dx[i];
ty = cur.y + dy[i];
if (ok(tx,ty) && !visited2[tx][ty] && mp[tx][ty] != '#')
{
if (tx == nx && ty == ny) continue;
visited2[tx][ty] = true;
q.push((People){tx,ty,cur.path+people_path[i]});
}
}
}
return false;
}
inline void bfs1()
{
int i,tx,ty;
queue<Box> q;
Box cur;
memset(visited1,false,sizeof(visited1));
while (!q.empty()) q.pop();
q.push((Box){bx,by,px,py,""});
while (!q.empty())
{
cur = q.front();
q.pop();
if (cur.bx == ex && cur.by == ey)
{
cout<< cur.path << endl;
return;
}
for (i = ; i < ; i++)
{
tx = cur.bx + dx[i];
ty = cur.by + dy[i];
if (ok(tx,ty) && !visited1[tx][ty] && mp[tx][ty] != '#')
{
if (bfs2(cur.px,cur.py,cur.bx-dx[i],cur.by-dy[i],cur.bx,cur.by))
{
visited1[tx][ty] = true;
q.push((Box){tx,ty,cur.bx,cur.by,cur.path+t+box_path[i]});
}
}
}
}
printf("Impossible.\n");
} int main()
{ while (scanf("%d%d",&n,&m) != EOF && n && m)
{
getchar();
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
mp[i][j] = getchar();
if (mp[i][j] == 'S')
{
px = i;
py = j;
}
if (mp[i][j] == 'B')
{
bx = i;
by = j;
}
if (mp[i][j] == 'T')
{
ex = i;
ey = j;
}
}
getchar();
}
printf("Maze #%d\n",++TC);
bfs1();
printf("\n");
} return ; }
【POJ 1475】 Pushing Boxes的更多相关文章
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
- 【POJ 1125】Stockbroker Grapevine
id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...
随机推荐
- HashTable, HashSet, HashMap的区别
HashTable, HashSet, HashMap的区别 hash是一种很常见也很重要的数据结构,是用hash函数根据键值(key)计算出存储地址,以便直接访问.由完美hash函数(即键值 ...
- Python语言之控制流(if...elif...else,while,for,break,continue)
1.if...elif...else... number = 23 guess = int(input('Enter an integer : ')) if guess == number: prin ...
- 在PHP中调用php_ssh实现远程登陆linux服务器并执行shell脚本。
这个功能主要用于在web端利用程序对远程服务器进行操作,通过PHP_ssh执行shell脚本来实现. 首先要安装php_ssh2组件,linux中centos7下有ssh2源,直接安装.window下 ...
- (转)学习淘淘商城第二十二课(KindEditor富文本编辑器的使用)
http://blog.csdn.net/u012453843/article/details/70184155 上节课我们一起学习了怎样解决KindEditor富文本编辑器上传图片的浏览器兼容性问题 ...
- Python虚拟环境和requirements.txt文件的使用
参考: https://www.centos.bz/2018/05/centos-7-4-%E5%AE%89%E8%A3%85python3%E5%8F%8A%E8%99%9A%E6%8B%9F%E7 ...
- css中有些属性的前面会加上“*”或“_(兼容IE浏览器)
给不同浏览器识别: color{ background-color: #CC00FF; /*所有浏览器都会显示为紫色*/ background-color: #FF0000\9; /*IE6.IE7. ...
- CAD在图纸保存的同时,也把基本信息保存了(网页版)
主要用到函数说明: MxDrawXCustomFunction::Mx_SaveDwgToURLEx 保存DWG文件到服务器上的扩展函数.详细说明如下: 参数 说明 pszServerUrl 服务器网 ...
- POJ3984——迷宫问题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31616 Accepted: 18100 Descriptio ...
- python数字取反~
>>> a = [1,2,3,4,5,7,6,4,2,10] >>> h = len(a)//2 >>> h 5 >>> ~h ...
- 16.copy_to定制组合field解决cross-fields搜索弊端
主要知识点: 在index的mapping中加copy_to字段的方法 copy_to搜索方法 用most_fields策略,去实现cross-fields搜索,有3大弊端,为了解决这三个弊端 ...