【题目链接】

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的更多相关文章

  1. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  2. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  3. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  4. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  5. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  6. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  7. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  8. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

  9. 【POJ 1125】Stockbroker Grapevine

    id=1125">[POJ 1125]Stockbroker Grapevine 最短路 只是这题数据非常水. . 主要想大牛们试试南阳OJ同题 链接例如以下: http://acm. ...

随机推荐

  1. WEB文件上传之apache common upload使用(一)

    文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...

  2. 北京Python开发培训怎么选?

    北京的地理优势和经济优势基本无需多言,作为全国机会最多的地方,吸引了无数的北漂前赴后继.作为中国互联网中心之一,北京有海量Python岗位正在等待大家淘金. 近几年中,Python一直是市场上最受欢迎 ...

  3. 第一节:setTimeout和setInterval定时器

    区别:  setInterval函数的用法与setTimeout完全一致,区别仅仅在于setInterval指定某个任务每隔一段时间就执行一次,也就是无限次的定时执行. 取消定时器:(clearTim ...

  4. 基于owncloud构建私有云储存网盘

    注意事项:需要ping通外网 需要LAMP架构yum -y install httpd php php-mysql mariadb-server mariadb sqlite php-dom php- ...

  5. Django - 视图获取请求头

    1.urls.py(url和函数对应关系) 2.通过request.evniron,返回request的所有信息,用索引的方式,获取用户请求头信息. 3.也可以通过key,value方式,来展示请求头 ...

  6. swift-UINavigationController纯代码自定义导航控制器及底部工具栏的使用

    step1:自定义一个类  NTViewController,该类继承UITabBarController: // // NTViewController.swift // Housekeeper / ...

  7. 理解Mysql prepare预处理语句

    MySQL 5.1对服务器一方的预制语句提供支持.如果您使用合适的客户端编程界面,则这种支持可以发挥在MySQL 4.1中实施的高效客户端/服务器二进制协议的优势.候选界面包括MySQL C API客 ...

  8. PHP排序算法之快速排序

    原理:找到当前数组中的任意一个元素(一般选择第一个元素),作为标准,新建两个空数组left.rignt,遍历整个数组元素,如果遍历到的元素比当前的元素小就放到数组left,比当前的元素大放到rignt ...

  9. PAT 1111 Online Map

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  10. hdu2005 第几天?【C++】

    第几天? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...