【题目链接】

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. HDU_1532_最大流

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. 文件上传原理--FileReader

    单个文件:<div> <input value="上传" type="file" id="photos_upload"&g ...

  3. mysql动态执行sql批量删除数据

    CREATE PROCEDURE `sp_delete_pushmsg_data`() BEGIN ); ); declare l_dutyno int; ; ; ; ; day),'%Y-%m-%d ...

  4. TP调用JS

    echo "<script>alert('删除成功');window.location.href='?c=Banner&a=index' </script>& ...

  5. c#用控制台程序安装启动停止卸载服务

    第一步:新建控制台项目  第二步:添加服务 第三步:右键新建完成的服务项 点击 在start 和stop事件中分别写上   第四步 编写代码 双击打开 using System; using Syst ...

  6. sysbench测试阿里云ECS云磁盘的IOPS,吞吐量

    测试阿里云ECS 对象:在aliyun上买了一个ECS附加的云盘,使用sysbench测试云盘的IOPS和吞吐量 sysbench prepare 准备文件,10个文件,1个1G [root@iZwz ...

  7. 【JAVA】简陋的学生信息管理系统

    因为之前写了一个学生信息管理系统,但还是处于命令行界面,不美观,于是打算做一个完整的界面出来. 在网上查阅资料后发现C++本身是不支持图形化界面的(可以使用第三方的Qt来做) 权衡之下还是选择了JAV ...

  8. 学习Spring框架等技术的方向、方法和动机

    学习Spring框架最早学习Spring框架是在大二的时候,当时看了几本书,看了一些视频,主要是传智播客的.更多的,还是写代码,单独写Spring的,也有与Struts和Hibernate等框架整合的 ...

  9. HOG特征过程解释(转)

    1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的 ...

  10. 用R语言 画条形图(基于ggplot2包)

    1.用qplot(x,data=data,geom.=”bar”,weight=y)+scale_y_continuous("y")画出y关于x的条形. 图中提示binwidth这 ...